PHP code example of mekras / symfony-bundle-testing

1. Go to this page and download the library: Download mekras/symfony-bundle-testing 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/ */

    

mekras / symfony-bundle-testing example snippets


namespace Acme\MyBundle\Tests\Integration;

use Mekras\Symfony\BundleTesting\BaseSymfonyIntegrationTestCase;

abstract class IntegrationTestCase extends BaseSymfonyIntegrationTestCase
{
}

namespace Acme\MyBundle\Tests\Integration;

use Acme\MyBundle\AcmeMyBundle;
use Mekras\Symfony\BundleTesting\BaseSymfonyIntegrationTestCase;

abstract class IntegrationTestCase extends BaseSymfonyIntegrationTestCase
{
    protected function getBundleClass(): string
    {
        return MyBundle::class;
    }
}

namespace Acme\MyBundle\Tests\Integration;

final class SomeTest extends SymfonyIntegrationTestCase
{
    public function testSomething(): void
    {
        // Создаём тестовый контейнер зависимостей:
        $container = $this->createContainer();
        
        // Здесь можно настраивать контейнер зависимостей и задавать ожидания.
        
        // Компилируем контейнер, чтобы подготовить его к тестам:
        $container->compile();
        
        // Здесь можно писать утверждения.
    }
}

final class SomeTest extends SymfonyIntegrationTestCase
{
    public function testSomething(): void
    {
        $container = $this->createContainer();

        // …
    }
}

public function testSomething(): void
{
    $container = $this->createContainer();
    $container->loadExtension(new MyExtension());
    $container->compile();

    self::assertTrue($container->hasExtension('my_extension_alias'));
}

public function testSomething(): void
{
    $container = $this->createContainer();
    $container->registerBundle(MonologBundle::class);
    $container->compile();

    self::assertTrue($container->hasExtension('monolog'));
}

public function testFooServiceExists(): void
{
    $container = $this->createContainer();
    $container->expectDefinitionsExists(
      'some.service.id',
      'other.service.id',
      // …
    );
    $container->compile();
    // Если не добавить эту строку, то PHPUnit может ругаться на отсутствие утверждений.
    $this->expectNotToPerformAssertions();
}

public function testSomething(): void
{
    $container = $this->createContainer();
    $container->compile();
    
    $someService = $container->get('some.service.id');
    // Ваши проверки…
}

public function testSomething(): void
{
    $container = $this->createContainer();
    $this->addToLocator(['some.private.service.id'], $container);
    $container->compile();
    
    $someService = $this->getFromLocator('some.private.service.id', $container);
    // Ваши проверки…
}

public function testSomething(): void
{
    $container = $this->createContainer();
    $container->makePublic('some.private.service.id');
    $container->compile();
    
    $someService = $container->get('some.private.service.id');
    // Ваши проверки…
}

public function testSomething(): void
{
    $container = $this->createContainer();
    // Добавляет в контейнер MonologBundle:
    $container->registerBundle(MonologBundle::class);
    $container->compile();
    
    // Ваши проверки…
}

namespace Acme\MyBundle\Tests\Integration;

use Mekras\Symfony\BundleTesting\BaseSymfonyIntegrationTestCase;
use Symfony\Bundle\MonologBundle\MonologBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

abstract class IntegrationTestCase extends BaseSymfonyIntegrationTestCase
{
    protected function createContainer(array $public = []): ContainerBuilder
    {
        $container = parent::createContainer($public);

        $container->registerBundle(MonologBundle::class);

        return $container;
    }
}

namespace Acme\MyBundle\Tests\Integration;

use Mekras\Symfony\BundleTesting\BaseSymfonyIntegrationTestCase;
use Symfony\Bundle\MonologBundle\MonologBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

abstract class IntegrationTestCase extends BaseSymfonyIntegrationTestCase
{
    protected function getRequiredBundles(): array
    {
        return \array_merge(
            parent::getRequiredBundles(),
            [
                MonologBundle::class,
            ]
        );
    }
}

public function testSomething(): void
{
    $container = $this->createContainer();
    $container->makePublic('some.private.service.id');
    $container->compile();
    
    $someService = $container->get('some.private.service.id');
    // Ваши проверки…
}