Skip to content

Commit

Permalink
Updating dependencies and refactoring ResponseFactoryTest and Seriali…
Browse files Browse the repository at this point in the history
…zer/FactoryTest

This commit updates the version numbers of several dependencies in composer.json and performs a significant refactoring of ResponseFactoryTest and Serializer/FactoryTest. The changes in ResponseFactoryTest include refactoring the test names and adding new test methods. At the same time, additional test methods were added and existing methods were revised. A new file, XmlDummy.php, is also created for testing purposes.
  • Loading branch information
Dropelikeit committed Mar 16, 2024
1 parent b554572 commit 2576b43
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 47 deletions.
28 changes: 14 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
}
],
"require": {
"php": "^8.1",
"illuminate/http": "^9.0|^10.0",
"illuminate/support": "^9.0|^10.0",
"illuminate/contracts": "^9.0|^10.0",
"jms/serializer": "^3.27"
"php": "^8.1|^8.2",
"illuminate/http": "^9.0|^10.0|^11.0",
"illuminate/support": "^9.0|^10.0|^11.0",
"illuminate/contracts": "^9.0|^10.0|^11.0",
"jms/serializer": "^3.30"
},
"autoload": {
"psr-4": {
Expand All @@ -37,17 +37,17 @@
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"friendsofphp/php-cs-fixer": "^3.23",
"phpunit/phpunit": "^10.3",
"nunomaduro/larastan": "^2.6",
"orchestra/testbench": "^8.9",
"friendsofphp/php-cs-fixer": "^3.51",
"phpunit/phpunit": "^10.0",
"larastan/larastan": "^2.9",
"orchestra/testbench": "^8.9|^9.0",
"phpstan/phpstan-phpunit": "^1.3",
"php-parallel-lint/php-parallel-lint": "^1.3",
"symfony/cache": "^6.3",
"vimeo/psalm": "^5.15",
"psalm/plugin-laravel": "^2.8",
"psalm/plugin-phpunit": "^0.18.4",
"infection/infection": "^0.27.6"
"symfony/cache": "^6.3|^7.0",
"vimeo/psalm": "^5.23",
"psalm/plugin-laravel": "^2.10",
"psalm/plugin-phpunit": "^0.19",
"infection/infection": "^0.27.10"
},
"scripts": {
"lint": "parallel-lint --exclude .git --exclude vendor .",
Expand Down
147 changes: 114 additions & 33 deletions tests/Http/Responses/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
use Dropelikeit\LaravelJmsSerializer\Serializer\Factory;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Dummy;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Response;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\XmlDummy;
use Illuminate\Http\Response as LaravelResponse;
use InvalidArgumentException;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -20,10 +25,8 @@
*/
final class ResponseFactoryTest extends TestCase
{
/**
* @psalm-var MockObject&Contracts\Config
*/
private MockObject $config;
private readonly MockObject&Contracts\Config $config;
private readonly MockObject&SerializerInterface $serializer;

public function setUp(): void
{
Expand All @@ -33,11 +36,13 @@ public function setUp(): void
->getMockBuilder(Contracts\Config::class)
->disableOriginalConstructor()
->getMock();

$this->serializer = $this
->getMockBuilder(SerializerInterface::class)
->getMock();
}

/**
* @test
*/
#[Test]
public function canCreateResponse(): void
{
$this->config
Expand All @@ -63,9 +68,7 @@ public function canCreateResponse(): void
self::assertEquals('{"amount":12,"text":"Hello World!"}', $response->getContent());
}

/**
* @test
*/
#[Test]
public function canCreateFromArrayIterator(): void
{
$this->config
Expand All @@ -91,9 +94,7 @@ public function canCreateFromArrayIterator(): void
self::assertEquals('[{"key":"magic_number","value":12}]', $response->getContent());
}

/**
* @test
*/
#[Test]
public function canCreateJsonResponseFromArray(): void
{
$this->config
Expand Down Expand Up @@ -122,9 +123,7 @@ public function canCreateJsonResponseFromArray(): void
);
}

/**
* @test
*/
#[Test]
public function canCreateXmlResponseFromArray(): void
{
$this->config
Expand All @@ -140,7 +139,7 @@ public function canCreateXmlResponseFromArray(): void
$this->config
->expects(self::once())
->method('getSerializeType')
->willReturn(Contracts\Config::SERIALIZE_TYPE_XML);
->willReturn('xml');

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

Expand All @@ -165,9 +164,7 @@ public function canCreateXmlResponseFromArray(): void
);
}

/**
* @test
*/
#[Test]
public function canChangeStatusCode(): void
{
$this->config
Expand Down Expand Up @@ -195,9 +192,7 @@ public function canChangeStatusCode(): void
self::assertEquals('{"amount":12,"text":"Hello World!"}', $response->getContent());
}

/**
* @test
*/
#[Test]
public function canUseGivenContext(): void
{
$this->config
Expand Down Expand Up @@ -225,11 +220,9 @@ public function canUseGivenContext(): void

/**
* @psalm-param Contracts\Config::SERIALIZE_TYPE_* $changeSerializeTypeTo
* @param string $expectedResult
*
* @test
* @dataProvider dataProviderCanSerializeWithSerializeType
*/
#[Test]
#[DataProvider(methodName: 'dataProviderCanSerializeWithSerializeType')]
public function canSerializeWithSerializeType(string $changeSerializeTypeTo, string $expectedResult): void
{
$this->config
Expand Down Expand Up @@ -282,9 +275,7 @@ public static function dataProviderCanSerializeWithSerializeType(): array
];
}

/**
* @test
*/
#[Test]
public function canNotCreateWithUnknownSerializeType(): void
{
$this->expectException(SerializeType::class);
Expand All @@ -310,9 +301,7 @@ public function canNotCreateWithUnknownSerializeType(): void
$responseFactory->withSerializeType('array');
}

/**
* @test
*/
#[Test]
public function canCreateQuietResponse(): void
{
$responseFactory = new ResponseFactory((new Factory())->getSerializer($this->config), $this->config);
Expand All @@ -321,4 +310,96 @@ public function canCreateQuietResponse(): void

$this->assertEquals(new LaravelResponse(status: 204), $response);
}

#[Test]
public function throwInvalidArgumentExceptionIfContentOnCreateMethodIsEmpty(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Expected a different value than "".');

$object = new Dummy();

$this->config
->expects(self::never())
->method('getCacheDir');

$this->config
->expects(self::never())
->method('debug');

$this->config
->expects(self::once())
->method('getSerializeType')
->willReturn(Contracts\Config::SERIALIZE_TYPE_JSON);

$this->serializer
->expects(self::once())
->method('serialize')
->with($object, 'json', null, null)
->wilLReturn('');

(new ResponseFactory($this->serializer, $this->config))->create($object);
}

#[Test]
public function throwInvalidArgumentExceptionIfContentOnCreateFromArrayMethodIsEmpty(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Expected a different value than "".');

$object = new Dummy();

$this->config
->expects(self::never())
->method('getCacheDir');

$this->config
->expects(self::never())
->method('debug');

$this->config
->expects(self::once())
->method('getSerializeType')
->willReturn(Contracts\Config::SERIALIZE_TYPE_JSON);

$this->serializer
->expects(self::once())
->method('serialize')
->with([$object], 'json', null)
->wilLReturn('');

(new ResponseFactory($this->serializer, $this->config))->createFromArray([$object]);
}

#[Test]
public function canDetectIfSerializeTypeIsXmlResultResponseHasXml(): void
{
$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('xml');

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

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

$this->assertEquals(
'<?xml version="1.0" encoding="UTF-8"?>
<XmlDummy title="My test"/>
',
$response->getContent(),
);
}
}
19 changes: 19 additions & 0 deletions tests/ResponseFactory/XmlDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory;

use JMS\Serializer\Annotation\XmlAttribute;
use JMS\Serializer\Annotation\XmlRoot;

#[XmlRoot('XmlDummy')]
final class XmlDummy
{
#[XmlAttribute]
private readonly string $title;

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

0 comments on commit 2576b43

Please sign in to comment.