Skip to content

Commit

Permalink
Cover caching with tests
Browse files Browse the repository at this point in the history
- Addition of caching tests.
- Support for nested directory structures in the test directory.
  • Loading branch information
alexander-nitsche committed Apr 20, 2023
1 parent 7835658 commit 2e6ddc6
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 42 deletions.
30 changes: 20 additions & 10 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,38 @@ protected function getTestDirectory()
protected function removeTestDirectory()
{
if (is_dir($this->getTestDirectory())) {
$files = glob($this->getTestDirectory().'/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->getTestDirectory(), \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $path) {
if ($path->isDir() && !$path->isLink()) {
rmdir($path->getPathname());
} else {
unlink($path->getPathname());
}
}
rmdir($this->getTestDirectory());
}
}

protected function addFileToTestDirectory($filename, $content)
protected function createDirectoryInTestDirectory($path)
{
file_put_contents($this->getPathOfTestDirectoryFile($filename), $content);
mkdir($this->getPathInTestDirectory($path), 0777, true);
}

protected function getPathOfTestDirectoryFile($filename)
protected function createFileInTestDirectory($path, $content)
{
return $this->getTestDirectory().'/'.$filename;
file_put_contents($this->getPathInTestDirectory($path), $content);
}

protected function getPathOfTestDirectoryFileAsUrl($filename)
protected function getPathInTestDirectory($path)
{
return 'file://'.$this->getPathOfTestDirectoryFile($filename);
return $this->getTestDirectory().'/'.$path;
}

protected function getPathInTestDirectoryAsUrl($path)
{
return 'file://'.$this->getPathInTestDirectory($path);
}
}
8 changes: 4 additions & 4 deletions tests/build/DocumentationManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public function delegateSucceeds()
-------
');

$this->addFileToTestDirectory('README.md', $readme);
$this->createFileInTestDirectory('README.md', $readme);

$readmeFile = $this->getPathOfTestDirectoryFile('README.md');
$readmeFile = $this->getPathInTestDirectory('README.md');
$apiClass = ReflectionFixture::class;

$this->documentationManager->setReadmeFile($readmeFile);
Expand Down Expand Up @@ -121,9 +121,9 @@ public function delegateFailsIfReadmeFormatChanges()
')."\n";
$expectedReadme = $readme;

$this->addFileToTestDirectory('README.md', $readme);
$this->createFileInTestDirectory('README.md', $readme);

$readmeFile = $this->getPathOfTestDirectoryFile('README.md');
$readmeFile = $this->getPathInTestDirectory('README.md');

