PHP code example of liorchamla / symfony-test-helpers

1. Go to this page and download the library: Download liorchamla/symfony-test-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

liorchamla / symfony-test-helpers example snippets




namespace Tests\MyCoolFeature;

use Liior\SymfonyTestHelpers\WebTestCase;

class MyCoolTest extends WebTestCase
{
    public function testItAllWorksFine(): void
    {
        $this->get('/');

        $this->assertSee('Hello World!');
    }
}



use Liior\SymfonyTestHelpers\Concerns\WithClientTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyCoolTest extends WebTestCase
{
    use WithClientTrait;

    public function testItRuns(): void
    {
        $this->get('/my/route');

        // Asserts that 'Hello World' is in the content of the response
        $this->assertSee('Hello World!');

        // Asserts that 'foobar' is not in the content of the response
        $this->assertNotSee('foobar')
    }
}



use Liior\SymfonyTestHelpers\Concerns\WithClientTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyCoolTest extends WebTestCase
{
    use WithClientTrait;

    public function testItRuns()
    {
        $this->get('/');    // equivalent to $this->client->request('GET', '/')
        $this->post('/');   // equivalent to $this->client->request('POST', '/')
        $this->put('/');    // equivalent to $this->client->request('PUT', '/')
        $this->patch('/');  // equivalent to $this->client->request('PATCH', '/')
        $this->delete('/'); // equivalent to $this->client->request('DELETE', '/')
    }
}



use Liior\SymfonyTestHelpers\Concerns\WithAuthenticationTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyCoolTest extends WebTestCase
{
    use WithAuthenticationTrait;

    public function testItRuns(): void
    {
        $client = static::createClient();

        // Authenticate with dummy username, don't need the user to exist in the database
        $this->authenticate($client, "fakeUserName", 'my_firewall_name');
        $client->request('GET', '/protected/route');

        // You can pass custom roles for your simulated user
        $this->authenticate($client, "fakeUserName", 'my_firewall_name', ['ROLE_ADMIN']);
        $client->request('GET', '/admin/foo');

        // A shortcut to authenticate as an admin
        $this->authenticateAsAdmin($client, "fakeUserName", 'my_firewall_name');
        $client->request('GET', '/admin/foo');
    }
}



use Liior\SymfonyTestHelpers\Concerns\WithAuthenticationTrait;
use Liior\SymfonyTestHelpers\Concerns\WithDatabaseTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\Entity\User;

class MyCoolTest extends WebTestCase
{
    use WithAuthenticationTrait;
    use WithDatabaseTrait;

    public function testItRuns(): void
    {
        $client = static::createClient();

        // Create a user (it must be an instance of UserInterface)
        $user = new User;
        $user->setEmail("[email protected]")
            ->setPassword("password")
            ->setRoles(['ROLE_MANAGER', 'ROLE_AUTHOR']);

        // You get this from WithDatabaseTrait
        $this->getManager()->persist($user);
        $this->getManager()->flush();

        // Then you can authenticate with this user
        $this->authenticate($client, $user, "my_firewall_name");
        $client->request('GET', '/protected/route');

        // You can also override roles :
        $this->authenticate($client, $user, "my_firewall_name", ['ROLE_MODERATOR', 'ROLE_ADMIN']);
        $client->request('GET', '/admin/foo');
    }
}



use App\Entity\Task;
use Liior\SymfonyTestHelpers\Concerns\WithDatabaseTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyCoolTest extends WebTestCase
{
    use WithDatabaseTrait;

    public function testItRuns(): void
    {
        $client = static::createClient();

        // Retrieve a repository
        $repository = $this->getRepository(Task::class);
        $tasks = $repository->findAll();

        // Retrieve a manager
        $task = new Task();
        $task->setTitle('A task');
        $manager = $this->getManager();
        $manager->persist($task);
        $manager->flush();

        // Shortcut function to create a row inside database (returns the persisted entity)
        $task = $this->createOne(Task::class, function(Task $entity) {
            $entity
                ->setTitle("A task")
                ->setDescription("A description")
                ->setDueDate(new \DateTime())
            ;
        });

        // Shortcut function to create several rows inside database (returns an array of persisted entities)
        $tasks = $this->createMany(Task::class, function(Task $entity, int $index) {
            $entity
                ->setTitle("Task n°$index")
                ->setDescription("Description for task $index")
                ->setDueDate(new \DateTime('+'.$index.' days'))
            ;
        });

        // Experimental! An assertion function which looks up data in a table
        // With a simple string
        $this->assertDatabaseHas('A title', Task::class);

        // With an array of data
        $this->assertDatabaseHas([
            'title' => 'A title',
            'description' => 'A description'
        ], Task::class);

        // With a customized query
        $this->assertDatabaseHas('James', Task::class, function(\Doctrine\ORM\QueryBuilder $qb) {
            $qb->addSelect('a.name')
                ->innerJoin($root. '.author', 'a');
        });

        // Inversed logic : asserts that database is missing 'foo' in Task entity
        $this->assertDatabaseNotHas('foo', Task::class);
    }
}



use Faker\Factory;
use Liior\Faker\PricesProvider;
use Liior\SymfonyTestHelpers\Concerns\WithFakerTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyCoolTest extends WebTestCase
{
    use WithFakerTrait;

    // You can choose your locale (default is fr_FR 👍)
    protected $fakerLocale = 'en_GB';

    public function testItRuns()
    {
        // You can use all faker's methods!
        $sentence = $this->fake()->sentence;
        $paragraph = $this->fake()->paragraph;

        // You can also tweak the way Faker's generator is created
        // and set providers as you want.
        $this->initializeFaker(function(){
            $faker = Factory::create('fr_FR');
            $faker->addProvider(new PricesProvider($faker));

            return $faker;
        });
    }
}