Skip to content

Commit

Permalink
Refactor tests and enhance JSON response
Browse files Browse the repository at this point in the history
The tests have been restructured and json response has been enhanced. All test-related things within the "ResponseFactory" directory have been moved to a new "data" directory. Also, the composer.json file and ServiceProvider.php have been updated with new dependencies and adjustments. Updates to ResponseFactoryTest.php now allow JSON responses as well.
  • Loading branch information
Dropelikeit committed May 13, 2024
1 parent 89b60d8 commit 2787d2f
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ Homestead.json
composer.lock

/tmp
/build
/tests/Http/Responses/metadata
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
"vimeo/psalm": "^5.23",
"psalm/plugin-laravel": "^2.10",
"psalm/plugin-phpunit": "^0.19",
"infection/infection": "^0.27.10"
"infection/infection": "^0.27.10",
"phpat/phpat": "^0.10.15",
"phpstan/extension-installer": "^1.3"
},
"scripts": {
"lint": "parallel-lint --exclude .git --exclude vendor .",
Expand All @@ -69,7 +71,8 @@
},
"config": {
"allow-plugins": {
"infection/extension-installer": true
"infection/extension-installer": true,
"phpstan/extension-installer": true
}
}
}
10 changes: 5 additions & 5 deletions infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
"@default": true
},
"logs": {
"html": "infection.html",
"summary": "summary.log",
"json": "infection-log.json",
"perMutator": "per-mutator.md",
"html": "build/infection/infection.html",
"summary": "build/infection/summary.log",
"json": "build/infection/infection-log.json",
"perMutator": "build/infection/per-mutator.md",
"github": true,
"stryker": {
"badge": "master"
},
"summaryJson": "summary.json"
"summaryJson": "build/infection/summary.json"
},
}
1 change: 0 additions & 1 deletion src/Http/Responses/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ private function getResponse(string $content): Response
return new JsonResponse(
data: $content,
status: $this->status,
headers: [self::HEADER_NAME_CONTENT_TYPE => self::HEADER_VALUE_APPLICATION_JSON],
json: true
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Dropelikeit\LaravelJmsSerializer\Http\Responses\ResponseFactory;
use Dropelikeit\LaravelJmsSerializer\Serializer\Factory;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use function sprintf;
Expand Down Expand Up @@ -64,8 +65,7 @@ public function register(): void

$this->app->bind(ResponseBuilder::class, ResponseFactory::class);

$app = $this->app;
$this->app->bind('ResponseFactory', static function ($app): ResponseFactory {
$this->app->bind('ResponseFactory', static function (Application $app): ResponseFactory {
return $app->get(ResponseFactory::class);
});
}
Expand Down
59 changes: 48 additions & 11 deletions tests/Http/Responses/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use Dropelikeit\LaravelJmsSerializer\Exception\SerializeType;
use Dropelikeit\LaravelJmsSerializer\Http\Responses\ResponseFactory;
use Dropelikeit\LaravelJmsSerializer\Serializer\Factory;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Dummy;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Response;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\XmlDummy;
use Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory\Dummy;
use Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory\JsonDummy;
use Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory\Response;
use Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory\XmlDummy;
use Illuminate\Http\Response as LaravelResponse;
use InvalidArgumentException;
use JMS\Serializer\SerializationContext;
Expand All @@ -19,6 +20,7 @@
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
* @author Marcel Strahl <info@marcel-strahl.de>
Expand Down Expand Up @@ -88,7 +90,7 @@ public function canCreateFromArrayIterator(): void

$responseFactory = new ResponseFactory((new Factory())->getSerializer($this->config), $this->config);

$response = $responseFactory->create(Response::create([new Response\Item()]));
$response = $responseFactory->create(Response::create([new \Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory\Response\Item()]));

self::assertEquals(200, $response->getStatusCode());
self::assertEquals('[{"key":"magic_number","value":12}]', $response->getContent());
Expand All @@ -114,7 +116,7 @@ public function canCreateJsonResponseFromArray(): void

$responseFactory = new ResponseFactory((new Factory())->getSerializer($this->config), $this->config);

$response = $responseFactory->createFromArray(require __DIR__ . '/../../ResponseFactory/dummy_array.php');
$response = $responseFactory->createFromArray(require __DIR__ . '/../../data/ResponseFactory/dummy_array.php');

self::assertEquals(200, $response->getStatusCode());
self::assertEquals(
Expand Down Expand Up @@ -143,7 +145,7 @@ public function canCreateXmlResponseFromArray(): void

$responseFactory = new ResponseFactory((new Factory())->getSerializer($this->config), $this->config);

$response = $responseFactory->createFromArray(require __DIR__ . '/../../ResponseFactory/dummy_array.php');
$response = $responseFactory->createFromArray(require __DIR__ . '/../../data/ResponseFactory/dummy_array.php');

self::assertEquals(200, $response->getStatusCode());
self::assertEquals(
Expand Down Expand Up @@ -376,6 +378,14 @@ public function throwInvalidArgumentExceptionIfContentOnCreateFromArrayMethodIsE
#[Test]
public function canDetectIfSerializeTypeIsXmlResultResponseHasXml(): void
{
$expectedResponse = new LaravelResponse(
content: '<?xml version="1.0" encoding="UTF-8"?>
<XmlDummy title="My test"/>
',
status: 200,
headers: ['Content-Type' => 'application/xml']
);

$this->config
->expects(self::once())
->method('getCacheDir')
Expand All @@ -395,11 +405,38 @@ public function canDetectIfSerializeTypeIsXmlResultResponseHasXml(): void

$response = $responseFactory->create(new XmlDummy());

$this->assertEquals(
'<?xml version="1.0" encoding="UTF-8"?>
<XmlDummy title="My test"/>
',
$response->getContent(),
$this->assertEquals($expectedResponse, $response);
}

#[Test]
public function canDetectIfSerializeTypeIsJSONResultResponseHasJSON(): void
{
$expectedResponse = new JsonResponse(
data: '{"title":"My test"}',
status: 200,
headers: ['Content-Type' => 'application/json'],
json: true
);

$this->config
->expects(self::once())
->method('getCacheDir')
->willReturn(__DIR__);

$this->config
->expects(self::once())
->method('debug')
->willReturn(true);

$this->config
->expects(self::once())
->method('getSerializeType')
->willReturn('json');

$responseFactory = new ResponseFactory((new Factory())->getSerializer($this->config), $this->config);

$response = $responseFactory->create(new JsonDummy());

$this->assertEquals($expectedResponse, $response);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory;
namespace Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory;

use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Response\Item;
use Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory\Response\Item;
use JMS\Serializer\Annotation as Serializer;

/**
Expand All @@ -25,7 +25,7 @@ final class Dummy
/**
* @Serializer\Type("array<Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Response\Item>")
* @var array<int, Item>|null
* @psalm-param list<Item>
* @psalm-param Item
*/
public ?array $items = null;

Expand Down
22 changes: 22 additions & 0 deletions tests/data/ResponseFactory/JsonDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory;

use JMS\Serializer\Annotation as Serializer;

final class JsonDummy
{
#[Serializer\Type(values: 'string')]
private readonly string $title;

public function __construct()
{
$this->title = 'My test';
}

public function getTitle(): string
{
return $this->title;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory;
namespace Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory;

use ArrayIterator;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Response\Item;
use Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory\Response\Item;
use Webmozart\Assert\Assert;

/**
Expand All @@ -15,7 +15,7 @@ final class Response extends ArrayIterator
{
/**
* @param array<int, Item> $items
* @psalm-param list<Item> $items
* @psalm-param Item $items
*/
private function __construct(array $items)
{
Expand All @@ -27,7 +27,7 @@ private function __construct(array $items)

/**
* @param array<int, Item> $items
* @psalm-param list<Item> $items
* @psalm-param Item $items
*
* @return self
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Response;
namespace Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory\Response;

use JMS\Serializer\Annotation as Serializer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory;
namespace Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory;

use JMS\Serializer\Annotation\XmlAttribute;
use JMS\Serializer\Annotation\XmlRoot;
Expand Down
File renamed without changes.

0 comments on commit 2787d2f

Please sign in to comment.