php-solution / sf-functional-test
Extra functionality for work with Symfony functional tests
Installs: 68 490
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 10
Open Issues: 1
Requires
- php: >=8.1
- ext-json: *
- doctrine/data-fixtures: >=1.2
- justinrainbow/json-schema: >=5.2
- nelmio/alice: >=3.0
- phpspec/prophecy-phpunit: >=2.0
- phpunit/phpunit: >=10.0
- symfony/browser-kit: >=5.0
- symfony/console: >=5.0
- symfony/framework-bundle: >=5.0
- symfony/property-access: >=5.0
- theofidry/alice-data-fixtures: >=1.0
Requires (Dev)
- doctrine/doctrine-bundle: ^2.15
- doctrine/mongodb-odm: >=2.0
- doctrine/orm: >=2.0
- ramsey/uuid: >=3.9
- symfony/dotenv: >=5.0
- symfony/uid: >=5.0
Suggests
- doctrine/doctrine-bundle: for use Doctrine on test cases
- doctrine/migrations: for use Listener which run Doctrine migrations before test suite
- symfony/dotenv: for use Env Listener which load env vars from paths
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v4.x-dev
- v4.8.2
- v4.8.1
- v4.8.0
- v4.7.3
- v4.7.2
- v4.7.1
- v4.7
- v4.6
- v4.5.2
- v4.5.1
- v4.5.0
- v4.4.5
- v4.4.4
- v4.4.3
- v4.4.2
- v4.4.1
- v4.4
- v4.3.16
- v4.3.15
- v4.3.14
- v4.3.13
- 4.3.12
- 4.3.11
- 4.3.10
- 4.3.9
- 4.3.8
- 4.3.7
- 4.3.6
- v4.3.5
- v4.3.4
- v4.3.3
- v4.3.2
- v4.3.1
- 4.3
- v4.2.9
- v4.2.8
- v4.2.7
- v4.2.6
- v4.2.5
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.1.0
- v4.0.2
- v4.0.1
- v4.0.0
- v1.x-dev
- v1.3
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2
- v1.1
- v1.0
- v0.x-dev
- v0.1.1
- v0.1
- dev-master / 1.x-dev
This package is auto-updated.
Last update: 2025-07-17 08:07:21 UTC
README
Install
$ composer require php-solution/sf-functional-test
Load environment variables from files
Add to your phpunit.xml extension and configure paths (comma separated relative file paths from your phpunit.xml configuration file):
<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunEnvLoaderExtension"> <parameter name="paths" value="../.env,.env"/> </bootstrap>
Load Doctrine fixtures before test cases
Add to your phpunit.xml extension:
<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunCommandLauncherExtension"> <parameter name="command" value="functional-test:fixtures:load"/> <!--Default is false. If true, if command's exit code > 0 then tests will fail immediately--> <parameter name="exitOnError" value="true" /> </bootstrap>
Run Doctrine migrations before test cases
Add to your phpunit.xml extension:
<bootstrap class="\PhpSolution\FunctionalTest\PhpUnit\Extension\DoctrineMigrationExtension" />
Or simply:
<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunCommandLauncherExtension"> <parameter name="command" value="doctrine:migration:migrate --no-interaction"/> <parameter name="exitOnError" value="true" /> </bootstrap>
Run sf command with parameters
Add to your phpunit.xml extension:
<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunCommandLauncherExtension"> <parameter name="command" value="doctrine:mongodb:schema:drop --collection"/> </bootstrap>
Run native command with parameters
Add to your phpunit.xml extension:
<bootstrap class="PhpSolution\FunctionalTest\PhpUnit\Extension\PreRunNativeCommandLauncherExtension"> <parameter name="command" value="bin/console doctrine:mongodb:schema:drop --collection"/> </bootstrap>
Using Test case additional functionallity PhpSolution\FunctionalTest\TestCase\AppTestCase
Using Authorization:
- Add to your config_test.yml:
security: firewalls: your_secured_category: http_basic: ~
- Use on TestCase
$client = $this->getAuthorizedClient('user_login', 'password');
Use profiler for testing
- Add to your framework.yml:
when@test: framework: profiler: collect: false
- Add ProfilerTrait to your TestCase
use PhpSolution\FunctionalTest\TestCase\ProfilerTrait;
- Use the following methods to run request with profiler:
[$response, $profiler] = self::withRequestProfiler(static function () { return self::createSystemAuthorizedTester() ->setExpectedStatusCode(Response::HTTP_OK) ->sendGet('/some/awesome/endpoint', ['foo' => 'bar']); });
By default, the following collectors are enabled: 'db', 'http_client', 'cache', 'memory'
but you can always disable or enable new collectors by passing them in the withRequestProfiler
method:
[$response, $profiler] = self::withRequestProfiler(static function () { return self::createSystemAuthorizedTester() ->setExpectedStatusCode(Response::HTTP_OK) ->sendGet('/some/awesome/endpoint', ['foo' => 'bar']); }, ['db', 'http_client']);
- Use profiler to get collectors:
self::getCollector($profiler, 'http_client');
Work with Doctrine (ORM, ODM)
- Add EntityTrait or DocumentTrait to your TestCase
$this->getDoctrine()
- Find Entity helper method:
protected function findEntity(string $entityClass, string $orderBy = 'id', array $findBy = []) protected function findDocument(string $documentClass, array $criteria = []) protected function findDocuments(string $documentClass, array $criteria = [], array $orderBy = [])
- Refresh Entity:
protected function refreshEntity($entity) protected function refreshDocument($document)
Assert doctrine queries using request profiler
- Make sure you have set up the profiler as described above.
- Add ProfilerTrait and EntityProfilerTrait to your TestCase:
use PhpSolution\FunctionalTest\TestCase\ProfilerTrait; use PhpSolution\FunctionalTest\TestCase\EntityProfilerTrait;
- Assert queries using profiler:
self::getDoctrineCollector($profiler)->getQueries(); // returns array of executed queries self::assertDoctrineQueriesCount(8, $profiler); self::assertDoctrineQueriesCountLessThanOrEqual(3, $profiler); self::assertDoctrineSelectQueriesCount(2, $profiler); self::assertDoctrineSelectQueriesCountLessThanOrEqual(2, $profiler); self::assertDoctrineUpdateQueriesCount(1, $profiler); self::assertDoctrineUpdateQueriesCountLessThanOrEqual(1, $profiler); self::assertDoctrineInsertQueriesCount(1, $profiler); self::assertDoctrineInsertQueriesCountLessThanOrEqual(1, $profiler); self::assertDoctrineDeleteQueriesCount(1, $profiler); self::assertDoctrineDeleteQueriesCountLessThanOrEqual(1, $profiler);
Test emails
- Add config
swiftmailer: disable_delivery: true spool: type: file path: '%kernel.project_dir%/var/spool' delivery_addresses: ~
- Add SpoolTrait and find methods
public function purgeSpool() public function getSpooledEmails() public function getEmailContent($file) protected function getSpoolDir()
Example of correct project structure:
See correct project structure and configs for functional tests on link