Skip to content

Commit

Permalink
Merge pull request #23 from membrane-php/correct-test-namespace
Browse files Browse the repository at this point in the history
Correct Test Namespace
  • Loading branch information
charjr authored Feb 27, 2024
2 parents c13fb31 + 841c73a commit e889c25
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 54 deletions.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"license": "MPL-2.0",
"autoload": {
"psr-4": {
"Membrane\\Laravel\\": "src/",
"Membrane\\Laravel\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Membrane\\Laravel\\Tests\\": "tests/",
"Membrane\\Laravel\\Fixtures\\": "tests/fixtures/"
}
},
Expand Down
11 changes: 6 additions & 5 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<ruleset name="PSR">
<description>PHPCS configuration file.</description>
<file>src</file>
<description>PHPCS configuration file.</description>
<file>src</file>
<file>tests</file>

<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<rule ref="PSR1"/>
<rule ref="PSR12"/>
<rule ref="PSR1"/>
<rule ref="PSR12"/>
</ruleset>
45 changes: 36 additions & 9 deletions tests/ApiProblemBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

declare(strict_types=1);

namespace Membrane\Laravel;
namespace Membrane\Laravel\Tests;

use Membrane\Laravel\ApiProblemBuilder;
use Membrane\Laravel\ToSymfony;
use Membrane\OpenAPI\Exception\CannotProcessSpecification;
use Membrane\Renderer\Renderer;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -17,7 +19,6 @@
#[UsesClass(ToSymfony::class)]
class ApiProblemBuilderTest extends TestCase
{

#[Test]
public function buildFromRendererTest(): void
{
Expand Down Expand Up @@ -48,7 +49,12 @@ public static function dataSetsToBuildFromException(): array
'path not found, no apiResponseTypes' => [
CannotProcessSpecification::pathNotFound('api.json', '/pets'),
new SymfonyResponse(
'{"title":"Not Found","type":"about:blank","status":404,"detail":"\/pets does not match any specified paths in api.json"}',
json_encode([
'title' => 'Not Found',
'type' => 'about:blank',
'status' => 404,
'detail' => '/pets does not match any specified paths in api.json',
]),
404,
['Content-Type' => 'application/problem+json']
),
Expand All @@ -57,7 +63,12 @@ public static function dataSetsToBuildFromException(): array
'path not found, no applicable apiResponseType' => [
CannotProcessSpecification::pathNotFound('api.json', '/pets'),
new SymfonyResponse(
'{"title":"Not Found","type":"about:blank","status":404,"detail":"\/pets does not match any specified paths in api.json"}',
json_encode([
'title' => 'Not Found',
'type' => 'about:blank',
'status' => 404,
'detail' => '/pets does not match any specified paths in api.json',
]),
404,
['Content-Type' => 'application/problem+json']
),
Expand All @@ -66,7 +77,12 @@ public static function dataSetsToBuildFromException(): array
'path not found, applicable apiResponseType' => [
CannotProcessSpecification::pathNotFound('api.json', '/pets'),
new SymfonyResponse(
'{"title":"Not Found","type":"Path Not Found","status":404,"detail":"\/pets does not match any specified paths in api.json"}',
json_encode([
'title' => 'Not Found',
'type' => 'Path Not Found',
'status' => 404,
'detail' => '/pets does not match any specified paths in api.json',
]),
404,
['Content-Type' => 'application/problem+json']
),
Expand All @@ -75,11 +91,20 @@ public static function dataSetsToBuildFromException(): array
'method not found, applicable apiResponseType' => [
CannotProcessSpecification::methodNotFound('get'),
new SymfonyResponse(
'{"title":"Method Not Allowed","type":"Method Not Found","status":405,"detail":"get operation not specified on path"}',
json_encode([
'title' => 'Method Not Allowed',
'type' => 'Method Not Found',
'status' => 405,
'detail' => 'get operation not specified on path',
]),
405,
['Content-Type' => 'application/problem+json']
),
[404 => 'Path Not Found', 405 => 'Method Not Found', 418 => 'I\'m a teapot'],
[
404 => 'Path Not Found',
405 => 'Method Not Found',
418 => 'I\'m a teapot',
],
],
];
}
Expand All @@ -98,7 +123,9 @@ public function buildFromExceptionTest(

self::assertSame($expected->getContent(), $actual->getContent());
self::assertSame($expected->getStatusCode(), $actual->getStatusCode());
self::assertSame($expected->headers->get('Content-Type'), $actual->headers->get('Content-Type'));
self::assertSame(
$expected->headers->get('Content-Type'),
$actual->headers->get('Content-Type')
);
}

}
41 changes: 25 additions & 16 deletions tests/Middleware/RequestValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

declare(strict_types=1);

namespace Membrane\Laravel\Middleware;
namespace Membrane\Laravel\Tests\Middleware;

use Illuminate\Contracts\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Membrane\Laravel\ApiProblemBuilder;
use Membrane\Laravel\Middleware\RequestValidation;
use Membrane\Laravel\ToPsr7;
use Membrane\Laravel\ToSymfony;
use Membrane\OpenAPI\Exception\CannotProcessSpecification;
Expand All @@ -19,7 +20,6 @@
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;


#[CoversClass(RequestValidation::class)]
#[UsesClass(ApiProblemBuilder::class)]
#[UsesClass(ToPsr7::class)]
Expand All @@ -31,18 +31,21 @@ public function registersResultInstanceInContainer(): void
{
$url = '/pets?limit=5&tags[]=cat&tags[]=tabby';
$expected = Result::valid([
'path' => [],
'query' => ['limit' => 5, 'tags' => ['cat', 'tabby']],
'header' => [],
'cookie' => [],
'body' => '',
'request' => ['method' => 'get', 'operationId' => 'findPets'],
]
);
'path' => [],
'query' => ['limit' => 5, 'tags' => ['cat', 'tabby']],
'header' => [],
'cookie' => [],
'body' => '',
'request' => ['method' => 'get', 'operationId' => 'findPets'],
]);

$apiProblemBuilder = self::createStub(ApiProblemBuilder::class);
$container = self::createMock(Container::class);
$sut = new RequestValidation(__DIR__ . '/../fixtures/petstore-expanded.json', $apiProblemBuilder, $container);
$sut = new RequestValidation(
__DIR__ . '/../fixtures/petstore-expanded.json',
$apiProblemBuilder,
$container
);

$container->expects(self::once())
->method('instance')
Expand All @@ -57,17 +60,21 @@ public static function dataSetsThatThrowCannotProcessSpecification(): array
'path not found' => [
'/hats',
Method::GET,
CannotProcessSpecification::pathNotFound('petstore-expanded.json', '/hats'),
CannotProcessSpecification::pathNotFound(
'petstore-expanded.json',
'/hats'
),
],
'method not found' => [
'/pets',
Method::DELETE,
CannotProcessSpecification::methodNotFound(Method::DELETE->value),
CannotProcessSpecification::methodNotFound(
Method::DELETE->value
),
],
];
}


