Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileTest.php « Integration « tests « CustomPiwikJs « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4d96bd6478df5e9fa2f5b76f15f1a8b4fbe4372e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link    http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CustomPiwikJs\tests\Integration;

use Piwik\Plugins\CustomPiwikJs\File;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group CustomPiwikJs
 * @group FileTest
 * @group File
 * @group Plugins
 */
class FileTest extends IntegrationTestCase
{
    const NOT_EXISTING_FILE = 'notExisTinGFile.js';

    /**
     * @var string
     */
    private $dir = '';

    public function setUp()
    {
        parent::setUp();
        $this->dir = PIWIK_DOCUMENT_ROOT . '/plugins/CustomPiwikJs/tests/resources/';
    }

    public function tearDown()
    {
        if (file_exists($this->dir . self::NOT_EXISTING_FILE)) {
            unlink($this->dir . self::NOT_EXISTING_FILE);
        }

        parent::tearDown();
    }

    private function makeFile($fileName = 'test.js')
    {
        return new File($this->dir . $fileName);
    }

    private function makeNotReadableFile()
    {
        return $this->makeFile(self::NOT_EXISTING_FILE);
    }

    public function test_getName()
    {
        $this->assertSame('test.js', $this->makeFile()->getName());
        $this->assertSame('notExisTinGFile.js', $this->makeNotReadableFile()->getName());
    }

    public function test_hasReadAccess()
    {
        $this->assertTrue($this->makeFile()->hasReadAccess());
        $this->assertFalse($this->makeNotReadableFile()->hasReadAccess());
    }

    public function test_hasWriteAccess()
    {
        $this->assertTrue($this->makeFile()->hasWriteAccess());
        $this->assertFalse($this->makeNotReadableFile()->hasWriteAccess());
    }

    public function test_checkReadable_shouldNotThrowException_IfIsReadable()
    {
        $this->makeFile()->checkReadable();
        $this->assertTrue(true);
    }

    public function test_checkWritable_shouldNotThrowException_IfIsWritable()
    {
        $this->makeFile()->checkWritable();
        $this->assertTrue(true);
    }

    /**
     * @expectedException \Piwik\Plugins\CustomPiwikJs\Exception\AccessDeniedException
     * @expectedExceptionMessage not readable
     */
    public function test_checkReadable_shouldThrowException_IfNotIsReadable()
    {
        $this->makeNotReadableFile()->checkReadable();
    }

    /**
     * @expectedException \Piwik\Plugins\CustomPiwikJs\Exception\AccessDeniedException
     * @expectedExceptionMessage not writable
     */
    public function test_checkWritable_shouldThrowException_IfNotIsWritable()
    {
        $this->makeNotReadableFile()->checkWritable();
    }

    public function test_getContent()
    {
        $this->assertSame("// Hello world\nvar fooBar = 'test';", $this->makeFile()->getContent());
    }

    public function test_getContent_returnsNull_IfFileIsNotReadableOrNotExists()
    {
        $this->assertNull($this->makeNotReadableFile()->getContent());
    }

    public function test_save()
    {
        $notExistingFile = $this->makeNotReadableFile();
        $this->assertFalse($notExistingFile->hasReadAccess());
        $this->assertFalse($notExistingFile->hasWriteAccess());

        $notExistingFile->save('myTestContent');

        $this->assertEquals('myTestContent', $notExistingFile->getContent());
        $this->assertTrue($notExistingFile->hasReadAccess());
        $this->assertTrue($notExistingFile->hasWriteAccess());
    }

}