$this->documentationManager->setReadmeFile($readmeFile);
$this->documentationManager->setApiClass(ReflectionFixture::class);
Expand Down
24 changes: 12 additions & 12 deletions tests/build/DownloadManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ protected function tearDownFixture()
*/
public function delegateSucceeds()
{
$this->addFileToTestDirectory('hyph-af.tex', 'original');
$this->addFileToTestDirectory('remote-hyph-af.tex', 'original');
$this->addFileToTestDirectory('hyph-as.tex', 'original');
$this->addFileToTestDirectory('remote-hyph-as.tex', 'changed');
$this->addFileToTestDirectory('configuration.json', json_encode([
$this->createFileInTestDirectory('hyph-af.tex', 'original');
$this->createFileInTestDirectory('remote-hyph-af.tex', 'original');
$this->createFileInTestDirectory('hyph-as.tex', 'original');
$this->createFileInTestDirectory('remote-hyph-as.tex', 'changed');
$this->createFileInTestDirectory('configuration.json', json_encode([
'files' => [
[
'fromUrl' => $this->getPathOfTestDirectoryFileAsUrl('remote-hyph-af.tex'),
'toPath' => $this->getPathOfTestDirectoryFile('hyph-af.tex'),
'fromUrl' => $this->getPathInTestDirectoryAsUrl('remote-hyph-af.tex'),
'toPath' => $this->getPathInTestDirectory('hyph-af.tex'),
],
[
'fromUrl' => $this->getPathOfTestDirectoryFileAsUrl('remote-hyph-as.tex'),
'toPath' => $this->getPathOfTestDirectoryFile('hyph-as.tex'),
'fromUrl' => $this->getPathInTestDirectoryAsUrl('remote-hyph-as.tex'),
'toPath' => $this->getPathInTestDirectory('hyph-as.tex'),
],
[
'fromUrl' => $this->getPathOfTestDirectoryFileAsUrl('remote-not-available.tex'),
'toPath' => $this->getPathOfTestDirectoryFile('hyph-bg.tex'),
'fromUrl' => $this->getPathInTestDirectoryAsUrl('remote-not-available.tex'),
'toPath' => $this->getPathInTestDirectory('hyph-bg.tex'),
],
],
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

$expectedOutputRegex = '#Result: 3/3 files processed, 1 changed, 1 unchanged and 1 failed.#';

$configurationFile = $this->getPathOfTestDirectoryFile('configuration.json');
$configurationFile = $this->getPathInTestDirectory('configuration.json');

$this->downloadManager->setConfigurationFile($configurationFile);
$result = $this->downloadManager->delegate();
Expand Down
8 changes: 4 additions & 4 deletions tests/build/ReleaseManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public function delegateSucceeds()
- Fixed PHP 7.4 compatibility (#37) by @Dargmuesli.
');

$this->addFileToTestDirectory('README.md', $readme);
$this->createFileInTestDirectory('README.md', $readme);

$releaseType = SemanticVersioning::PATCH_RELEASE;
$readmeFile = $this->getPathOfTestDirectoryFile('README.md');
$readmeFile = $this->getPathInTestDirectory('README.md');

$this->releaseManager->setReleaseType($releaseType);
$this->releaseManager->setReadmeFile($readmeFile);
Expand Down Expand Up @@ -146,10 +146,10 @@ public function delegateFailsIfReadmeFormatChanges()
')."\n";
$expectedReadme = $readme;

$this->addFileToTestDirectory('README.md', $readme);
$this->createFileInTestDirectory('README.md', $readme);

$releaseType = SemanticVersioning::PATCH_RELEASE;
$readmeFile = $this->getPathOfTestDirectoryFile('README.md');
$readmeFile = $this->getPathInTestDirectory('README.md');

$this->releaseManager->setReleaseType($releaseType);
$this->releaseManager->setReadmeFile($readmeFile);
Expand Down
76 changes: 64 additions & 12 deletions tests/src/SyllableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Vanderlee\SyllableTest\Src;

use Vanderlee\Syllable\Cache\Json;
use Vanderlee\Syllable\Cache\Serialized;
use Vanderlee\Syllable\Hyphen\Text;
use Vanderlee\Syllable\Syllable;
use Vanderlee\SyllableTest\AbstractTestCase;
Expand Down Expand Up @@ -104,25 +106,75 @@ public function testGetHyphen()
}

/**
* @todo Implement testSetCache().
* @return array[]
*/
public function testSetCache()
public function dataCache()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
return [
[
Json::class,
'en-us',
'json',
'json/syllable.en-us.json',
'{'.
'"version":1.4,'.
'"patterns":{%a},'.
'"max_pattern":9,'.
'"hyphenation":{%a},'.
'"left_min_hyphen":2,'.
'"right_min_hyphen":2'.
'}',
],
[
Serialized::class,
'en-us',
'serialized',
'serialized/syllable.en-us.serialized',
'a:6:{'.
's:7:"version";d:1.4;'.
's:8:"patterns";a:4939:{%a}'.
's:11:"max_pattern";i:9;'.
's:11:"hyphenation";a:15:{%a}'.
's:15:"left_min_hyphen";i:2;'.
's:16:"right_min_hyphen";i:2;'.
'}',
],
];
}

/**
* @todo Implement testGetCache().
* @dataProvider dataCache
*
* @return void
*/
public function testGetCache()
public function testCache($cacheClass, $language, $cacheDirectory, $cacheFile, $expectedCacheContent)
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->createDirectoryInTestDirectory($cacheDirectory);

$cacheDirectory = $this->getPathInTestDirectory($cacheDirectory);
$cacheFile = $this->getPathInTestDirectory($cacheFile);

$this->object->setLanguage($language);
$this->object->setCache(new $cacheClass($cacheDirectory));

$this->assertFileNotExists($cacheFile);
$this->assertFalse($this->object->getCache()->__isset('version'));
$this->assertFalse($this->object->getCache()->__isset('patterns'));
$this->assertFalse($this->object->getCache()->__isset('max_pattern'));
$this->assertFalse($this->object->getCache()->__isset('hyphenation'));
$this->assertFalse($this->object->getCache()->__isset('left_min_hyphen'));
$this->assertFalse($this->object->getCache()->__isset('right_min_hyphen'));

$this->object->hyphenateWord('Supercalifragilisticexpialidocious');

$this->assertFileExists($cacheFile);
$this->assertStringMatchesFormat($expectedCacheContent, file_get_contents($cacheFile));
$this->assertSame(1.4, $this->object->getCache()->__get('version'));
$this->assertNotEmpty($this->object->getCache()->__get('patterns'));
$this->assertGreaterThan(0, $this->object->getCache()->__get('max_pattern'));
$this->assertNotEmpty($this->object->getCache()->__get('hyphenation'));
$this->assertInternalType('int', $this->object->getCache()->__get('left_min_hyphen'));
$this->assertInternalType('int', $this->object->getCache()->__get('right_min_hyphen'));
}

/**
Expand Down

0 comments on commit 2e6ddc6

Please sign in to comment.