#[Test]
#[DataProvider('dataSetsThatThrowCannotProcessSpecification')]
public function catchesCannotProcessSpecification(
Expand All @@ -87,7 +94,9 @@ public function catchesCannotProcessSpecification(
->method('buildFromException')
->with($expected);

$sut->handle(Request::create($path, $method->value), fn($p) => new Response());
$sut->handle(
Request::create($path, $method->value),
fn($p) => new Response()
);
}

}
24 changes: 19 additions & 5 deletions tests/Middleware/ResponseJsonFlatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

declare(strict_types=1);

namespace Membrane\Laravel\Middleware;
namespace Membrane\Laravel\Tests\Middleware;

use Illuminate\Contracts\Container\Container;
use Illuminate\Http\Request;
use Membrane\Laravel\ApiProblemBuilder;
use Membrane\Laravel\Middleware\ResponseJsonFlat;
use Membrane\Laravel\ToSymfony;
use Membrane\Result\FieldName;
use Membrane\Result\Message;
Expand All @@ -19,7 +20,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;


#[CoversClass(ResponseJsonFlat::class)]
#[UsesClass(ApiProblemBuilder::class)]
#[UsesClass(ToSymfony::class)]
Expand All @@ -35,11 +35,25 @@ public static function dataSetsToHandle(): array
'invalid result returns response with ApiProblem' => [
Result::invalid(
1,
new MessageSet(new FieldName('id', 'pet'), new Message('must be an integer', [])),
new MessageSet(new FieldName('pet'), new Message('%s is a required field', ['name']))
new MessageSet(
new FieldName('id', 'pet'),
new Message('must be an integer', [])
),
new MessageSet(
new FieldName('pet'),
new Message('%s is a required field', ['name'])
)
),
(new SymfonyResponse(
content: '{"errors":{"pet->id":["must be an integer"],"pet":["name is a required field"]},"title":"Request payload failed validation","type":"about:blank","status":400}',
content: json_encode([
'errors' => [
'pet->id' => ['must be an integer'],
'pet' => ['name is a required field'],
],
'title' => 'Request payload failed validation',
'type' => 'about:blank',
'status' => 400,
]),
status: 400,
headers: ['Content-Type' => ['application/problem+json']],
))->setProtocolVersion('1.1'),
Expand Down
34 changes: 29 additions & 5 deletions tests/Middleware/ResponseJsonNestedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

declare(strict_types=1);

namespace Membrane\Laravel\Middleware;
namespace Membrane\Laravel\Tests\Middleware;

use Illuminate\Contracts\Container\Container;
use Illuminate\Http\Request;
use Membrane\Laravel\ApiProblemBuilder;
use Membrane\Laravel\Middleware\ResponseJsonNested;
use Membrane\Laravel\ToSymfony;
use Membrane\Result\FieldName;
use Membrane\Result\Message;
Expand All @@ -19,7 +20,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;


#[CoversClass(ResponseJsonNested::class)]
#[UsesClass(ApiProblemBuilder::class)]
#[UsesClass(ToSymfony::class)]
Expand All @@ -35,11 +35,35 @@ public static function dataSetsToHandle(): array
'invalid result returns response with ApiProblem' => [
Result::invalid(
1,
new MessageSet(new FieldName('id', 'pet'), new Message('must be an integer', [])),
new MessageSet(new FieldName('pet'), new Message('%s is a required field', ['name']))
new MessageSet(
new FieldName('id', 'pet'),
new Message('must be an integer', [])
),
new MessageSet(
new FieldName('pet'),
new Message('%s is a required field', ['name'])
)
),
(new SymfonyResponse(
content: '{"errors":{"errors":[],"fields":{"pet":{"errors":["name is a required field"],"fields":{"id":{"errors":["must be an integer"],"fields":[]}}}}},"title":"Request payload failed validation","type":"about:blank","status":400}',
content: json_encode([
'errors' => [
'errors' => [],
'fields' => [
'pet' => [
'errors' => ['name is a required field'],
'fields' => [
'id' => [
'errors' => ['must be an integer'],
'fields' => [],
],
],
],
],
],
'title' => 'Request payload failed validation',
'type' => 'about:blank',
'status' => 400,
]),
status: 400,
headers: ['Content-Type' => ['application/problem+json']],
))->setProtocolVersion('1.1'),
Expand Down
15 changes: 7 additions & 8 deletions tests/ToPsr7Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

declare(strict_types=1);

namespace Membrane\Laravel;
namespace Membrane\Laravel\Tests;

use Illuminate\Http\Request;
use Membrane\Laravel\ToPsr7;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;

/**
* @covers \Membrane\Laravel\ToPsr7
*/
#[CoversClass(ToPsr7::class)]
class ToPsr7Test extends TestCase
{

/** @test */
public function invokeTurnsLaravelRequestIntoPsrRequest(): void
#[Test]
public function itConvertsLaravelRequestIntoPsrRequest(): void
{
$sut = new ToPsr7();
$request = Request::create('/pets/1');
Expand All @@ -26,5 +26,4 @@ public function invokeTurnsLaravelRequestIntoPsrRequest(): void
self::assertSame('/pets/1', $actual->getUri()->getPath());
self::assertSame('GET', $actual->getMethod());
}

}
11 changes: 6 additions & 5 deletions tests/ToSymfonyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

declare(strict_types=1);

namespace Membrane\Laravel\Tests;

use Membrane\Laravel\ToSymfony;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

/**
* @covers \Membrane\Laravel\ToSymfony
*/
#[CoversClass(ToSymfony::class)]
class ToSymfonyTest extends TestCase
{
/** @test */
public function invokeTest(): void
#[Test]
public function itConvertsPsrResponseToSymfonyResponse(): void
{
$sut = new ToSymfony();
$request = new \Nyholm\Psr7\Response();
Expand Down

0 comments on commit e889c25

Please sign in to comment.