-
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.
Merge pull request #2 from softonic/Add-tests
Added tests
- Loading branch information
Showing
4 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,156 @@ | ||
<?php | ||
|
||
namespace Softonic\RestApiNestedResources\Http\Middleware; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Response; | ||
use Mockery; | ||
use Mockery\MockInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; | ||
|
||
class EnsureModelDoesNotExistTest extends TestCase | ||
{ | ||
private Builder $builder; | ||
|
||
private $model; | ||
|
||
private EnsureModelDoesNotExist $middleware; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->builder = Mockery::mock(Builder::class); | ||
$this->model = Mockery::mock('alias:PlatformVersion'); | ||
|
||
$this->middleware = new EnsureModelDoesNotExist(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
Mockery::close(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function whenModelDoesNotExistItShouldPassToTheNextMiddleware() | ||
{ | ||
$pathParameters = [ | ||
'platform' => 'windows', | ||
]; | ||
$requestParameters = [ | ||
'id_platform' => 'mac', | ||
'id_version' => 'test', | ||
'name' => 'windows test', | ||
]; | ||
|
||
$request = $this->getRequest($pathParameters, $requestParameters); | ||
|
||
$this->builder->shouldReceive('count') | ||
->once() | ||
->andReturn(0); | ||
|
||
$this->model->shouldReceive('where') | ||
->once() | ||
->with([ | ||
'id_platform' => 'windows', | ||
'id_version' => 'test', | ||
]) | ||
->andReturn($this->builder); | ||
|
||
[$controllerResponse, $next] = $this->whenNextMiddlewareIsExecuted(); | ||
|
||
$response = $this->middleware->handle( | ||
$request, | ||
$next, | ||
'PlatformVersion', | ||
'id_platform', | ||
'id_version' | ||
); | ||
|
||
self::assertSame($controllerResponse, $response, 'The response must not be modified'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function whenModelExistsItShouldAbortTheExecution() | ||
{ | ||
$pathParameters = [ | ||
'platform' => 'windows', | ||
]; | ||
$requestParameters = [ | ||
'id_platform' => 'mac', | ||
'id_version' => 'test', | ||
'name' => 'windows test', | ||
]; | ||
|
||
$request = $this->getRequest($pathParameters, $requestParameters); | ||
|
||
$this->builder->shouldReceive('count') | ||
->once() | ||
->andReturn(1); | ||
|
||
$this->model->shouldReceive('where') | ||
->once() | ||
->with([ | ||
'id_platform' => 'windows', | ||
'id_version' => 'test', | ||
]) | ||
->andReturn($this->builder); | ||
|
||
$next = $this->whenNextMiddlewareIsNotExecuted(); | ||
|
||
$parametersToCheck = [ | ||
'id_platform' => 'windows', | ||
'id_version' => 'test', | ||
]; | ||
$this->expectException(ConflictHttpException::class); | ||
$this->expectExceptionMessage( | ||
'PlatformVersion resource already exists for ' . json_encode($parametersToCheck) | ||
); | ||
|
||
$this->middleware->handle( | ||
$request, | ||
$next, | ||
'PlatformVersion', | ||
'id_platform', | ||
'id_version' | ||
); | ||
} | ||
|
||
private function getRequest(array $pathParameters, array $requestParameters): MockInterface | ||
{ | ||
$request = \Mockery::mock(Request::class); | ||
$request->shouldReceive('route->parameters') | ||
->once() | ||
->andReturn($pathParameters); | ||
$request->shouldReceive('all') | ||
->once() | ||
->andReturn($requestParameters); | ||
|
||
return $request; | ||
} | ||
|
||
protected function whenNextMiddlewareIsExecuted(): array | ||
{ | ||
$controllerResponse = Response::getFacadeRoot(); | ||
$next = function (\Illuminate\Http\Request $request) use ($controllerResponse) { | ||
return $controllerResponse; | ||
}; | ||
|
||
return [$controllerResponse, $next]; | ||
} | ||
|
||
protected function whenNextMiddlewareIsNotExecuted(): \Closure | ||
{ | ||
return function (Request $request) { | ||
self::assertTrue(false, 'The next handler must never be executed in this case.'); | ||
}; | ||
} | ||
} |
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,143 @@ | ||
<?php | ||
|
||
namespace Softonic\RestApiNestedResources\Http\Middleware; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Response; | ||
use PHPUnit\Framework\TestCase; | ||
use Softonic\RestApiNestedResources\PreProcessors\EnsureModelExists as EnsureModelExistsProcessor; | ||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; | ||
|
||
class EnsureModelExistsTest extends TestCase | ||
{ | ||
private EnsureModelExistsProcessor $ensureModelExistsProcessor; | ||
|
||
private EnsureModelExists $middleware; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->ensureModelExistsProcessor = \Mockery::mock(EnsureModelExistsProcessor::class); | ||
|
||
$this->middleware = new EnsureModelExists($this->ensureModelExistsProcessor); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function whenModelExistsItShouldPassToTheNextMiddleware() | ||
{ | ||
$pathParameters = [ | ||
'program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', | ||
'platform' => 'windows', | ||
]; | ||
$requestParameters = [ | ||
'id_program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', | ||
'id_platform' => 'mac', | ||
'id_version' => '1.0', | ||
'id_platformversion' => 'test', | ||
]; | ||
$request = $this->getRequest($pathParameters, $requestParameters); | ||
|
||
$this->ensureModelExistsProcessor->shouldReceive('process') | ||
->once() | ||
->with( | ||
'PlatformVersion', | ||
['id_platform', 'id_version=id_platformversion'], | ||
[ | ||
'id_program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', | ||
'id_platform' => 'windows', | ||
'id_version' => '1.0', | ||
'id_platformversion' => 'test', | ||
] | ||
); | ||
|
||
[$controllerResponse, $next] = $this->whenNextMiddlewareIsExecuted(); | ||
|
||
$response = $this->middleware->handle( | ||
$request, | ||
$next, | ||
'PlatformVersion', | ||
'id_platform', | ||
'id_version=id_platformversion' | ||
); | ||
|
||
self::assertSame($controllerResponse, $response, 'The response must not be modified'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function whenModelDoesNotExistItShouldAbortTheExecution() | ||
{ | ||
$pathParameters = [ | ||
'program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', | ||
'platform' => 'windows', | ||
]; | ||
$requestParameters = [ | ||
'id_program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', | ||
'id_platform' => 'mac', | ||
'id_version' => '1.0', | ||
'id_platformversion' => 'test', | ||
]; | ||
$request = $this->getRequest($pathParameters, $requestParameters); | ||
|
||
$this->ensureModelExistsProcessor->shouldReceive('process') | ||
->once() | ||
->with( | ||
'PlatformVersion', | ||
['id_platform', 'id_version=id_platformversion'], | ||
[ | ||
'id_program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', | ||
'id_platform' => 'windows', | ||
'id_version' => '1.0', | ||
'id_platformversion' => 'test', | ||
] | ||
) | ||
->andThrow(new ConflictHttpException('error message')); | ||
|
||
$next = $this->whenNextMiddlewareIsNotExecuted(); | ||
|
||
$this->expectException(ConflictHttpException::class); | ||
$this->expectExceptionMessage('error message'); | ||
|
||
$this->middleware->handle( | ||
$request, | ||
$next, | ||
'PlatformVersion', | ||
'id_platform', | ||
'id_version=id_platformversion' | ||
); | ||
} | ||
|
||
private function getRequest(array $pathParameters, array $requestParameters): Request | ||
{ | ||
$request = \Mockery::mock(Request::class); | ||
$request->shouldReceive('route->parameters') | ||
->once() | ||
->andReturn($pathParameters); | ||
$request->shouldReceive('all') | ||
->once() | ||
->andReturn($requestParameters); | ||
|
||
return $request; | ||
} | ||
|
||
private function whenNextMiddlewareIsExecuted(): array | ||
{ | ||
$controllerResponse = Response::getFacadeRoot(); | ||
$next = function (Request $request) use ($controllerResponse) { | ||
return $controllerResponse; | ||
}; | ||
|
||
return [$controllerResponse, $next]; | ||
} | ||
|
||
private function whenNextMiddlewareIsNotExecuted(): \Closure | ||
{ | ||
return function (Request $request) { | ||
self::assertTrue(false, 'The next handler must never be executed in this case.'); | ||
}; | ||
} | ||
} |
Oops, something went wrong.