I've a web project on Symfony 5.4 / PHP 8.1
I try wrote tests for my API (Api Platform 2.6.8) but. when I want to execute a request of type POST
or PUT
declared as in the testCreate()
method in RaceApiTest
, it will execute correctly without problem.
On the other hand, if I do as in testPut()
, when I run the test, it will return the error segmentation fault
without more information (nothing goes back in the logs)
What is all the more strange because on the method testGetCollection()
and testGet()
it does not pose any problem and everything works fine
What it gives in 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
My 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');
}
}
Thanks