Completed
Pull Request — master (#6)
by nicolas
07:24
created

ConfigFileScriptCacheTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 2
A testInstance() 0 4 1
A testSetGetClear() 0 14 1
A tearDown() 0 8 2
1
<?php
2
3
namespace Dekalee\AdbackAnalytics\Tests\Unit\Driver;
4
5
use Dekalee\AdbackAnalytics\Driver\ScriptCacheInterface;
6
use Dekalee\AdbackAnalyticsBundle\Driver\ConfigFileScriptCache;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
/**
10
 * Class ConfigFileScriptCacheTest
11
 */
12
class ConfigFileScriptCacheTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var ConfigFileScriptCache
16
     */
17
    protected $cache;
18
19
    /**
20
     * @var Filesystem
21
     */
22
    protected $fs;
23
    protected $cacheDir;
24
25
    /**
26
     * Set up the test
27
     */
28
    public function setUp()
29
    {
30
        $this->fs = new Filesystem();
31
        $this->cacheDir = __DIR__ . '/cache';
32
        if ($this->fs->exists($this->cacheDir)) {
33
            $this->fs->remove($this->cacheDir);
34
        }
35
36
        $this->cache = new ConfigFileScriptCache($this->cacheDir, true);
37
    }
38
39
    /**
40
     * Test instance
41
     */
42
    public function testInstance()
43
    {
44
        $this->assertInstanceOf(ScriptCacheInterface::CLASS, $this->cache);
45
    }
46
47
    /**
48
     * Test set, get and clear
49
     */
50
    public function testSetGetClear()
51
    {
52
        $cacheFile = $this->cacheDir . '/adback/apiCache.php';
53
54
        $this->cache->setAnalyticsScript('foo');
55
        $this->assertTrue($this->fs->exists($cacheFile));
56
        $this->assertSame(json_encode(['adback_analytics_script' => 'foo']), file_get_contents($cacheFile));
57
58
        $this->assertSame('foo', $this->cache->getAnalyticsScript());
59
60
        $this->cache->clearAnalyticsData();
61
        $this->assertTrue($this->fs->exists($cacheFile));
62
        $this->assertSame(json_encode([]), file_get_contents($cacheFile));
63
    }
64
65
    /**
66
     * Tears down the fixture, for example, close a network connection.
67
     * This method is called after a test is executed.
68
     */
69
    protected function tearDown()
70
    {
71
        parent::tearDown();
72
73
        if ($this->fs->exists($this->cacheDir)) {
74
            $this->fs->remove($this->cacheDir);
75
        }
76
    }
77
}
78