Completed
Push — 6.7 ( 1e956c...1bb2fd )
by
unknown
13:43
created

testDoesNotRespondToNoIoRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 14
loc 14
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\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
        self::assertEquals(
92
            new BinaryStreamResponse($binaryFile, $this->ioServiceMock),
93
            $event->getResponse()
94
        );
95
    }
96
97
    public function testRespondsToIoRequest()
98
    {
99
        $uri = '/var/test/storage/images/image.png';
100
        $host = 'phoenix-rises.fm';
101
        $urlPrefix = "http://$host/var/test/storage";
102
        $this->configureIoUrlPrefix($urlPrefix);
103
        $request = $this->createRequest($uri, $host);
104
105
        $event = $this->createEvent($request);
106
107
        $binaryFile = new BinaryFile(array('mtime' => new DateTime()));
108
109
        $this->ioServiceMock
110
            ->expects($this->once())
111
            ->method('loadBinaryFileByUri')
112
            ->with(sprintf('http://%s%s', $host, $uri))
113
            ->will($this->returnValue($binaryFile));
114
115
        $this->eventListener->onKernelRequest($event);
116
117
        self::assertTrue($event->hasResponse());
118
        self::assertEquals(
119
            new BinaryStreamResponse($binaryFile, $this->ioServiceMock),
120
            $event->getResponse()
121
        );
122
    }
123
124
    private function configureIoUrlPrefix($urlPrefix)
125
    {
126
        $this->configResolverMock
127
            ->expects($this->any())
128
            ->method('getParameter')
129
            ->with('io.url_prefix')
130
            ->willReturn($urlPrefix);
131
    }
132
133
    /**
134
     * @return Request
135
     */
136
    protected function createRequest($semanticPath, $host = 'localhost')
137
    {
138
        $request = Request::create(sprintf('http://%s%s', $host, $semanticPath));
139
        $request->attributes->set('semanticPathinfo', $semanticPath);
140
141
        return $request;
142
    }
143
144
    /**
145
     * @param $request
146
     *
147
     * @return GetResponseEvent
148
     */
149
    protected function createEvent($request)
150
    {
151
        $event = new GetResponseEvent(
152
            $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
153
            $request,
154
            HttpKernelInterface::MASTER_REQUEST
155
        );
156
157
        return $event;
158
    }
159
}
160