php-solution/sf-functional-test

Extra functionality for work with Symfony functional tests

v5.0.2 2020-10-30 14:09 UTC

README

Install

$ composer require php-solution/sf-functional-test

Load environment variables from files

Add to your phpunit.xml listener and configure arguments(relative file paths from your phpunit.xml configuration file):

<listener class="PhpSolution\FunctionalTest\PHPUnit\Listener\EnvLoader">
    <arguments>
        <array>
            <element key="0">
                <string>../.env</string>
            </element>
            <element key="1">
                <string>.env</string>
            </element>
        </array>
    </arguments>
</listener>

Load Doctrine fixtures before test cases

Add to your phpunit.xml Listener:

<listener class="PhpSolution\FunctionalTest\PHPUnit\Listener\CommandLauncher">
    <arguments>
        <string>functional-test:fixtures:load</string>
    </arguments>
</listener>

Run Doctrine migrations before test cases

Add to your phpunit.xml Listener:

<listener class="PhpSolution\FunctionalTest\PHPUnit\Listener\CommandLauncher">
    <arguments>
        <string>doctrine:migrations:migrate</string>
    </arguments>
</listener>

Run command with parameters

Add to your phpunit.xml Listener:

<listener class="PhpSolution\FunctionalTest\PHPUnit\Listener\CommandLauncher">
    <arguments>
        <string>doctrine:mongodb:schema:drop</string>
        <string>--collection</string>
    </arguments>
</listener>

Using Test case additional functionallity PhpSolution\FunctionalTest\TestCase\AppTestCase

Using Authorization:

  1. Add to your config_test.yml:
security:
    firewalls:
        your_secured_category:
            http_basic: ~
  1. Use on TestCase
$client = $this->getAuthorizedClient('user_login', 'password');

Work with Doctrine (ORM, ODM)

  1. Add EntityTrait or DocumentTrait to your TestCase
$this->getDoctrine()
  1. 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 = [])
  1. Refresh Entity:
protected function refreshEntity($entity) 
protected function refreshDocument($document)

Test emails

  1. Add config
swiftmailer:
    disable_delivery: true
    spool:
        type: file
        path: '%kernel.project_dir%/var/spool'
    delivery_addresses: ~
  1. 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