Completed
Push — master ( d27a4c...d0eef7 )
by Sergii
01:51
created

FileCollectionTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 172
c 1
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testAdd() 0 17 1
B testFilter() 0 24 2
A testIndex() 0 23 1
A testBadFilter() 0 9 2
A testBadFilterWithObject() 0 9 2
A testGetByUid() 0 11 1
A testCorruptedIndex() 0 19 2
B fileProvider() 0 27 1
1
<?php
2
namespace Tests\AppBundle\Sync\Entity;
3
4
use AppBundle\Sync\Entity\File;
5
use AppBundle\Sync\Entity\FileCollection;
6
use AppBundle\Sync\Entity\Filter\FilterInterface;
7
use Tests\AppBundle\Mock\FileCollection as FileCollectionMock;
8
9
/**
10
 * File Collection tests
11
 *
12
 * @author Sergey Sadovoi <[email protected]>
13
 */
14
class FileCollectionTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @dataProvider fileProvider
18
     */
19
    public function testAdd($files)
20
    {
21
        // Create collection
22
        $fc = new FileCollection();
23
        $this->assertCount(0, $fc);
24
25
        // Add some files
26
        $fc->addFile($files[0]);
27
        $this->assertCount(1, $fc);
28
29
        $fc->addFile($files[1]);
30
        $this->assertCount(2, $fc);
31
32
        // Igrore adding the same file
33
        $fc->addFile($files[0]);
34
        $this->assertCount(2, $fc);
35
    }
36
37
    /**
38
     * @dataProvider fileProvider
39
     */
40
    public function testFilter($files)
41
    {
42
        $filter1 = $this->getMockBuilder(FilterInterface::class)
43
            ->setMethods(['valid'])
44
            ->getMock();
45
        $filter1
46
            ->expects($this->exactly(count($files)))
47
            ->method('valid')
48
            ->willReturn(true);
49
        $filter2 = $this->getMockBuilder(FilterInterface::class)
50
            ->setMethods(['valid'])
51
            ->getMock();
52
        $filter2
53
            ->expects($this->exactly(count($files)))
54
            ->method('valid')
55
            ->willReturn(true);
56
57
        $fc = new FileCollection();
58
        foreach ($files as $file) {
59
            $fc->addFile($file);
60
        }
61
62
        $fc->filter([$filter1, $filter2]);
63
    }
64
65
    /**
66
     * @dataProvider fileProvider
67
     */
68
    public function testIndex($files)
69
    {
70
        // Create collection
71
        $fc = new FileCollection();
72
73
        // Add some files
74
        $fc->addFile($files[0]);
75
        $fc->addFile($files[1]);
76
        $fc->addFile($files[0]);
77
        $fc->addFile($files[2]);
78
        $fc->del($files[0]->getPath());
79
80
        /**
81
         * Get from index
82
         *
83
         * @var File $file
84
         */
85
        $file = $files[2];
86
        $this->assertEquals($file, $fc->getByUid($file->getUid()));
87
88
        $fc->del($file->getPath());
89
        $this->assertNull($fc->getByUid($file->getUid()));
90
    }
91
92
    /**
93
     * @dataProvider fileProvider
94
     * @expectedException \AppBundle\Exception\FilterException
95
     */
96
    public function testBadFilter($files)
97
    {
98
        $fc = new FileCollection();
99
        foreach ($files as $file) {
100
            $fc->addFile($file);
101
        }
102
103
        $fc->filter(['bad filter']);
104
    }
105
106
    /**
107
     * @dataProvider fileProvider
108
     * @expectedException \AppBundle\Exception\FilterException
109
     */
110
    public function testBadFilterWithObject($files)
111
    {
112
        $fc = new FileCollection();
113
        foreach ($files as $file) {
114
            $fc->addFile($file);
115
        }
116
117
        $fc->filter([new \stdClass()]);
118
    }
119
120
    /**
121
     * @dataProvider fileProvider
122
     */
123
    public function testGetByUid($files)
124
    {
125
        /**
126
         * @var File $file
127
         */
128
        $file = $files[0];
129
        $fc   = new FileCollection();
130
        $fc->addFile($file);
131
132
        $this->assertEquals($file, $fc->getByUid($file->getUid()));
133
    }
134
135
    /**
136
     * @dataProvider fileProvider
137
     */
138
    public function testCorruptedIndex($files)
139
    {
140
        $fc = new FileCollectionMock();
141
142
        /**
143
         * @var File $file
144
         */
145
        foreach ($files as $file) {
146
            $fc->addFile($file);
147
        }
148
149
        $file = $files[0];
150
151
        $fc->clearIndexKeys();
152
        $this->assertNull($fc->getByUid($file->getUid()));
153
154
        $fc->clearIndex();
155
        $this->assertNull($fc->del($file->getUid()));
156
    }
157
158
    public function fileProvider()
159
    {
160
        $files = [];
161
162
        $file = new File();
163
        $file->setUid('TEST00113.mov');
164
        $file->setPath('/some/test/path/TEST00113.mov');
165
        $file->setModified(new \DateTime());
166
        $file->setSize(100);
167
        $files[] = $file;
168
169
        $file = new File();
170
        $file->setUid('TEST00313.mov');
171
        $file->setPath('/some/test2/path/TEST00313.mov');
172
        $file->setModified(new \DateTime());
173
        $file->setSize(200);
174
        $files[] = $file;
175
176
        $file = new File();
177
        $file->setUid('TEST00113.mov');
178
        $file->setPath('/some/test2/path/TEST00113.mov');
179
        $file->setModified(new \DateTime());
180
        $file->setSize(200);
181
        $files[] = $file;
182
183
        return [[$files]];
184
    }
185
}
186