Salut, comment allez-vous ?
J'ai un projet web sous Symfony 5.4 / PHP 8.1 dans un container docker qui utilise API Platform 2.6.8 en plus pour effectuer certains traitement (et potentiellement faire une appli mobile dans le futur.)
Actuellement je suis en train d'écrire des tests pour l'API et je suis confronté à quelque chose que je ne parviens pas à comprendre ni à résoudre :
J'ai la classe App\Tests\Http\Controller\Api\RaceApiTest
qui hérite de App\Tests\ApiTestCase
qui hérite elle même de ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase
Lors que je veux executer une requête de type POST
ou PUT
déclaré comme dans la méthode testCreate()
dans RaceApiTest
, cela va s'exécuter correctement sans problème.
Par contre, si je fais comme dans testPut()
, quand j'exécute le test,il va me retourner l'erreur segmentation fault
sans plus d'informations (rien ne remonte dans les logs)
Ce qui est d'autant plus étrange car sur la méthode testGetCollection()
et testGet()
cela ne pose aucun soucis et tout fonctionne bien
Ce que ça donne en console :
www-data@5cd579b1818c:~/project/symfony$ bin/phpunit --debug --verbose tests/Http/Controller/Api/RaceApiTest.php --filter testGet
PHPUnit 9.5.11 by Sebastian Bergmann and contributors.
Runtime: PHP 8.1.2
Configuration: /var/www/project/symfony/phpunit.xml
Testing App\Tests\Http\Controller\Api\RaceApiTest
Test 'App\Tests\Http\Controller\Api\RaceApiTest::testGetCollection' started
Test 'App\Tests\Http\Controller\Api\RaceApiTest::testGetCollection' ended
Test 'App\Tests\Http\Controller\Api\RaceApiTest::testGet' started
Test 'App\Tests\Http\Controller\Api\RaceApiTest::testGet' ended
Time: 00:06.637, Memory: 64.50 MB
OK (2 tests, 8 assertions)
----
www-data@5cd579b1818c:~/project/symfony$ bin/phpunit --debug --verbose tests/Http/Controller/Api/RaceApiTest.php --filter testCreate
PHPUnit 9.5.11 by Sebastian Bergmann and contributors.
Runtime: PHP 8.1.2
Configuration: /var/www/project/symfony/phpunit.xml
Testing App\Tests\Http\Controller\Api\RaceApiTest
Test 'App\Tests\Http\Controller\Api\RaceApiTest::testCreate' started
Test 'App\Tests\Http\Controller\Api\RaceApiTest::testCreate' ended
Time: 00:04.979, Memory: 56.50 MB
OK (1 test, 1 assertion)
---
www-data@5cd579b1818c:~/project/symfony$ bin/phpunit --debug --verbose tests/Http/Controller/Api/RaceApiTest.php --filter testPut
PHPUnit 9.5.11 by Sebastian Bergmann and contributors.
Runtime: PHP 8.1.2
Configuration: /var/www/project/symfony/phpunit.xml
Testing App\Tests\Http\Controller\Api\RaceApiTest
Test 'App\Tests\Http\Controller\Api\RaceApiTest::testPut' started
Segmentation fault
Mon code :
<?php
// test/ApiTestCase.php
namespace App\Tests;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase as BaseApiTestCase;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
class ApiTestCase extends BaseApiTestCase
{
/**
* Description $client field
*
* @var Client $client
*/
protected Client $client;
/**
* Description setUp function
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->client = static::createClient();
}
<?php
// tests/Http/Controller/Api/RaceApiTest.php
declare(strict_types=1);
namespace App\Tests\Http\Controller\Api;
use App\Domain\Auth\Entity\User;
use App\Domain\Character\Entity\Race;
use App\Tests\ApiTestCase;
use App\Tests\FixturesTrait;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class RaceApiTest extends ApiTestCase
{
use FixturesTrait;
/**
* Description API_URL const
*
* @var string API_URL
*/
private const API_URL = '/api/character/races';
/**
* Description testGetCollection function
*
* @return void
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws DecodingExceptionInterface
*/
public function testGetCollection(): void
{
$this->loadFixtures(['races']);
/** @var ResponseInterface $response */
$response = $this->client->request('GET', self::API_URL);
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/api/contexts/Character/Race',
'@id' => '/api/character/races',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 101,
'hydra:view' => [
'@id' => '/api/character/races?page=1',
'@type' => 'hydra:PartialCollectionView',
'hydra:first' => '/api/character/races?page=1',
'hydra:last' => '/api/character/races?page=4',
'hydra:next' => '/api/character/races?page=2',
],
]);
$this->assertCount(30, $response->toArray()['hydra:member']);
$this->assertMatchesResourceCollectionJsonSchema(Race::class);
}
/**
* Description testCreate function
*
* @return void
* @throws TransportExceptionInterface
*/
public function testCreate(): void
{
/** @var User $users */
$users = $this->loadFixtures(['users']);
$this->login($users['user1']);
$client = static::createClient();
$client->request('POST', self::API_URL, [
'json' => [
'label' => 'Race-2',
'description' => 'bla bla bla',
'enabled' => false,
],
]);
$this->assertResponseRedirects();
}
/**
* Description testGet function
*
* @return void
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function testGet(): void
{
$this->loadFixtures(['races']);
/** @var string $iri */
$iri = $this->findIriBy(Race::class, ['label' => 'Race']);
$this->client->request('GET', $iri);
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertMatchesResourceItemJsonSchema(Race::class);
}
/**
* Description testPut function
*
* @return void
* @throws TransportExceptionInterface
*/
public function testPut(): void
{
/** @var User $users */
$users = $this->loadFixtures(['users', 'races']);
$this->login($users['user1']);
/** @var string $iri */
$iri = $this->findIriBy(Race::class, ['label' => 'Race']);
$this->client->request('PUT', $iri, [
'json' => [
'enabled' => false,
],
]);
$this->assertResponseStatusCodeSame(403);
$this->assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');
}
}
D'avance, merci