Skip to content

Commit

Permalink
Fixes #257: Check all the dependencies when checking database cache
Browse files Browse the repository at this point in the history
* Fixes #257: Check all the dependencies when checking database cache

*  README.md: Remove note about clearing cache, revert 89aa757 from #259
  • Loading branch information
alexislefebvre committed Apr 9, 2016
1 parent 9e0aa4f commit 23a229d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
7 changes: 0 additions & 7 deletions README.md
Expand Up @@ -312,9 +312,6 @@ Tips for Fixture Loading Tests
cache_sqlite_db: true
```

Note: If you use `DependentFixtureInterface` in your fixtures,
see the note in [Caveats](#caveats) about the caching of the SQLite database.

3. Load your Doctrine fixtures in your tests:

```php
Expand Down Expand Up @@ -775,10 +772,6 @@ To resolve the issue, it is recommended to configure your Doctrine slaves speci
Caveats
-------

* If you set the `liip_functional_test.cache_sqlite_db` parameter to `true`,
you have to clear the cache every time you modify a fixture
referenced by another fixture through `DependentFixtureInterface`:
`php app/console cache:clear --env=test`.
* QueryCount annotations currently only work for tests that have a method name
of `testFooBla()` (with a test prefix). The `@test` annotation isn't
supported at the moment.
Expand Down
6 changes: 5 additions & 1 deletion Test/WebTestCase.php
Expand Up @@ -318,7 +318,11 @@ protected function isBackupUpToDate(array $classNames, $backup)
$backupLastModifiedDateTime = new \DateTime();
$backupLastModifiedDateTime->setTimestamp(filemtime($backup));

foreach ($classNames as &$className) {
/** @var \Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader $loader */
$loader = $this->getFixtureLoader($this->getContainer(), $classNames);

// Use loader in order to fetch all the dependencies fixtures.
foreach ($loader->getFixtures() as $className) {
$fixtureLastModifiedDateTime = $this->getFixtureLastModified($className);
if ($backupLastModifiedDateTime < $fixtureLastModifiedDateTime) {
return false;
Expand Down
71 changes: 71 additions & 0 deletions Tests/Test/WebTestCaseConfigTest.php
Expand Up @@ -326,4 +326,75 @@ public function testLoadFixturesFilesWithHautelookCustomProvider()
$user->getName()
);
}

/**
* Update a fixture file and check that the cache will be refreshed.
*/
public function testBackupIsRefreshed()
{
// This value is generated in loadFixtures().
$md5 = '0ded9d8daaeaeca1056b18b9d0d433b2';

$fixtures = array(
'Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM\LoadDependentUserData',
);

$this->loadFixtures($fixtures);

$dependentFixtureFilePath = $this->getContainer()->get('kernel')->locateResource(
'@LiipFunctionalTestBundle/Tests/App/DataFixtures/ORM/LoadUserData.php'
);

$dependentFixtureFilemtime = filemtime($dependentFixtureFilePath);

$databaseFilePath = $this->getContainer()->getParameter('kernel.cache_dir')
.'/test_'.$md5.'.db';

if (!is_file($databaseFilePath)) {
$this->markTestSkipped($databaseFilePath.' is not a file.');
}

$databaseFilemtime = filemtime($databaseFilePath);

sleep(2);

// Reload the fixtures.
$this->loadFixtures($fixtures);

// The mtime of the file has not changed.
$this->assertSame(
$dependentFixtureFilemtime,
filemtime($dependentFixtureFilePath),
'File modification time of the fixture has been updated.'
);

// The backup has not been updated.
$this->assertSame(
$databaseFilemtime,
filemtime($databaseFilePath),
'File modification time of the backup has been updated.'
);

sleep(2);

// Update the filemtime of the fixture file used as a dependency:
// set a date in the future.
touch($dependentFixtureFilePath);

$this->loadFixtures($fixtures);

// The mtime of the fixture file has been updated.
$this->assertGreaterThan(
$dependentFixtureFilemtime,
filemtime($dependentFixtureFilePath),
'File modification time of the fixture has not been updated.'
);

// The backup has been refreshed: mtime is greater.
$this->assertGreaterThan(
$databaseFilemtime,
filemtime($databaseFilePath),
'File modification time of the backup has not been updated.'
);
}
}

0 comments on commit 23a229d

Please sign in to comment.