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
|
|
|
/** |
60
|
|
|
* @group time-sensitive |
61
|
|
|
*/ |
62
|
|
|
public function testOlderCacheFileMtDate() |
63
|
|
|
{ |
64
|
|
|
$this->createDummyCachedFile(); |
65
|
|
|
|
66
|
|
|
$expectedDate = (new \DateTime())->setTimestamp(time() - 3600); |
67
|
|
|
$actualDate = $this->cacheValidator->getOldestCachefileDate(); |
68
|
|
|
|
69
|
|
|
$this->assertInstanceOf(\DateTime::class, $actualDate); |
70
|
|
|
$this->assertSame($expectedDate->getTimestamp(), $actualDate->getTimestamp()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testOlderCacheFileMtDateNoFiles() |
74
|
|
|
{ |
75
|
|
|
$actualDate = $this->cacheValidator->getOldestCachefileDate(); |
76
|
|
|
|
77
|
|
|
$this->assertNull($actualDate); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function createDummyCachedFile() |
81
|
|
|
{ |
82
|
|
|
touch(sprintf('%s/catalogue.test.php', $this->cacheDir)); |
83
|
|
|
touch(sprintf('%s/catalogue2.test.php', $this->cacheDir), time() - 3600); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function deleteDummyCachedFile() |
87
|
|
|
{ |
88
|
|
|
$files = [ |
89
|
|
|
sprintf('%s/catalogue.test.php', $this->cacheDir), |
90
|
|
|
sprintf('%s/catalogue2.test.php', $this->cacheDir), |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
foreach ($files as $file) { |
94
|
|
|
if (file_exists($file)) { |
95
|
|
|
unlink($file); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|