Skip to content

Commit

Permalink
[EasyApiPlatform] Refactor tests (#1391)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexndlm committed Jun 13, 2024
1 parent 93a6228 commit f0361ff
Show file tree
Hide file tree
Showing 32 changed files with 266 additions and 544 deletions.
6 changes: 6 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@

// $envelope->last(Stamp::class) -> instance of "Stamp"
override(\Symfony\Component\Messenger\Envelope::last(0), type(0));

// self::getService(Type::class) → instance of "Type"
override(
\EonX\EasyTest\Traits\ContainerServiceTrait::getService(),
type(0),
);
5 changes: 4 additions & 1 deletion packages/EasyApiPlatform/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
"doctrine/common": "^3.0",
"doctrine/annotations": "^1.14",
"doctrine/doctrine-bundle": "^2.8",
"eonx-com/easy-test": "^5.11",
"mockery/mockery": "^1.6",
"phpspec/prophecy": "^1.0",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^10.2",
"symfony/symfony": "^6.4"
"symfony/browser-kit": "^6.4",
"symfony/framework-bundle": "^6.4",
"symfony/routing": "^6.4"
},
"autoload": {
"psr-4": {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Util\RequestAttributesExtractor;
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
use ApiPlatform\State\Util\RequestAttributesExtractor;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace EonX\EasyApiPlatform\Serializers;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
use ApiPlatform\State\SerializerContextBuilderInterface;
use EonX\EasyApiPlatform\Paginators\CustomPaginatorInterface;
use Symfony\Component\HttpFoundation\Request;

Expand Down
48 changes: 48 additions & 0 deletions packages/EasyApiPlatform/tests/AbstractApiTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace EonX\EasyApiPlatform\Tests;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use EonX\EasyApiPlatform\Tests\Fixtures\App\Kernel\ApplicationKernel;
use EonX\EasyTest\Traits\ArrayAssertionTrait;
use EonX\EasyTest\Traits\ContainerServiceTrait;
use EonX\EasyTest\Traits\PrivatePropertyAccessTrait;
use Symfony\Component\HttpKernel\KernelInterface;

abstract class AbstractApiTestCase extends ApiTestCase
{
use ArrayAssertionTrait;
use ContainerServiceTrait;
use PrivatePropertyAccessTrait;

protected static Client $client;

protected function setUp(): void
{
static::$client = static::createClient(
[],
[
'headers' => ['accept' => ['application/json']],
]
);
}

protected static function createKernel(array $options = []): KernelInterface
{
return new ApplicationKernel('test', false);
}

protected function initDatabase(): void
{
$entityManager = self::getService(EntityManagerInterface::class);
$metaData = $entityManager->getMetadataFactory()
->getAllMetadata();
$schemaTool = new SchemaTool($entityManager);
$schemaTool->dropSchema($metaData);
$schemaTool->updateSchema($metaData);
}
}
43 changes: 13 additions & 30 deletions packages/EasyApiPlatform/tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,24 @@

namespace EonX\EasyApiPlatform\Tests;

use Mockery;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use EonX\EasyApiPlatform\Tests\Fixtures\App\Kernel\ApplicationKernel;
use EonX\EasyTest\Traits\ContainerServiceTrait;
use EonX\EasyTest\Traits\PrivatePropertyAccessTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* This class has for objective to provide common features to all tests without having to update
* the class they all extend.
*/
abstract class AbstractTestCase extends TestCase
abstract class AbstractTestCase extends KernelTestCase
{
protected function tearDown(): void
{
$fs = new Filesystem();
$var = __DIR__ . '/../var';

if ($fs->exists($var)) {
$fs->remove($var);
}
use ContainerServiceTrait;
use PrivatePropertyAccessTrait;

parent::tearDown();
protected function setUp(): void
{
self::bootKernel();
}

/**
* @param class-string|object $target
*/
protected static function mock(string|object $target, ?callable $expectations = null): MockInterface
protected static function createKernel(array $options = []): KernelInterface
{
/** @var \Mockery\MockInterface $mock */
$mock = Mockery::mock($target);

if ($expectations !== null) {
$expectations($mock);
}

return $mock;
return new ApplicationKernel('test', false);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit f0361ff

Please sign in to comment.