I'would like to have some functionnal tests inside my ApiPlatform. When I'm inserting a User with postman, I have no probleme. But when I'am using test case I have this error message :
Uncaught PHP Exception Doctrine\ORM\ORMInvalidArgumentException: "A new entity was found through the relationship 'App\Entity\User#profile' that was not configured to cascade persist operations for entity: App\Entity\Profile#000000004079006a00000000554e4576. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example #ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'App\Entity\Profile#__toString()' to get a clue."
Here my testing code :
<?php
namespace App\Tests;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\Entity\User;
class UsersTest extends ApiTestCase
{
public function testCreateBook(): void
{
$response = static::createClient()->request('POST', '/api/users', ['json' => [
'email' => '0099740915',
'username' => 'The Handmaid\'s Tale',
'password' => 'test',
'profile' => [
'firstName' => 'Test FirstName'
]
]]);
$this->assertResponseStatusCodeSame(201);
}
}
My phpunit configuration bellow :
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="bin/.phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="config/bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="7.5" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>
I have have the following documentation from ApiPlatform : https://api-platform.com/docs/distribution/testing/
I don't know, why with a TestCase it's not working. Somebody have an idea ?
On Postman all are pefectly workling
Thanks for your help
Related
Symfony3 Unitests fails with the following error
Undefined index: kernel
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.0/phpunit.xsd"
backupGlobals="false"
colors="true"
backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="app/autoload.php">
<php>
<ini name="error_reporting" value="-1" />
<env name="APP_ENV" value="test" />
<env name="APP_DEBUG" value="1" />
<env name="SHELL_VERBOSITY" value="-1" />
<env name="KERNEL_CLASS" value="AppKernel" />
<env name="DEFAULT_DB_DRIVER" value="pdo_mysql" />
<env name="DEFAULT_DB_HOST" value="mysql" />
<env name="DEFAULT_DB_NAME" value="db" />
<env name="DEFAULT_DB_USER" value="db" />
<env name="DEFAULT_DB_PASSWORD" value="db" />
<env name="MAIL_ENCRYPTION" value="ssl" />
<env name="MAIL_PORT" value="587" />
<env name="MAIL_SERVER" value="localhost" />
<env name="MAIL_USER" value="root" />
<env name="MAIL_PASSWORD" value="root" />
<env name="DEFAULT_DB_PORT" value="3306" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled" />
<server name="KERNEL_DIR" value="app/" />
<server name="KERNEL_CLASS" value="AppKernel" />
<!-- define your env variables for the test env here -->
</php>
<testsuites>
<testsuite name="AmazonAPI Tests">
<directory>src/ProductInterfaceBundle/Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../src/*Bundle/Resources</directory>
<directory>../src/*Bundle/Tests</directory>
<directory>../src/*/*Bundle/Resources</directory>
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Resources</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\CoverageListener">
<arguments>
<null/>
<boolean>true</boolean>
</arguments>
</listener>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
<arguments>
<array>
<element key="time-sensitive"><string>Symfony\Component\HttpFoundation</string></element>
</array>
</arguments>
</listener>
</listeners>
</phpunit>
and hier is my test class
use Namespace\ProductInterfaceBundle\Command\AmazonCommand;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
class AmazonCommandTest extends KernelTestCase
{
public function testExecute()
{
self::bootKernel();
$application = new Application(self::$kernel);
$application->add(new AmazonCommand());
$command = $application->find('vendor:amazon:submit
');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
// pass arguments to the helper
'username' => 'Wouter',
));
$output = $commandTester->getDisplay();
$this->assertContains('Username: Wouter', $output);
}
}
What I'm missing in this case?
I guess you need to share more of you code: Where exactly is this errormessage thrown and what happens at this line?
Seems like there is an uninitialized array with the "kernel" key missing. But without code: I have no idea. So I guess: More code needed :-)
btw: You shall update your Symfony (I think 6.0 is latest) and PHPUnit (I think version 9.5 is latest)
i am trying to apply functional test using WebTestCase.
class CalculatorControllerTest extends WebTestCase
{
public function testSumPost()
{
$client = static::createClient();
$client->request('GET', '/calculator/sum');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
I have the following error message when i run test:
You cannot create the client used in functional tests if the "framework.test" config is not set to true
My test framework:
framework:
test: ~
session:
storage_id: session.storage.mock_file
router:
resource: "%kernel.root_dir%/config/routing_test.yml"
profiler:
collect: false
My phpunit.xml: (i tries also test instead of dev in APP_ENV & Debug 1 instead of 0)
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
bootstrap = "config/bootstrap.php" >
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="AppKernel" />
<env name="APP_ENV" value="dev" />
<env name="APP_DEBUG" value="0" />
<env name="SHELL_VERBOSITY" value="-1" />
<!-- define your env variables for the test env here -->
</php>
<testsuites>
<testsuite name="Plein Test Suite">
<directory>tests/unit</directory>
</testsuite>
</testsuites>
I took a look of all relative issues but i can not find a good answer, any ideas please?
I fixed this issue by adding the following to my phpunit.xml.dist / phpunit.xml file (inside the phpunit tag):
<php>
<env name="APP_ENV" value="test" force="true"/>
</php>
Here is my phpunit.xml file
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true"
strict="true"
bootstrap="./vendor/autoload.php">
<testsuites>
<php>
<server name="KERNEL_DIR" value="app" />
<ini name="display_errors" value="true"/>
</php>
<testsuite name="Functional Tests">
<directory>./tests/src/myApp/AppBundle/functional</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
Here is the test class
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class MyTest extends WebTestCase
{
private $client;
public function setUp()
{
$this->client = static::createClient();
}
When running the test I get at the line in setUp:
Either set KERNEL_DIR in your phpunit.xml according to
https://symfony.com/doc/current/book/testing.html#your-first-functional-test
or override the WebTestCase::createKernel() method
Move the <php> ... </php> out of the <testsuites>... block to it's own top-level.
Here's a trimmed down copy of my own main project:
<phpunit
bootstrap = "app/bootstrap_test.php"
verbose = "true"
>
<php>
<ini name="memory_limit" value="-1" />
<!-- <server name="KERNEL_DIR" value="./app/" /> older-style -->
<server name="KERNEL_CLASS" value="AppKernel" />
<server name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
</php>
<testsuites>
<testsuite name="project.name">
<directory suffix=".php">./src/*Bundle</directory>
<directory suffix=".php">./src/AppBundle</directory> -->
<directory>./tests/App</directory>
</testsuite>
</testsuites>
The Bundle https://github.com/saadtazi/SaadTaziGChartBundle (version 2.0) passes "twig.form.resources" as an argument.
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="twig.extension.g_chart.class">SaadTazi\GChartBundle\Twig\GChartExtension</parameter>
<parameter key="twig.extension.visualization.template">SaadTaziGChartBundle:chart:visualizationChart.html.twig</parameter>
</parameters>
<services>
<service id="twig.extension.g_chart" class="%twig.extension.g_chart.class%">
<tag name="twig.extension" alias="g_chart" />
<argument key="twig.form.resources" type="collection">
<argument key="gChartTemplate">%twig.extension.visualization.template%</argument>
</argument>
</service>
</services>
</container>
This configuration throws an Exception in Syfony 3.3:
Invalid key "twig.form.resources" found in arguments of method "__construct()" for service "twig.extension.g_chart": only integer or $named arguments are allowed.
The constructor has a resources property:
/**
* #param array $resources a list of resources (see Resources/g_chart.xml)
*/
public function __construct(array $resources = array()) {
$this->resources = $resources;
}
I've tried fixing it by passing "twig.form_themes", which is the replacement for "twig.form.resources", but that throws an Exception too. Any thoughts appreciated, thanks!
I have difficulties setting up a functional test in Symfony 3.2.4.
namespace Tests\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ClinicalTrialsControllerTest extends WebTestCase
{
public function testHTTPConnection()
{
$client = static::createClient();
The client is not created and I constantly get an error message:
Unable to guess the Kernel directory.
/Applications/MAMP/htdocs/Symfony/coremed/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:57
/Applications/MAMP/htdocs/Symfony/coremed/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:118
/Applications/MAMP/htdocs/Symfony/coremed/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:164
/Applications/MAMP/htdocs/Symfony/coremed/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:145
/Applications/MAMP/htdocs/Symfony/coremed/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php:33
/Applications/MAMP/htdocs/Symfony/coremed/Tests/Controller/ClinicalTrialsControllerTest.php:17
In phpunit.dist.xml the kernel directory is set to
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_DIR" value='app/' />
</php>
Any hints as to how to set the kernel directory?
I have this in my phpunit.dist.xml:
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_DIR" value="app/" />
</php>
Why don't you change it and see if that works?
It should be:
phpunit.xml.dist
Not phpunit.dist.xml.