Symfony 3.4 PHPUnit testing database data

symfony new my_project 3.4
cd my_project
# if you have the phpunit phar installed globally
phpunit tests/
# if you use the phar file directly
php phpunit.phar tests/
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

E 1 / 1 (100%)

Time: 128 ms, Memory: 14.00MB

There was 1 error:

1) Tests\AppBundle\Controller\DefaultControllerTest::testIndex
Error: Call to undefined method Symfony\Component\Yaml\Parser::parseFile()
cd my_project
composer require --dev symfony/phpunit-bridge
./vendor/bin/simple-phpunit
composer require --dev doctrine/doctrine-fixtures-bundle
// app/AppKernel.php

// ...
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
// ...
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
}
// AppBundle/DataFixtures/ORM/LoadUserData.php

namespace AppBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use AppBundle\Entity\User;

class LoadUserData implements FixtureInterface, ContainerAwareInterface
{
private $data = [
'ROLE_USER' => [
'username' => 'user',
'email' => 'user@slowcode.io',
'plainPassword' => 'user@slowcode.io'
],
'ROLE_ADMIN' => [
'username' => 'admin',
'email' => 'admin@slowcode.io',
'plainPassword' => 'admin@slowcode.io'
],
];

private $container;

public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

public function load(ObjectManager $manager)
{
$userManager = $this->container->get('fos_user.user_manager');

foreach ($this->data as $role => $attrs) {
$user = $userManager->createUser();
foreach ($attrs as $attr => $val) {
$function = 'set'. ucwords($attr);
$user->$function($val);
$user->setEnabled(true);
}
$user->setRoles(['ROLE_USER', $role]);
$userManager->updateUser($user, true);
}
}
}
php bin/console doctrine:fixtures:load --append
In LoadDataFixturesDoctrineCommand.php line 95:

[InvalidArgumentException]
Could not find any fixture services to load.
# services.yml
services:
# ...
AppBundle\DataFixtures\:
resource: '../../src/AppBundle/DataFixtures'
tags: ['doctrine.fixture.orm']
# config_test.yml
# ...
# Doctrine Configuration
doctrine:
dbal:
driver: "%test_database_driver%"
host: "%test_database_host%"
port: "%test_database_port%"
dbname: "%test_database_name%"
user: "%test_database_user%"
password: "%test_database_password%"
# parameters.yml
parameters:
# ...
test_database_driver: pdo_mysql
test_database_host: localhost
test_database_port: null
test_database_name: test_db_name
test_database_user: test_db_user
test_database_password: test_db_pw
// tests/AppBundle/DataFixtures/DataFixtureTestCase.php
namespace Tests\AppBundle\DataFixtures;

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DataFixtureTestCase extends WebTestCase
{
/** @var Application $application */
protected static $application;

/** @var Client $client */
protected $client;

/** @var ContainerInterface $container */
protected $container;

/** @var EntityManager $entityManager */
protected $entityManager;

/**
* {@inheritDoc}
*/
public function setUp()
{
self::runCommand('doctrine:database:drop --force');
self::runCommand('doctrine:database:create');
self::runCommand('doctrine:schema:create');
self::runCommand('doctrine:fixtures:load --append --no-interaction');

$this->client = static::createClient();
$this->container = $this->client->getContainer();
$this->entityManager = $this->container->get('doctrine.orm.entity_manager');

parent::setUp();
}

protected static function runCommand($command)
{
$command = sprintf('%s --quiet', $command);

return self::getApplication()->run(new StringInput($command));
}

protected static function getApplication()
{
if (null === self::$application) {
$client = static::createClient();

self::$application = new Application($client->getKernel());
self::$application->setAutoExit(false);
}

return self::$application;
}

/**
* {@inheritDoc}
*/
protected function tearDown()
{
self::runCommand('doctrine:database:drop --force');

parent::tearDown();

$this->entityManager->close();
$this->entityManager = null; // avoid memory leaks
}
}
// tests/AppBundle/User/FooTest.phpclass FooTest extends DataFixtureTestCase
{
protected $fooService;

/**
* {@inheritDoc}
*/
public function setUp()
{
parent::setUp();
$this->fooService = $this->container->get('app.service.foo');
}

public function test_get_two_users()
{
$users = $this->entityManager->getRepository(User::class)->findAll();
$this->assertEquals(2, count($users));
}


}
./vendor/bin/simple-phpunit
bash test

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store