Skip to content

Commit

Permalink
Move Unit test to "Unit" dir and add a Feature test. (#37)
Browse files Browse the repository at this point in the history
* Move Unit test to "Unit" dir and add a Feature test.

* Add Laravel framework to composer and update ServiceProviderTest

This commit adds the Laravel framework to the composer.json dependencies and modifies the ServiceProviderTest. Specifically, it binds a mock of the Repository class to the 'config' service within the application object. This will aid in creating a more controlled and dependable testing environment.

* Update access modifier for setUp in ServiceProviderTest

Changed the access modifier for setUp method from "protected" to "public" in ServiceProviderTest. This change ensures correct accessibility as per defined rules.

* Remove static from closure in ServiceProviderTest

Removed the 'static' keyword from the closure used in the 'bind' method within the ServiceProviderTest file. This allows the closure, which is used in testing, to have access to the context it's created in.

* Add orchestra/testbench dependency to composer.json

The "orchestra/testbench" package has been added as a requirement in composer.json to enhance testing capabilities. This package provides a solid base to conduct Laravel package unit tests, further improving code quality and stability.

* Update ServiceProviderTest with mocked configRepository

Implemented mock of the Repository class in the ServiceProviderTest file. This will allow testing of the configRepository's set and get methods, enhancing the reliability and efficiency of our software testing phase.

* Add storage functionality to ServiceProviderTest

This update introduces a mock for the 'exists' and 'makeDirectory' methods from the Storage facade within the ServiceProviderTest. This change allows us to simulate the creation of a cache data directory in the test environment and enhance the overall test coverage.

* Update paths for storage calls in ServiceProviderTest

This commit updates the storage paths used for 'exists' and 'makeDirectory' methods in ServiceProviderTest to be absolute rather than relative. Also, it sets the 'useStoragePath' for the application to the updated path, ensuring consistency between tests and application storage paths.

* Update ServiceProviderTest to assume cache directory exists

The test method in ServiceProviderTest for cache directory existence was modified. Previously, the test assumed that the cache directory doesn't exist and hence it made a directory. Now, the test has been updated to assume that the cache directory already exists.

* Update storage path in ServiceProviderTest

The storage path defined in ServiceProviderTest.php has been updated. This change is reflected in the 'useStoragePath' method which now points to the 'data/storage' directory instead of just 'data'.

* Refactor spaces in string paths in ServiceProviderTest

In the ServiceProviderTest, the paths used in the 'shouldReceive' and 'useStoragePath' methods are slightly refactored. Spaces have been added after the concatenation dot in the directories for increased clarity and consistency according to coding standards.

* Update path in .php-cs-fixer.dist.php file

The commit revises the excluded directory in the .php-cs-fixer.dist.php configuration file. It now properly ignores the 'tests/Unit/Http/Responses/metadata' directory instead of 'tests/Http/Responses/metadata'.

* Update psalm annotations for function parameters

The commit modifies psalm annotations in the Response class for the $items parameters in the functions present. It changes the type from 'Item' to 'list<Item>', thus giving a clearer representation of the expected input.
  • Loading branch information
Dropelikeit authored May 20, 2024
1 parent 7be1765 commit 2c682c5
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
'whitespace_after_comma_in_array' => true,
])
->setFinder(PhpCsFixer\Finder::create()
->exclude(['vendor', 'tests/Http/Responses/metadata'])
->exclude(['vendor', 'tests/Unit/Http/Responses/metadata'])
->in(__DIR__)
)
;
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"require-dev": {
"roave/security-advisories": "dev-latest",
"friendsofphp/php-cs-fixer": "^3.52",
"phpunit/phpunit": "^10.0",
"phpunit/phpunit": "^10.5",
"larastan/larastan": "^2.9",
"orchestra/testbench": "^8.9|^9.0",
"phpstan/phpstan-phpunit": "^1.3",
Expand All @@ -47,7 +47,8 @@
"vimeo/psalm": "^5.23",
"psalm/plugin-laravel": "^2.10",
"psalm/plugin-phpunit": "^0.19",
"infection/infection": "^0.27.10"
"infection/infection": "^0.27.10",
"laravel/framework": "^10.48"
},
"scripts": {
"lint": "parallel-lint --exclude .git --exclude vendor .",
Expand Down
11 changes: 8 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
<text outputFile="php://stdout" showUncoveredFiles="false"/>
</report>
</coverage>
<testsuite name="Laravel-JMS-Serializer">
<directory suffix="Test.php">tests</directory>
</testsuite>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
Expand Down
72 changes: 72 additions & 0 deletions tests/Feature/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\Feature;

use Dropelikeit\LaravelJmsSerializer\Contracts\ResponseBuilder;
use Dropelikeit\LaravelJmsSerializer\Http\Responses\ResponseFactory;
use Dropelikeit\LaravelJmsSerializer\ServiceProvider;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\TestCase;

final class ServiceProviderTest extends TestCase
{
private readonly Application $application;

public function setUp(): void
{
parent::setUp();

$configRepository = $this
->getMockBuilder(Repository::class)
->disableOriginalConstructor()
->getMock();

$configRepository
->expects(self::once())
->method('set')
->with('laravel-jms-serializer', [
'serialize_null' => true,
'serialize_type' => 'json', // Contracts\Config::SERIALIZE_TYPE_XML
'debug' => false,
'add_default_handlers' => true,
'custom_handlers' => [],
]);

$configRepository
->expects(self::exactly(6))
->method('get')
->willReturnOnConsecutiveCalls([], true, 'json', false, true, []);

Storage::shouldReceive('exists')->once()->with(__DIR__ . '/data/storage/framework/cache/data')->andReturn(true);

$this->application = new Application();
$this->application->useStoragePath(__DIR__ . '/data/storage');

$this->application->bind('config', fn () => $configRepository);

$this->application->register(ServiceProvider::class);
}

/**
* @test
*/
public function canBuildResponseFactoryByIdFromConfiguredServiceProvider(): void
{
$responseFactory = $this->application->get('ResponseFactory');

$this->assertInstanceOf(ResponseBuilder::class, $responseFactory);
}

/**
* @test
*/
public function canBuildResponseFactoryByClassConfiguredServiceProvider(): void
{
$responseFactory = $this->application->get(ResponseFactory::class);

$this->assertInstanceOf(ResponseBuilder::class, $responseFactory);
}
}
Empty file added tests/Feature/data/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\Config;
namespace Dropelikeit\LaravelJmsSerializer\Tests\Unit\Config;

use Dropelikeit\LaravelJmsSerializer\Config\Config;
use Dropelikeit\LaravelJmsSerializer\Contracts\CustomHandlerConfiguration;
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\Facades;
namespace Dropelikeit\LaravelJmsSerializer\Tests\Unit\Facades;

use Dropelikeit\LaravelJmsSerializer\Facades\ResponseFactoryFacade;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\Http\Responses;
namespace Dropelikeit\LaravelJmsSerializer\Tests\Unit\Http\Responses;

use Dropelikeit\LaravelJmsSerializer\Config\Config;
use Dropelikeit\LaravelJmsSerializer\Contracts;
use Dropelikeit\LaravelJmsSerializer\Exception\SerializeType;
use Dropelikeit\LaravelJmsSerializer\Http\Responses\ResponseFactory;
use Dropelikeit\LaravelJmsSerializer\Serializer\Factory;
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 Dropelikeit\LaravelJmsSerializer\Tests\Unit\data\ResponseFactory\Dummy;
use Dropelikeit\LaravelJmsSerializer\Tests\Unit\data\ResponseFactory\JsonDummy;
use Dropelikeit\LaravelJmsSerializer\Tests\Unit\data\ResponseFactory\Response;
use Dropelikeit\LaravelJmsSerializer\Tests\Unit\data\ResponseFactory\XmlDummy;
use Illuminate\Http\Response as LaravelResponse;
use InvalidArgumentException;
use JMS\Serializer\SerializationContext;
Expand Down Expand Up @@ -90,7 +90,7 @@ public function canCreateFromArrayIterator(): void

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

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

self::assertEquals(200, $response->getStatusCode());
self::assertEquals('[{"key":"magic_number","value":12}]', $response->getContent());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\Serializer;
namespace Dropelikeit\LaravelJmsSerializer\Tests\Unit\Serializer;

use DateTime;
use Dropelikeit\LaravelJmsSerializer\Config\Config;
use Dropelikeit\LaravelJmsSerializer\Contracts\CustomHandlerConfiguration;
use Dropelikeit\LaravelJmsSerializer\Serializer\Factory;
use Dropelikeit\LaravelJmsSerializer\Tests\Serializer\data\CustomHandler;
use Dropelikeit\LaravelJmsSerializer\Tests\Unit\Serializer\data\CustomHandler;
use InvalidArgumentException;
use JMS\Serializer\Context;
use JMS\Serializer\Handler\HandlerRegistry;
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\Serializer\data;
namespace Dropelikeit\LaravelJmsSerializer\Tests\Unit\Serializer\data;

use Dropelikeit\LaravelJmsSerializer\Contracts\CustomHandlerConfiguration;
use JMS\Serializer\Context;
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;
namespace Dropelikeit\LaravelJmsSerializer\Tests\Unit;

use Dropelikeit\LaravelJmsSerializer\Config\Config;
use Dropelikeit\LaravelJmsSerializer\Http\Responses\ResponseFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

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

use Dropelikeit\LaravelJmsSerializer\Tests\data\ResponseFactory\Response\Item;
use Dropelikeit\LaravelJmsSerializer\Tests\Unit\data\ResponseFactory\Response\Item;
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\data\ResponseFactory;
namespace Dropelikeit\LaravelJmsSerializer\Tests\Unit\data\ResponseFactory;

use JMS\Serializer\Annotation as Serializer;

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

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

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

/**
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\data\ResponseFactory\Response;
namespace Dropelikeit\LaravelJmsSerializer\Tests\Unit\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\data\ResponseFactory;
namespace Dropelikeit\LaravelJmsSerializer\Tests\Unit\data\ResponseFactory;

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

0 comments on commit 2c682c5

Please sign in to comment.