Completed
Push — ezp26760-fix_broken_LegacyDFSC... ( 5ec717...32b468 )
by
unknown
23:06
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')],
45
            ['prefix/my/file.png', 'image/png', 123, 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 View Code Duplication
    public function testDelete()
74
    {
75
        $statement = $this->createDbalStatementMock();
76
        $statement
77
            ->expects($this->once())
78
            ->method('rowCount')
79
            ->will($this->returnValue(1));
80
81
        $this->dbalMock
82
            ->expects($this->once())
83
            ->method('prepare')
84
            ->with($this->anything())
85
            ->will($this->returnValue($statement));
86
87
        $this->handler->delete('prefix/my/file.png');
88
    }
89
90
    /**
91
     * @expectedException \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException
92
     */
93 View Code Duplication
    public function testDeleteNotFound()
94
    {
95
        $statement = $this->createDbalStatementMock();
96
        $statement
97
            ->expects($this->once())
98
            ->method('rowCount')
99
            ->will($this->returnValue(0));
100
101
        $this->dbalMock
102
            ->expects($this->once())
103
            ->method('prepare')
104
            ->with($this->anything())
105
            ->will($this->returnValue($statement));
106
107
        $this->handler->delete('prefix/my/file.png');
108
    }
109
110
    public function testLoad()
111
    {
112
        $statement = $this->createDbalStatementMock();
113
        $statement
114
            ->expects($this->once())
115
            ->method('rowCount')
116
            ->will($this->returnValue(1));
117
118
        $statement
119
            ->expects($this->once())
120
            ->method('fetch')
121
            ->will($this->returnValue(array('size' => 123, 'datatype' => 'image/png', 'mtime' => 1307155200)));
122
123
        $this->dbalMock
124
            ->expects($this->once())
125
            ->method('prepare')
126
            ->with($this->anything())
127
            ->will($this->returnValue($statement));
128
129
        $expectedSpiBinaryFile = new SPIBinaryFile();
130
        $expectedSpiBinaryFile->id = 'prefix/my/file.png';
131
        $expectedSpiBinaryFile->size = 123;
132
        $expectedSpiBinaryFile->mtime = new DateTime('@1307155200');
133
        $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...
134
135
        self::assertEquals(
136
            $expectedSpiBinaryFile,
137
            $this->handler->load('prefix/my/file.png')
138
        );
139
    }
140
141
    /**
142
     * @expectedException \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException
143
     */
144 View Code Duplication
    public function testLoadNotFound()
145
    {
146
        $statement = $this->createDbalStatementMock();
147
        $statement
148
            ->expects($this->once())
149
            ->method('rowCount')
150
            ->will($this->returnValue(0));
151
152
        $this->dbalMock
153
            ->expects($this->once())
154
            ->method('prepare')
155
            ->with($this->anything())
156
            ->will($this->returnValue($statement));
157
158
        $this->handler->load('prefix/my/file.png');
159
    }
160
161 View Code Duplication
    public function testExists()
162
    {
163
        $statement = $this->createDbalStatementMock();
164
        $statement
165
            ->expects($this->once())
166
            ->method('rowCount')
167
            ->will($this->returnValue(1));
168
169
        $this->dbalMock
170
            ->expects($this->once())
171
            ->method('prepare')
172
            ->with($this->anything())
173
            ->will($this->returnValue($statement));
174
175
        self::assertTrue($this->handler->exists('prefix/my/file.png'));
176
    }
177
178 View Code Duplication
    public function testExistsNot()
179
    {
180
        $statement = $this->createDbalStatementMock();
181
        $statement
182
            ->expects($this->once())
183
            ->method('rowCount')
184
            ->will($this->returnValue(0));
185
186
        $this->dbalMock
187
            ->expects($this->once())
188
            ->method('prepare')
189
            ->with($this->anything())
190
            ->will($this->returnValue($statement));
191
192
        self::assertFalse($this->handler->exists('prefix/my/file.png'));
193
    }
194
195 View Code Duplication
    public function testDeletedirectory()
196
    {
197
        $statement = $this->createDbalStatementMock();
198
        $statement
199
            ->expects($this->once())
200
            ->method('bindValue')
201
            ->with(1, 'folder/subfolder/%');
202
203
        $this->dbalMock
204
            ->expects($this->once())
205
            ->method('prepare')
206
            ->with($this->anything())
207
            ->will($this->returnValue($statement));
208
209
        $this->handler->deleteDirectory('folder/subfolder/');
210
    }
211
212
    /**
213
     * @return \PHPUnit_Framework_MockObject_MockObject
214
     */
215
    protected function createDbalStatementMock()
216
    {
217
        return $this->getMock('Doctrine\DBAL\Driver\Statement');
218
    }
219
}
220