1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\TranslatorBundle\Tests\Service\Importer; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\TranslatorBundle\Repository\TranslationRepository; |
6
|
|
|
use Kunstmaan\TranslatorBundle\Service\Translator\CacheValidator; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class CacheValidatorTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
private $cacheValidator; |
12
|
|
|
|
13
|
|
|
private $cacheDir = __DIR__.'/../../app/cache'; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$date = new \DateTimeImmutable(); |
18
|
|
|
$yesterday = $date->modify('-1 day'); |
19
|
|
|
|
20
|
|
|
$translationRepository = $this->createMock(TranslationRepository::class); |
21
|
|
|
$translationRepository->method('getLastChangedTranslationDate')->willReturn($yesterday); |
22
|
|
|
|
23
|
|
|
$this->cacheValidator = new CacheValidator(); |
24
|
|
|
$this->cacheValidator->setTranslationRepository($translationRepository); |
25
|
|
|
|
26
|
|
|
$this->cacheDir = sprintf('%s/translations/', $this->cacheDir); |
27
|
|
|
if (!is_dir($this->cacheDir)) { |
28
|
|
|
mkdir($this->cacheDir, 0777, true); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->cacheValidator->setCacheDir($this->cacheDir); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function tearDown() |
35
|
|
|
{ |
36
|
|
|
$this->deleteDummyCachedFile(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @group cacher |
41
|
|
|
*/ |
42
|
|
|
public function testIsCacheFreshWithNoCache() |
43
|
|
|
{ |
44
|
|
|
// cache is always fresh, because there is none |
45
|
|
|
$fresh = $this->cacheValidator->isCacheFresh(); |
46
|
|
|
$this->assertTrue($fresh); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @group cacher |
51
|
|
|
*/ |
52
|
|
|
public function testIsCacheFreshWithExistingCache() |
53
|
|
|
{ |
54
|
|
|
$this->createDummyCachedFile(); |
55
|
|
|
$fresh = $this->cacheValidator->isCacheFresh(); |
56
|
|
|
$this->assertTrue($fresh); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function createDummyCachedFile() |
60
|
|
|
{ |
61
|
|
|
touch(sprintf('%s/catalogue.test.php', $this->cacheDir)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function deleteDummyCachedFile() |
65
|
|
|
{ |
66
|
|
|
$file = sprintf('%s/catalogue.test.php', $this->cacheDir); |
67
|
|
|
if (file_exists($file)) { |
68
|
|
|
unlink($file); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|