Completed
Push — ezp25946-migrate_files_to_othe... ( 56a30c...6cf922 )
by
unknown
24:31
created

StreamFileListenerTest::testRespondsToIoRequest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 31
rs 8.8571
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\Bundle\EzPublishIOBundle\Tests\EventListener;
10
11
use eZ\Bundle\EzPublishIOBundle\EventListener\StreamFileListener;
12
use eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse;
13
use eZ\Publish\Core\IO\Values\BinaryFile;
14
use PHPUnit_Framework_TestCase;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17
use Symfony\Component\HttpKernel\HttpKernelInterface;
18
use DateTime;
19
20
class StreamFileListenerTest extends PHPUnit_Framework_TestCase
21
{
22
    /** @var StreamFileListener */
23
    private $eventListener;
24
25
    /** @var \eZ\Publish\Core\IO\IOServiceInterface|\PHPUnit_Framework_MockObject_MockObject */
26
    private $ioServiceMock;
27
28
    private $ioUriPrefix = 'var/test/storage';
29
30
    /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface|\PHPUnit_Framework_MockObject_MockObject */
31
    private $configResolverMock;
32
33
    public function setUp()
34
    {
35
        $this->ioServiceMock = $this->getMock('eZ\Publish\Core\IO\IOServiceInterface');
36
37
        $this->configResolverMock = $this->getMock('eZ\Publish\Core\MVC\ConfigResolverInterface');
38
39
        $this->eventListener = new StreamFileListener($this->ioServiceMock, $this->configResolverMock);
40
    }
41
42 View Code Duplication
    public function testDoesNotRespondToNonIoUri()
43
    {
44
        $request = $this->createRequest('/Not-an-image');
45
        $event = $this->createEvent($request);
46
47
        $this->configureIoUrlPrefix('var/test/storage');
48
        $this->ioServiceMock
49
            ->expects($this->never())
50
            ->method('loadBinaryFileByUri');
51
52
        $this->eventListener->onKernelRequest($event);
53
54
        self::assertNull($event->getResponse());
55
    }
56
57 View Code Duplication
    public function testDoesNotRespondToNoIoRequest()
58
    {
59
        $request = $this->createRequest('/Not-an-image', 'bar.fr');
60
        $event = $this->createEvent($request);
61
62
        $this->configureIoUrlPrefix('http://foo.com/var/test/storage');
63
        $this->ioServiceMock
64
            ->expects($this->never())
65
            ->method('loadBinaryFileByUri');
66
67
        $this->eventListener->onKernelRequest($event);
68
69
        self::assertNull($event->getResponse());
70
    }
71
72
    public function testRespondsToIoUri()
73
    {
74
        $uri = '/var/test/storage/images/image.png';
75
        $this->configureIoUrlPrefix(ltrim($uri, '/'));
76
        $request = $this->createRequest($uri);
77
78
        $event = $this->createEvent($request);
79
80
        $binaryFile = new BinaryFile(array('mtime' => new DateTime()));
81
82
        $this->ioServiceMock
83
            ->expects($this->once())
84
            ->method('loadBinaryFileByUri')
85
            ->with($uri)
86
            ->will($this->returnValue($binaryFile));
87
88
        $this->eventListener->onKernelRequest($event);
89
90
        self::assertTrue($event->hasResponse());
91
        $expectedResponse = new BinaryStreamResponse($binaryFile, $this->ioServiceMock);
92
        $response = $event->getResponse();
93
        // since symfony/symfony v3.2.7 Response sets Date header if not explicitly set
94
        // @see https://github.com/symfony/symfony/commit/e3d90db74773406fb8fdf07f36cb8ced4d187f62
95
        $expectedResponse->setDate($response->getDate());
0 ignored issues
show
Bug introduced by
It seems like $response->getDate() can be null; however, setDate() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
96
        self::assertEquals(
97
            $expectedResponse,
98
            $response
99
        );
100
    }
101
102
    public function testRespondsToIoRequest()
103
    {
104
        $uri = '/var/test/storage/images/image.png';
105
        $host = 'phoenix-rises.fm';
106
        $urlPrefix = "http://$host/var/test/storage";
107
        $this->configureIoUrlPrefix($urlPrefix);
108
        $request = $this->createRequest($uri, $host);
109
110
        $event = $this->createEvent($request);
111
112
        $binaryFile = new BinaryFile(array('mtime' => new DateTime()));
113
114
        $this->ioServiceMock
115
            ->expects($this->once())
116
            ->method('loadBinaryFileByUri')
117
            ->with(sprintf('http://%s%s', $host, $uri))
118
            ->will($this->returnValue($binaryFile));
119
120
        $this->eventListener->onKernelRequest($event);
121
122
        self::assertTrue($event->hasResponse());
123
        $expectedResponse = new BinaryStreamResponse($binaryFile, $this->ioServiceMock);
124
        $response = $event->getResponse();
125
        // since symfony/symfony v3.2.7 Response sets Date header if not explicitly set
126
        // @see https://github.com/symfony/symfony/commit/e3d90db74773406fb8fdf07f36cb8ced4d187f62
127
        $expectedResponse->setDate($response->getDate());
0 ignored issues
show
Bug introduced by
It seems like $response->getDate() can be null; however, setDate() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
128
        self::assertEquals(
129
            $expectedResponse,
130
            $response
131
        );
132
    }
133
134
    private function configureIoUrlPrefix($urlPrefix)
135
    {
136
        $this->configResolverMock
137
            ->expects($this->any())
138
            ->method('getParameter')
139
            ->with('io.url_prefix')
140
            ->willReturn($urlPrefix);
141
    }
142
143
    /**
144
     * @return Request
145
     */
146
    protected function createRequest($semanticPath, $host = 'localhost')
147
    {
148
        $request = Request::create(sprintf('http://%s%s', $host, $semanticPath));
149
        $request->attributes->set('semanticPathinfo', $semanticPath);
150
151
        return $request;
152
    }
153
154
    /**
155
     * @param $request
156
     *
157
     * @return GetResponseEvent
158
     */
159
    protected function createEvent($request)
160
    {
161
        $event = new GetResponseEvent(
162
            $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
163
            $request,
164
            HttpKernelInterface::MASTER_REQUEST
165
        );
166
167
        return $event;
168
    }
169
}
170