Completed
Push — 6.7 ( 730fb7...a124d5 )
by André
39:46 queued 26:19
created

LegacyDFSClusterTest::testDeletedirectory()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 33
nc 1
nop 0
dl 0
loc 43
rs 8.8571
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Legacy package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributd with this source code.
8
 */
9
namespace eZ\Publish\Core\IO\Tests\IOMetadataHandler;
10
11
use Doctrine\DBAL\Query\QueryBuilder;
12
use eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster;
13
use eZ\Publish\SPI\IO\BinaryFile as SPIBinaryFile;
14
use eZ\Publish\SPI\IO\BinaryFileCreateStruct as SPIBinaryFileCreateStruct;
15
use PHPUnit\Framework\TestCase;
16
use DateTime;
17
18
class LegacyDFSClusterTest extends TestCase
19
{
20
    /** @var \eZ\Publish\Core\IO\IOMetadataHandler|\PHPUnit_Framework_MockObject_MockObject */
21
    private $handler;
22
23
    /** @var \Doctrine\DBAL\Connection|\PHPUnit_Framework_MockObject_MockObject */
24
    private $dbalMock;
25
26
    /** @var \eZ\Publish\Core\IO\UrlDecorator|\PHPUnit_Framework_MockObject_MockObject */
27
    private $urlDecoratorMock;
28
29
    public function setUp()
30
    {
31
        $this->dbalMock = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock();
32
        $this->urlDecoratorMock = $this->getMock('eZ\Publish\Core\IO\UrlDecorator');
33
34
        $this->handler = new LegacyDFSCluster(
35
            $this->dbalMock,
36
            $this->urlDecoratorMock,
37
            ['prefix' => 'var/test']
38
        );
39
    }
40
41
    public function providerCreate()
42
    {
43
        return [
44
            ['prefix/my/file.png', 'image/png', 123, new DateTime('@1307155200'), new DateTime('@1307155200')],
45
            ['prefix/my/file.png', 'image/png', 123, new DateTime('@1307155200'), new DateTime('@1307155200')], // Duplicate, should not fail
46
            ['prefix/my/file.png', 'image/png', 123, new DateTime('@1307155242'), new DateTime('@1307155242')],
47
        ];
48
    }
49
50
    /**
51
     * @dataProvider providerCreate
52
     */
53
    public function testCreate($id, $mimeType, $size, $mtime, $mtimeExpected)
54
    {
55
        $this->dbalMock
56
            ->expects($this->once())
57
            ->method('prepare')
58
            ->with($this->anything())
59
            ->will($this->returnValue($this->createDbalStatementMock()));
60
61
        $spiCreateStruct = new SPIBinaryFileCreateStruct();
62
        $spiCreateStruct->id = $id;
63
        $spiCreateStruct->mimeType = $mimeType;
64
        $spiCreateStruct->size = $size;
65
        $spiCreateStruct->mtime = $mtime;
66
67
        $spiBinary = $this->handler->create($spiCreateStruct);
68
69
        $this->assertInstanceOf('eZ\Publish\SPI\IO\BinaryFile', $spiBinary);
70
71
        $this->assertEquals($mtimeExpected, $spiBinary->mtime);
72
    }
73
74
    /**
75
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
76
     */
77
    public function testCreateInvalidArgument()
78
    {
79
        $this->dbalMock
80
            ->expects($this->never())
81
            ->method('prepare');
82
83
        $spiCreateStruct = new SPIBinaryFileCreateStruct();
84
        $spiCreateStruct->id = 'prefix/my/file.png';
85
        $spiCreateStruct->mimeType = 'image/png';
86
        $spiCreateStruct->size = 123;
87
        $spiCreateStruct->mtime = 1307155242; // Invalid, should be a DateTime
0 ignored issues
show
Documentation Bug introduced by
It seems like 1307155242 of type integer is incompatible with the declared type object<DateTime> of property $mtime.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
88
89
        $this->handler->create($spiCreateStruct);
90
    }
91
92 View Code Duplication
    public function testDelete()
93
    {
94
        $statement = $this->createDbalStatementMock();
95
        $statement
96
            ->expects($this->once())
97
            ->method('rowCount')
98
            ->will($this->returnValue(1));
99
100
        $this->dbalMock
101
            ->expects($this->once())
102
            ->method('prepare')
103
            ->with($this->anything())
104
            ->will($this->returnValue($statement));
105
106
        $this->handler->delete('prefix/my/file.png');
107
    }
108
109
    /**
110
     * @expectedException \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException
111
     */
112 View Code Duplication
    public function testDeleteNotFound()
113
    {
114
        $statement = $this->createDbalStatementMock();
115
        $statement
116
            ->expects($this->once())
117
            ->method('rowCount')
118
            ->will($this->returnValue(0));
119
120
        $this->dbalMock
121
            ->expects($this->once())
122
            ->method('prepare')
123
            ->with($this->anything())
124
            ->will($this->returnValue($statement));
125
126
        $this->handler->delete('prefix/my/file.png');
127
    }
128
129
    public function testLoad()
130
    {
131
        $statement = $this->createDbalStatementMock();
132
        $statement
133
            ->expects($this->once())
134
            ->method('rowCount')
135
            ->will($this->returnValue(1));
136
137
        $statement
138
            ->expects($this->once())
139
            ->method('fetch')
140
            ->will($this->returnValue(array('size' => 123, 'datatype' => 'image/png', 'mtime' => 1307155200)));
141
142
        $this->dbalMock
143
            ->expects($this->once())
144
            ->method('prepare')
145
            ->with($this->anything())
146
            ->will($this->returnValue($statement));
147
148
        $expectedSpiBinaryFile = new SPIBinaryFile();
149
        $expectedSpiBinaryFile->id = 'prefix/my/file.png';
150
        $expectedSpiBinaryFile->size = 123;
151
        $expectedSpiBinaryFile->mtime = new DateTime('@1307155200');
152
        $expectedSpiBinaryFile->mimeType = 'image/png';
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\SPI\IO\BinaryFile::$mimeType has been deprecated with message: Since 5.3.3, use IO\Handler::getMimeType()

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
153
154
        self::assertEquals(
155
            $expectedSpiBinaryFile,
156
            $this->handler->load('prefix/my/file.png')
157
        );
158
    }
159
160
    /**
161
     * @expectedException \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException
162
     */
163 View Code Duplication
    public function testLoadNotFound()
164
    {
165
        $statement = $this->createDbalStatementMock();
166
        $statement
167
            ->expects($this->once())
168
            ->method('rowCount')
169
            ->will($this->returnValue(0));
170
171
        $this->dbalMock
172
            ->expects($this->once())
173
            ->method('prepare')
174
            ->with($this->anything())
175
            ->will($this->returnValue($statement));
176
177
        $this->handler->load('prefix/my/file.png');
178
    }
179
180 View Code Duplication
    public function testExists()
181
    {
182
        $statement = $this->createDbalStatementMock();
183
        $statement
184
            ->expects($this->once())
185
            ->method('rowCount')
186
            ->will($this->returnValue(1));
187
188
        $this->dbalMock
189
            ->expects($this->once())
190
            ->method('prepare')
191
            ->with($this->anything())
192
            ->will($this->returnValue($statement));
193
194
        self::assertTrue($this->handler->exists('prefix/my/file.png'));
195
    }
196
197 View Code Duplication
    public function testExistsNot()
198
    {
199
        $statement = $this->createDbalStatementMock();
200
        $statement
201
            ->expects($this->once())
202
            ->method('rowCount')
203
            ->will($this->returnValue(0));
204
205
        $this->dbalMock
206
            ->expects($this->once())
207
            ->method('prepare')
208
            ->with($this->anything())
209
            ->will($this->returnValue($statement));
210
211
        self::assertFalse($this->handler->exists('prefix/my/file.png'));
212
    }
213
214
    public function testDeletedirectory()
215
    {
216
        $this->urlDecoratorMock
217
            ->expects($this->once())
218
            ->method('decorate')
219
            ->will($this->returnValue('prefix/images/_alias/subfolder'));
220
221
        $queryBuilderMock = $this
222
            ->getMockBuilder(QueryBuilder::class)
223
            ->disableOriginalConstructor()
224
            ->getMock();
225
226
        $queryBuilderMock->expects($this->at(0))
227
            ->method('delete')
228
            ->with('ezdfsfile')
229
            ->willReturn($queryBuilderMock);
230
231
        $queryBuilderMock->expects($this->at(1))
232
            ->method('where')
233
            ->with('name LIKE :spiPath ESCAPE :esc')
234
            ->willReturn($queryBuilderMock);
235
236
        $queryBuilderMock->expects($this->at(2))
237
            ->method('setParameter')
238
            ->with(':esc', '\\')
239
            ->willReturn($queryBuilderMock);
240
241
        $queryBuilderMock->expects($this->at(3))
242
            ->method('setParameter')
243
            ->with(':spiPath', 'prefix/images/\_alias/subfolder/%')
244
            ->willReturn($queryBuilderMock);
245
246
        $queryBuilderMock->expects($this->once())
247
            ->method('execute')
248
            ->willReturn(1);
249
250
        $this->dbalMock
251
            ->expects($this->once())
252
            ->method('createQueryBuilder')
253
            ->willReturn($queryBuilderMock);
254
255
        $this->handler->deleteDirectory('images/_alias/subfolder/');
256
    }
257
258
    /**
259
     * @return \PHPUnit_Framework_MockObject_MockObject
260
     */
261
    protected function createDbalStatementMock()
262
    {
263
        return $this->getMock('Doctrine\DBAL\Driver\Statement');
264
    }
265
}
266