Skip to content

Commit

Permalink
chore: migrate to phpunit 10
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Jan 15, 2024
1 parent 3480b78 commit ab5b189
Show file tree
Hide file tree
Showing 22 changed files with 270 additions and 446 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
run: docker compose exec -T php bin/console -e test doctrine:migrations:migrate --no-interaction
-
name: Run PHPUnit
run: docker compose exec -T php bin/phpunit
run: docker compose exec -T php vendor/bin/phpunit
-
name: Doctrine Schema Validator
run: docker compose exec -T php bin/console -e test doctrine:schema:validate
Expand Down
10 changes: 5 additions & 5 deletions api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
/.php-cs-fixer.cache
###< friendsofphp/php-cs-fixer ###

###> symfony/phpunit-bridge ###
.phpunit.result.cache
/phpunit.xml
###< symfony/phpunit-bridge ###

###> phpstan/phpstan ###
phpstan.neon
###< phpstan/phpstan ###

###> phpunit/phpunit ###
/phpunit.xml
.phpunit.result.cache
###< phpunit/phpunit ###
1 change: 0 additions & 1 deletion api/phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ parameters:
- var/cache/dev/Symfony/Config
bootstrapFiles:
- vendor/autoload.php
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
- src/Kernel.php
ignoreErrors:
- message: '#no (?:return|value) type specified in iterable type array\.#'
14 changes: 4 additions & 10 deletions api/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
convertDeprecationsToExceptions="false"
cacheDirectory=".phpunit.cache"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="APP_DEBUG" value="false" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
</php>

<testsuites>
Expand All @@ -24,17 +22,13 @@
</testsuite>
</testsuites>

<coverage processUncoveredFiles="true">
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</source>

<extensions>
<extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
<bootstrap class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
</extensions>
</phpunit>
29 changes: 14 additions & 15 deletions api/symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@
"phpstan.dist.neon"
]
},
"phpunit/phpunit": {
"version": "10.5",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "9.6",
"ref": "7364a21d87e658eb363c5020c072ecfdc12e2326"
},
"files": [
".env.test",
"phpunit.xml.dist",
"tests/bootstrap.php"
]
},
"psr/cache": {
"version": "3.0.0"
},
Expand Down Expand Up @@ -377,21 +391,6 @@
"symfony/password-hasher": {
"version": "v6.0.2"
},
"symfony/phpunit-bridge": {
"version": "6.3",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "6.3",
"ref": "01dfaa98c58f7a7b5a9b30e6edb7074af7ed9819"
},
"files": [
".env.test",
"bin/phpunit",
"phpunit.xml.dist",
"tests/bootstrap.php"
]
},
"symfony/polyfill-php70": {
"version": "v1.20.0"
},
Expand Down
97 changes: 30 additions & 67 deletions api/tests/Api/Admin/BookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
use App\Tests\Api\Admin\Trait\UsersDataProviderTrait;
use App\Tests\Api\Trait\SecurityTrait;
use App\Tests\Api\Trait\SerializerTrait;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mercure\Update;
use Zenstruck\Foundry\FactoryCollection;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
use PHPUnit\Framework\Attributes\Test;

final class BookTest extends ApiTestCase
{
Expand All @@ -35,11 +37,8 @@ protected function setup(): void
$this->client = self::createClient();
}

/**
* @dataProvider getNonAdminUsers
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getNonAdminUsers')]
public function asNonAdminUserICannotGetACollectionOfBooks(int $expectedCode, string $hydraDescription, ?UserFactory $userFactory): void
{
$options = [];
Expand All @@ -62,11 +61,8 @@ public function asNonAdminUserICannotGetACollectionOfBooks(int $expectedCode, st
]);
}

/**
* @dataProvider getUrls
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getUrls')]
public function asAdminUserICanGetACollectionOfBooks(FactoryCollection $factory, string $url, int $hydraTotalItems, int $itemsPerPage = null): void
{
// Cannot use Factory as data provider because BookFactory has a service dependency
Expand Down Expand Up @@ -132,9 +128,7 @@ public static function getUrls(): iterable
];
}

/**
* @test
*/
#[Test]
public function asAdminUserICanGetACollectionOfBooksOrderedByTitle(): void
{
BookFactory::createOne(['title' => 'Hyperion']);
Expand All @@ -155,11 +149,8 @@ public function asAdminUserICanGetACollectionOfBooksOrderedByTitle(): void
self::assertMatchesJsonSchema(file_get_contents(__DIR__ . '/schemas/Book/collection.json'));
}

