Completed
Push — 6.7 ( bac453...2b74e9 )
by
unknown
13:37
created

LegacyDFSClusterTest::providerCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
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 eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster;
12
use eZ\Publish\SPI\IO\BinaryFile as SPIBinaryFile;
13
use eZ\Publish\SPI\IO\BinaryFileCreateStruct as SPIBinaryFileCreateStruct;
14
use PHPUnit_Framework_TestCase;
15
use DateTime;
16
17
class LegacyDFSClusterTest extends PHPUnit_Framework_TestCase
18
{
19
    /** @var \eZ\Publish\Core\IO\IOMetadataHandler|\PHPUnit_Framework_MockObject_MockObject */
20
    private $handler;
21
22
    /** @var \Doctrine\DBAL\Connection|\PHPUnit_Framework_MockObject_MockObject */
23
    private $dbalMock;
24
25
    /** @var \eZ\Publish\Core\IO\UrlDecorator|\PHPUnit_Framework_MockObject_MockObject */
26
    private $urlDecoratorMock;
27
28
    public function setUp()
29
    {
30
        $this->dbalMock = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock();
31
        $this->urlDecoratorMock = $this->getMock('eZ\Publish\Core\IO\UrlDecorator');
32
33
        $this->handler = new LegacyDFSCluster(
34
            $this->dbalMock,
35
            $this->urlDecoratorMock,
36
            ['prefix' => 'var/test']
37
        );
38
    }
39
40
    public function providerCreate()
41
    {
42
        return [
43
            ['prefix/my/file.png', 'image/png', 123, new DateTime('@1307155200'), new DateTime('@1307155200')],
44
            ['prefix/my/file.png', 'image/png', 123, new DateTime('@1307155200'), new DateTime('@1307155200')], // Duplicate, should not fail
45
            ['prefix/my/file.png', 'image/png', 123, new DateTime('@1307155242'), new DateTime('@1307155242')],
46
        ];
47
    }
48
49
    /**
50
     * @dataProvider providerCreate
51
     */
52
    public function testCreate($id, $mimeType, $size, $mtime, $mtimeExpected)
53
    {
54
        $this->dbalMock
55
            ->expects($this->once())
56
            ->method('prepare')
57
            ->with($this->anything())
58
            ->will($this->returnValue($this->createDbalStatementMock()));
59
60
        $spiCreateStruct = new SPIBinaryFileCreateStruct();
61
        $spiCreateStruct->id = $id;
62
        $spiCreateStruct->mimeType = $mimeType;
63
        $spiCreateStruct->size = $size;
64
        $spiCreateStruct->mtime = $mtime;
65
66
        $spiBinary = $this->handler->create($spiCreateStruct);
67
68
        $this->assertInstanceOf('eZ\Publish\SPI\IO\BinaryFile', $spiBinary);
69
70
        $this->assertEquals($mtimeExpected, $spiBinary->mtime);
71
    }
72
73
    /**
74
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
75
     */
76
    public function testCreateInvalidArgument()
77
    {
78
        $this->dbalMock
79
            ->expects($this->never())
80
            ->method('prepare');
81
82
        $spiCreateStruct = new SPIBinaryFileCreateStruct();
83
        $spiCreateStruct->id = 'prefix/my/file.png';
84
        $spiCreateStruct->mimeType = 'image/png';
85
        $spiCreateStruct->size = 123;
86
        $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...
87
88
        $this->handler->create($spiCreateStruct);
89
    }
90
91 View Code Duplication
    public function testDelete()
92
    {
93
        $statement = $this->createDbalStatementMock();
94
        $statement
95
            ->expects($this->once())
96
            ->method('rowCount')
97
            ->will($this->returnValue(1));
98
99
        $this->dbalMock
100
            ->expects($this->once())
101
            ->method('prepare')
102
            ->with($this->anything())
103
            ->will($this->returnValue($statement));
104
105
        $this->handler->delete('prefix/my/file.png');
106
    }
107
108
    /**
109
     * @expectedException \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException
110
     */
111 View Code Duplication
    public function testDeleteNotFound()
112
    {
113
        $statement = $this->createDbalStatementMock();
114
        $statement
115
            ->expects($this->once())
116
            ->method('rowCount')
117
            ->will($this->returnValue(0));
118
119
        $this->dbalMock
120
            ->expects($this->once())
121
            ->method('prepare')
122
            ->with($this->anything())
123
            ->will($this->returnValue($statement));
124
125
        $this->handler->delete('prefix/my/file.png');
126
    }
127
128
    public function testLoad()
129
    {
130
        $statement = $this->createDbalStatementMock();
131
        $statement
132
            ->expects($this->once())
133
            ->method('rowCount')
134
            ->will($this->returnValue(1));
135
136
        $statement
137
            ->expects($this->once())
138
            ->method('fetch')
139
            ->will($this->returnValue(array('size' => 123, 'datatype' => 'image/png', 'mtime' => 1307155200)));
140
141
        $this->dbalMock
142
            ->expects($this->once())
143
            ->method('prepare')
144
            ->with($this->anything())
145
            ->will($this->returnValue($statement));
146
147
        $expectedSpiBinaryFile = new SPIBinaryFile();
148
        $expectedSpiBinaryFile->id = 'prefix/my/file.png';
149
        $expectedSpiBinaryFile->size = 123;
150
        $expectedSpiBinaryFile->mtime = new DateTime('@1307155200');
151
        $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...
152
153
        self::assertEquals(
154
            $expectedSpiBinaryFile,
155
            $this->handler->load('prefix/my/file.png')
156
        );
157
    }
158
159
    /**
160
     * @expectedException \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException
161
     */
162 View Code Duplication
    public function testLoadNotFound()
163
    {
164
        $statement = $this->createDbalStatementMock();
165
        $statement
166
            ->expects($this->once())
167
            ->method('rowCount')
168
            ->will($this->returnValue(0));
169
170
        $this->dbalMock
171
            ->expects($this->once())
172
            ->method('prepare')
173
            ->with($this->anything())
174
            ->will($this->returnValue($statement));
175
176
        $this->handler->load('prefix/my/file.png');
177
    }
178
179 View Code Duplication
    public function testExists()
180
    {
181
        $statement = $this->createDbalStatementMock();
182
        $statement
183
            ->expects($this->once())
184
            ->method('rowCount')
185
            ->will($this->returnValue(1));
186
187
        $this->dbalMock
188
            ->expects($this->once())
189
            ->method('prepare')
190
            ->with($this->anything())
191
            ->will($this->returnValue($statement));
192
193
        self::assertTrue($this->handler->exists('prefix/my/file.png'));
194
    }
195
196 View Code Duplication
    public function testExistsNot()
197
    {
198
        $statement = $this->createDbalStatementMock();
199
        $statement
200
            ->expects($this->once())
201
            ->method('rowCount')
202
            ->will($this->returnValue(0));
203
204
        $this->dbalMock
205
            ->expects($this->once())
206
            ->method('prepare')
207
            ->with($this->anything())
208
            ->will($this->returnValue($statement));
209
210
        self::assertFalse($this->handler->exists('prefix/my/file.png'));
211
    }
212
213 View Code Duplication
    public function testDeletedirectory()
214
    {
215
        $statement = $this->createDbalStatementMock();
216
        $statement
217
            ->expects($this->once())
218
            ->method('bindValue')
219
            ->with(1, 'folder/subfolder/%');
220
221
        $this->dbalMock
222
            ->expects($this->once())
223
            ->method('prepare')
224
            ->with($this->anything())
225
            ->will($this->returnValue($statement));
226
227
        $this->handler->deleteDirectory('folder/subfolder/');
228
    }
229
230
    /**
231
     * @return \PHPUnit_Framework_MockObject_MockObject
232
     */
233
    protected function createDbalStatementMock()
234
    {
235
        return $this->getMock('Doctrine\DBAL\Driver\Statement');
236
    }
237
}
238