Functional tests in Symfony 3.2.4 using phpStorm - symfony

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.

Related

symfony 3.1 testing Bundle fails with Undefined index: kernel

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)

You cannot create the client used in functional tests if the "framework.test" config is not set to true

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>

Symfony ApiPlatform ApiTestCase not working

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

How to resolve this in PHPUnit where it is asking me to set KERNEL_DIR in my phpunit.xml?

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>

Unity Interception MethodSignatureMatchingRule could not be resolved

Im using Unity (3.0) interception to add some crosscutting concerns to my application. Somehow I can't use the MethodSignatureMatchingRule in my configuration getting this error message:
{"The type name or alias MethodSignatureMatchingRule could not be resolved. Please check your configuration file and verify this type name."}
My configuration:
<?xml version="1.0"?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
<alias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
<containers>
<container name="MyContainer">
<extension type="Interception"/>
<interception>
<policy name="EhabAspect">
<matchingRule name="MethodSignatureMatchingRule" type="MethodSignatureMatchingRule">
<constructor>
<param name="methodName" value="Receive" type="string"/>
</constructor>
</matchingRule>
<callHandler ... (omitted)
</policy>
</interception>
<register type="IMyClass" mapTo="MyClass">
<lifetime type="singleton" />
<interceptor type="InterfaceInterceptor"/>
<policyInjection/>
</register>
</container>
</containers>
</unity>
The same configuration with the NamespaceMatchingRule works fine.
My assembly contains a reference to
Microsoft.Practices.EnterpriseLibrary.Common
Microsoft.Practices.Unity
Microsoft.Practices.Unity.Configuration
Any suggestions?
I was facing the same problem here. I solved using the full qualified name of the of the class.
<matchingRule name="AuthorizationMethod" type="Microsoft.Practices.Unity.InterceptionExtension.MethodSignatureMatchingRule, Microsoft.Practices.Unity.Interception">
<constructor>
<param name="methodName" value="Authenticate" type="string"/>
<param name="parameterTypeNames" dependencyName="EmptyArray"/>
</constructor>
</matchingRule>

Resources