Completed
Push — l10n_master ( 453465...12070f )
by Jeroen
15:21 queued 08:31
created

testOlderCacheFileMtDateNoFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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