/**
* @dataProvider getAllUsers
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getAllUsers')]
public function asAnyUserICannotGetAnInvalidBook(?UserFactory $userFactory): void
{
BookFactory::createOne();
Expand All @@ -184,11 +175,8 @@ public static function getAllUsers(): iterable
yield [UserFactory::new(['roles' => ['ROLE_ADMIN']])];
}

/**
* @dataProvider getNonAdminUsers
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getNonAdminUsers')]
public function asNonAdminUserICannotGetABook(int $expectedCode, string $hydraDescription, ?UserFactory $userFactory): void
{
$book = BookFactory::createOne();
Expand All @@ -213,11 +201,8 @@ public function asNonAdminUserICannotGetABook(int $expectedCode, string $hydraDe
]);
}

/**
* @dataProvider getNonAdminUsers
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getNonAdminUsers')]
public function asAdminUserICanGetABook(): void
{
$book = BookFactory::createOne();
Expand All @@ -240,11 +225,8 @@ public function asAdminUserICanGetABook(): void
self::assertMatchesJsonSchema(file_get_contents(__DIR__ . '/schemas/Book/item.json'));
}

/**
* @dataProvider getNonAdminUsers
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getNonAdminUsers')]
public function asNonAdminUserICannotCreateABook(int $expectedCode, string $hydraDescription, ?UserFactory $userFactory): void
{
$options = [];
Expand Down Expand Up @@ -276,11 +258,8 @@ public function asNonAdminUserICannotCreateABook(int $expectedCode, string $hydr
]);
}

/**
* @dataProvider getInvalidDataOnCreate
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getInvalidDataOnCreate')]
public function asAdminUserICannotCreateABookWithInvalidData(array $data, int $statusCode, array $expected): void
{
$token = $this->generateToken([
Expand All @@ -302,7 +281,7 @@ public function asAdminUserICannotCreateABookWithInvalidData(array $data, int $s
self::assertJsonContains($expected);
}

public function getInvalidDataOnCreate(): iterable
public static function getInvalidDataOnCreate(): iterable
{
yield 'no data' => [
[],
Expand All @@ -322,7 +301,7 @@ public function getInvalidDataOnCreate(): iterable
],
],
];
yield from $this->getInvalidData();
yield from self::getInvalidData();
}

public static function getInvalidData(): iterable
Expand Down Expand Up @@ -385,9 +364,8 @@ public static function getInvalidData(): iterable
/**
* @group apiCall
* @group mercure
*
* @test
*/
#[Test]
public function asAdminUserICanCreateABook(): void
{
$token = $this->generateToken([
Expand Down Expand Up @@ -443,11 +421,8 @@ public function asAdminUserICanCreateABook(): void
);
}

/**
* @dataProvider getNonAdminUsers
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getNonAdminUsers')]
public function asNonAdminUserICannotUpdateBook(int $expectedCode, string $hydraDescription, ?UserFactory $userFactory): void
{
$book = BookFactory::createOne();
Expand Down Expand Up @@ -481,9 +456,7 @@ public function asNonAdminUserICannotUpdateBook(int $expectedCode, string $hydra
]);
}

/**
* @test
*/
#[Test]
public function asAdminUserICannotUpdateAnInvalidBook(): void
{
BookFactory::createOne();
Expand All @@ -506,11 +479,8 @@ public function asAdminUserICannotUpdateAnInvalidBook(): void
self::assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
}

/**
* @dataProvider getInvalidData
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getInvalidData')]
public function asAdminUserICannotUpdateABookWithInvalidData(array $data, int $statusCode, array $expected): void
{
$book = BookFactory::createOne();
Expand All @@ -537,9 +507,8 @@ public function asAdminUserICannotUpdateABookWithInvalidData(array $data, int $s
/**
* @group apiCall
* @group mercure
*
* @test
*/
#[Test]
public function asAdminUserICanUpdateABook(): void
{
$book = BookFactory::createOne([
Expand Down Expand Up @@ -599,11 +568,8 @@ public function asAdminUserICanUpdateABook(): void
);
}

/**
* @dataProvider getNonAdminUsers
*
* @test
*/
#[Test]
#[DataProvider(methodName: 'getNonAdminUsers')]
public function asNonAdminUserICannotDeleteABook(int $expectedCode, string $hydraDescription, ?UserFactory $userFactory): void
{
$book = BookFactory::createOne();
Expand All @@ -628,9 +594,7 @@ public function asNonAdminUserICannotDeleteABook(int $expectedCode, string $hydr
]);
}

/**
* @test
*/
#[Test]
public function asAdminUserICannotDeleteAnInvalidBook(): void
{
BookFactory::createOne();
Expand All @@ -646,9 +610,8 @@ public function asAdminUserICannotDeleteAnInvalidBook(): void

/**
* @group mercure
*
* @test
*/
#[Test]
public function asAdminUserICanDeleteABook(): void
{
$book = BookFactory::createOne(['title' => 'Hyperion']);
Expand Down
Loading

0 comments on commit ab5b189

Please sign in to comment.