File: FileCacheTest.php

package info (click to toggle)
php-doctrine-cache 1.3.1-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 332 kB
  • ctags: 477
  • sloc: php: 1,572; xml: 127; makefile: 13; sh: 5
file content (107 lines) | stat: -rw-r--r-- 3,156 bytes parent folder | download | duplicates (3)
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
<?php

namespace Doctrine\Tests\Common\Cache;

/**
 * @group DCOM-101
 */
class FileCacheTest extends \Doctrine\Tests\DoctrineTestCase
{
    /**
     * @var \Doctrine\Common\Cache\FileCache
     */
    private $driver;

    protected function setUp()
    {
        $this->driver = $this->getMock(
            'Doctrine\Common\Cache\FileCache',
            array('doFetch', 'doContains', 'doSave'),
            array(), '', false
        );
    }

    public function getProviderFileName()
    {
         return array(
            //The characters :\/<>"*?| are not valid in Windows filenames.
            array('key:1', 'key1'),
            array('key\2', 'key2'),
            array('key/3', 'key3'),
            array('key<4', 'key4'),
            array('key>5', 'key5'),
            array('key"6', 'key6'),
            array('key*7', 'key7'),
            array('key?8', 'key8'),
            array('key|9', 'key9'),
            array('key[0]','key[0]'),
        );
    }

    /**
     * @dataProvider getProviderFileName
     */
    public function testInvalidFilename($key, $expected)
    {
        $cache  = $this->driver;
        $method = new \ReflectionMethod($cache, 'getFilename');

        $method->setAccessible(true);

        $value  = $method->invoke($cache, $key);
        $actual = pathinfo($value, PATHINFO_FILENAME);

        $this->assertEquals($expected, $actual);
    }

    public function testFilenameCollision()
    {
        $data['key:0']  = 'key0';
        $data['key\0']  = 'key0';
        $data['key/0']  = 'key0';
        $data['key<0']  = 'key0';
        $data['key>0']  = 'key0';
        $data['key"0']  = 'key0';
        $data['key*0']  = 'key0';
        $data['key?0']  = 'key0';
        $data['key|0']  = 'key0';

        $paths  = array();
        $cache  = $this->driver;
        $method = new \ReflectionMethod($cache, 'getFilename');

        $method->setAccessible(true);

        foreach ($data as $key => $expected) {
            $path   = $method->invoke($cache, $key);
            $actual = pathinfo($path, PATHINFO_FILENAME);

            $this->assertNotContains($path, $paths);
            $this->assertEquals($expected, $actual);

            $paths[] = $path;
        }
    }

    public function testFilenameShouldCreateThePathWithFourSubDirectories()
    {
        $cache          = $this->driver;
        $method         = new \ReflectionMethod($cache, 'getFilename');
        $key            = 'item-key';
        $expectedDir[]  = '84e0e2e893febb73';
        $expectedDir[]  = '7a0fee0c89d53f4b';
        $expectedDir[]  = 'b7fcb44c57cdf3d3';
        $expectedDir[]  = '2ce7363f5d597760';
        $expectedDir    = implode(DIRECTORY_SEPARATOR, $expectedDir);

        $method->setAccessible(true);

        $path       = $method->invoke($cache, $key);
        $filename   = pathinfo($path, PATHINFO_FILENAME);
        $dirname    = pathinfo($path, PATHINFO_DIRNAME);

        $this->assertEquals('item-key', $filename);
        $this->assertEquals(DIRECTORY_SEPARATOR . $expectedDir, $dirname);
        $this->assertEquals(DIRECTORY_SEPARATOR . $expectedDir . DIRECTORY_SEPARATOR . $key, $path);
    }
}