Skip to content

Commit

Permalink
add append fixtures feature to loadFixtures method
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Machado committed Feb 5, 2018
1 parent eca86d6 commit 3bca1f2
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This new major version introduces a number of breaking changes; see the [upgrade
* Added support for Symfony 4
* Added `.gitattributes` to make package slimmer
* Applied `declare(strict_types=1)` everywhere
* Added append fixture feature on `LoadFixtues`

### Changed
* Switched to PSR-4 dir structure with `src` and `tests` subfolders
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,26 @@ Tips for Fixture Loading Tests
}
```
6. This bundle uses Doctrine ORM by default. If you are using another driver just
6. If you want to append fixtures instead of clean database and load them, you have
to consider use the second parameter $append with value true.
```php
use Liip\FunctionalTestBundle\Test\WebTestCase;
class MyControllerTest extends WebTestCase
{
public function testIndex()
{
$this->loadFixtures(array(
'Me\MyBundle\DataFixtures\ORM\LoadAnotherObjectData',
true
));
// ...
}
}
```
7. This bundle uses Doctrine ORM by default. If you are using another driver just
specify the service id of the registry manager:
```php
Expand All @@ -393,7 +412,7 @@ Tips for Fixture Loading Tests
'Me\MyBundle\DataFixtures\MongoDB\LoadData'
);
$this->loadFixtures($fixtures, null, 'doctrine_mongodb');
$this->loadFixtures($fixtures, false, null, 'doctrine_mongodb');
$client = $this->createClient();
}
Expand Down
4 changes: 4 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ This is the list of actions that you need to take when upgrading this bundle fro
composer remove --dev nelmio/alice
composer require --dev liip/functional-test-bundle "~2.0"
```

* The interface of `LoadFixtures` had to be changed to allow append fixtures. The main difference is it had been added
a boolean second parameter. You will have to add it to `false` if you had changed the default manager, driver
or purge mode.
9 changes: 6 additions & 3 deletions src/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,14 @@ protected function isBackupUpToDate(array $classNames, string $backup): bool
* class path.
*
* @param array $classNames List of fully qualified class names of fixtures to load
* @param bool $append
* @param string $omName The name of object manager to use
* @param string $registryName The service id of manager registry to use
* @param int $purgeMode Sets the ORM purge mode
*
* @return null|AbstractExecutor
*/
protected function loadFixtures(array $classNames = [], ?string $omName = null, string $registryName = 'doctrine', ?int $purgeMode = null): ?AbstractExecutor
protected function loadFixtures(array $classNames = [], bool $append = false, ?string $omName = null, string $registryName = 'doctrine', ?int $purgeMode = null): ?AbstractExecutor
{
$container = $this->getContainer();
/** @var ManagerRegistry $registry */
Expand Down Expand Up @@ -417,7 +418,9 @@ protected function loadFixtures(array $classNames = [], ?string $omName = null,
}

$executor->setReferenceRepository($referenceRepository);
$executor->purge();
if (false === $append) {
$executor->purge();
}
}

$loader = $this->getFixtureLoader($container, $classNames);
Expand Down Expand Up @@ -456,7 +459,7 @@ private function cleanDatabase(ManagerRegistry $registry, EntityManager $om, ?st
$connection->query('SET FOREIGN_KEY_CHECKS=0');
}

$this->loadFixtures([], $omName, $registryName, $purgeMode);
$this->loadFixtures([], false, $omName, $registryName, $purgeMode);

if ($mysql) {
$connection->query('SET FOREIGN_KEY_CHECKS=1');
Expand Down
54 changes: 54 additions & 0 deletions tests/App/DataFixtures/ORM/LoadSecondUserData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/FunctionalTestBundle
*
* (c) Lukas Kahwe Smith <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Liip\FunctionalTestBundle\Tests\App\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadSecondUserData extends AbstractFixture implements FixtureInterface
{
/**
* @var ContainerInterface
*/
private $container;

/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null): void
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function load(ObjectManager $manager): void
{
/** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user */
$user = new User();
$user->setName('bar foo');
$user->setEmail('[email protected]');
$user->setPassword('12341234');
$user->setAlgorithm('plaintext');
$user->setEnabled(true);
$user->setConfirmationToken(null);

$manager->persist($user);
$manager->flush();
}
}
55 changes: 52 additions & 3 deletions tests/Test/WebTestCaseConfigMysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,55 @@ public function testLoadFixtures(): void
);
}

/**
* @group mysql
*/
public function testAppendFixtures(): void
{
$this->loadFixtures([
'Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM\LoadUserData',
]);

$this->loadFixtures(
['Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM\LoadSecondUserData'],
true
);

// Load data from database
$em = $this->getContainer()
->get('doctrine.orm.entity_manager');

/** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user */
$user = $em->getRepository('LiipFunctionalTestBundle:User')
->findOneBy([
'id' => 1,
]);

$this->assertSame(
'[email protected]',
$user->getEmail()
);

$this->assertTrue(
$user->getEnabled()
);

/** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user */
$user = $em->getRepository('LiipFunctionalTestBundle:User')
->findOneBy([
'id' => 3,
]);

$this->assertSame(
'[email protected]',
$user->getEmail()
);

$this->assertTrue(
$user->getEnabled()
);
}

/**
* Data fixtures and purge.
*
Expand Down Expand Up @@ -148,7 +197,7 @@ public function testLoadFixturesAndExcludeFromPurge(): void
);

$this->setExcludedDoctrineTables(['liip_user']);
$this->loadFixtures([], null, 'doctrine', 2);
$this->loadFixtures([], false, null, 'doctrine', 2);

// The exclusion from purge worked, the user table is still alive and well.
$this->assertSame(
Expand Down Expand Up @@ -188,7 +237,7 @@ public function testLoadFixturesAndPurge(): void
);

// 1 → ORMPurger::PURGE_MODE_DELETE
$this->loadFixtures([], null, 'doctrine', 1);
$this->loadFixtures([], false, null, 'doctrine', 1);

// The purge worked: there is no user.
$this->assertSame(
Expand All @@ -210,7 +259,7 @@ public function testLoadFixturesAndPurge(): void
);

// 2 → ORMPurger::PURGE_MODE_TRUNCATE
$this->loadFixtures([], null, 'doctrine', 2);
$this->loadFixtures([], false, null, 'doctrine', 2);

// The purge worked: there is no user.
$this->assertSame(
Expand Down
2 changes: 1 addition & 1 deletion tests/Test/WebTestCaseConfigPhpcrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testLoadFixturesPhPCr(): void
{
$fixtures = $this->loadFixtures([
'Liip\FunctionalTestBundle\Tests\AppConfigPhpcr\DataFixtures\PHPCR\LoadTaskData',
], null, 'doctrine_phpcr');
], false, null, 'doctrine_phpcr');

$this->assertInstanceOf(
'Doctrine\Bundle\PHPCRBundle\DataFixtures\PHPCRExecutor',
Expand Down

0 comments on commit 3bca1f2

Please sign in to comment.