-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a44756d
commit aced827
Showing
10 changed files
with
371 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OnurSimsek\Precondition\Tests; | ||
|
||
use Illuminate\Foundation\Testing\WithFaker; | ||
use OnurSimsek\Precondition\PreconditionServiceProvider; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
abstract class TestCase extends Orchestra | ||
{ | ||
use WithFaker; | ||
|
||
protected function getPackageProviders($app): array | ||
{ | ||
return [PreconditionServiceProvider::class]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace OnurSimsek\Precondition\Tests\Unit\Attributes; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Date; | ||
use OnurSimsek\Precondition\Attributes\Precondition; | ||
use OnurSimsek\Precondition\Exceptions\PreconditionFailedException; | ||
use OnurSimsek\Precondition\Exceptions\PreconditionRequiredException; | ||
use OnurSimsek\Precondition\Tests\TestCase; | ||
use OnurSimsek\Precondition\Tests\Unit\Fixtures\LostUpdateValidator; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class PreconditionTest extends TestCase | ||
{ | ||
private Precondition $precondition; | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->precondition = new Precondition(LostUpdateValidator::class); | ||
} | ||
|
||
#[Test] | ||
public function throw_precondition_required_exception() | ||
{ | ||
$request = $this->getMockBuilder(Request::class)->getMock(); | ||
$request->expects($this->once()) | ||
->method('header') | ||
->with('If-Unmodified-Since') | ||
->willReturn(null); | ||
|
||
self::expectException(PreconditionRequiredException::class); | ||
$this->precondition->validate($request); | ||
} | ||
|
||
#[Test] | ||
public function throw_precondition_failed_exception() | ||
{ | ||
$updatedAt = Date::create(2024, 1, 15, 10, 00, 00, 'utc'); | ||
|
||
$article = new \stdClass(); | ||
$article->updated_at = $updatedAt; | ||
|
||
$request = $this->getMockBuilder(Request::class)->getMock(); | ||
$request->expects($this->exactly(2)) | ||
->method('header') | ||
->with('If-Unmodified-Since') | ||
->willReturn((clone $updatedAt)->addYear()); | ||
|
||
$request->expects($this->once()) | ||
->method('route') | ||
->with('article') | ||
->willReturn($article); | ||
|
||
self::expectException(PreconditionFailedException::class); | ||
$this->precondition->validate($request); | ||
} | ||
|
||
#[Test] | ||
public function it_can_be_validate_a_proper_request() | ||
{ | ||
$updatedAt = Date::create(2024, 1, 15, 10, 00, 00, 'utc'); | ||
|
||
$article = new \stdClass(); | ||
$article->updated_at = $updatedAt; | ||
|
||
$request = $this->getMockBuilder(Request::class)->getMock(); | ||
$request->expects($this->exactly(2)) | ||
->method('header') | ||
->with('If-Unmodified-Since') | ||
->willReturn($updatedAt); | ||
|
||
$request->expects($this->once()) | ||
->method('route') | ||
->with('article') | ||
->willReturn($article); | ||
|
||
self::assertTrue($this->precondition->validate($request)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace OnurSimsek\Precondition\Tests\Unit\Fixtures; | ||
|
||
use Illuminate\Http\JsonResponse; | ||
use OnurSimsek\Precondition\Attributes\Precondition; | ||
|
||
class Controller | ||
{ | ||
#[Precondition(LostUpdateValidator::class)] | ||
public function withPreconditionMethod(): JsonResponse | ||
{ | ||
return response()->json(); | ||
} | ||
|
||
public function withoutPreconditionMethod(): JsonResponse | ||
{ | ||
return response()->json(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace OnurSimsek\Precondition\Tests\Unit\Fixtures; | ||
|
||
use Illuminate\Http\Request; | ||
use OnurSimsek\Precondition\Validators\PreconditionValidator; | ||
|
||
class LostUpdateValidator extends PreconditionValidator | ||
{ | ||
public function parameter(Request $request): array|string|null | ||
{ | ||
return $request->header('If-Unmodified-Since'); | ||
} | ||
|
||
public function __invoke(Request $request): bool | ||
{ | ||
return $this->parameter($request) == $request->route('article')->updated_at; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OnurSimsek\Precondition\Tests\Unit\Middleware; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Routing\Route; | ||
use Illuminate\Routing\RouteCollectionInterface; | ||
use Illuminate\Routing\Router; | ||
use OnurSimsek\Precondition\Attributes\Precondition; | ||
use OnurSimsek\Precondition\Middleware\PreconditionRequest; | ||
use OnurSimsek\Precondition\Reflection; | ||
use OnurSimsek\Precondition\Tests\TestCase; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class PreconditionRequestTest extends TestCase | ||
{ | ||
#[Test] | ||
public function it_should_not_be_touch_closure_route() | ||
{ | ||
$route = $this->getMockBuilder(Route::class)->disableOriginalConstructor()->getMock(); | ||
$route->expects($this->once()) | ||
->method('getControllerClass') | ||
->willReturn(null); | ||
|
||
$routeCollection = $this->getMockBuilder(RouteCollectionInterface::class)->getMock(); | ||
$routeCollection->expects($this->once()) | ||
->method('match') | ||
->willReturn($route); | ||
|
||
$router = $this->getMockBuilder(Router::class)->disableOriginalConstructor()->getMock(); | ||
$router->expects($this->once()) | ||
->method('getRoutes') | ||
->willReturn($routeCollection); | ||
|
||
$reflection = $this->getReflectionMock(); | ||
$middleware = new PreconditionRequest($router, $reflection); | ||
|
||
self::assertInstanceOf(Response::class, $middleware->handle(new Request(), fn (Request $request) => new Response())); | ||
} | ||
|
||
#[Test] | ||
public function it_should_not_be_touch_when_the_action_doesnt_have_the_attribute() | ||
{ | ||
$route = $this->getMockBuilder(Route::class)->disableOriginalConstructor()->getMock(); | ||
$route->expects($this->exactly(2)) | ||
->method('getControllerClass') | ||
->willReturn('UserController'); | ||
|
||
$route->expects($this->once()) | ||
->method('getActionMethod') | ||
->willReturn('index'); | ||
|
||
$routeCollection = $this->getMockBuilder(RouteCollectionInterface::class)->getMock(); | ||
$routeCollection->expects($this->once()) | ||
->method('match') | ||
->willReturn($route); | ||
|
||
$router = $this->getMockBuilder(Router::class)->disableOriginalConstructor()->getMock(); | ||
$router->expects($this->once()) | ||
->method('getRoutes') | ||
->willReturn($routeCollection); | ||
|
||
$reflection = $this->getReflectionMock(); | ||
$reflection->expects($this->once()) | ||
->method('reflect') | ||
->willReturnSelf(); | ||
|
||
$reflection->expects($this->once()) | ||
->method('hasPreconditionAttribute') | ||
->willReturn(false); | ||
|
||
$middleware = new PreconditionRequest($router, $reflection); | ||
|
||
self::assertInstanceOf(Response::class, $middleware->handle(new Request(), fn (Request $request) => new Response())); | ||
} | ||
|
||
#[Test] | ||
public function it_can_be_validate_precondition_request() | ||
{ | ||
$route = $this->getMockBuilder(Route::class)->disableOriginalConstructor()->getMock(); | ||
$route->expects($this->exactly(2)) | ||
->method('getControllerClass') | ||
->willReturn('UserController'); | ||
|
||
$route->expects($this->once()) | ||
->method('getActionMethod') | ||
->willReturn('index'); | ||
|
||
$routeCollection = $this->getMockBuilder(RouteCollectionInterface::class)->getMock(); | ||
$routeCollection->expects($this->once()) | ||
->method('match') | ||
->willReturn($route); | ||
|
||
$router = $this->getMockBuilder(Router::class)->disableOriginalConstructor()->getMock(); | ||
$router->expects($this->once()) | ||
->method('getRoutes') | ||
->willReturn($routeCollection); | ||
|
||
$precondition = $this->getPreconditionAttributeMock(); | ||
$precondition->expects($this->once()) | ||
->method('validate') | ||
->willReturn(true); | ||
|
||
$reflection = $this->getReflectionMock(); | ||
$reflection->expects($this->once()) | ||
->method('reflect') | ||
->willReturnSelf(); | ||
|
||
$reflection->expects($this->once()) | ||
->method('hasPreconditionAttribute') | ||
->willReturn(true); | ||
|
||
$reflection->expects($this->once()) | ||
->method('getPreconditionInstance') | ||
->willReturn($precondition); | ||
|
||
$middleware = new PreconditionRequest($router, $reflection); | ||
|
||
self::assertInstanceOf(Response::class, $middleware->handle(new Request(), fn (Request $request) => new Response())); | ||
} | ||
|
||
private function getReflectionMock(): \PHPUnit\Framework\MockObject\MockObject|Reflection | ||
{ | ||
return $this->getMockBuilder(Reflection::class)->disableOriginalConstructor()->getMock(); | ||
} | ||
|
||
private function getPreconditionAttributeMock(): \PHPUnit\Framework\MockObject\MockObject | ||
{ | ||
return $this->getMockBuilder(Precondition::class)->disableOriginalConstructor()->getMock(); | ||
} | ||
} |
Oops, something went wrong.