Completed
Push — ezp24853-enhance_rest_when_fai... ( 1cf498...fc8bde )
by
unknown
25:05
created

testNonRestRequestVariations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
3
/**
4
 * File containing the RequestListenerTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Bundle\EzPublishRestBundle\Tests\EventListener;
12
13
use eZ\Publish\Core\REST\Server\View\AcceptHeaderVisitorDispatcher;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16
use Symfony\Component\HttpKernel\HttpKernelInterface;
17
use eZ\Bundle\EzPublishRestBundle\EventListener\RequestListener;
18
use Symfony\Component\HttpKernel\KernelEvents;
19
20
class RequestListenerTest extends EventListenerTest
21
{
22
    const REST_ROUTE = '/api/ezp/v2/rest-route';
23
    const NON_REST_ROUTE = '/non-rest-route';
24
25
    public function provideExpectedSubscribedEventTypes()
26
    {
27
        return [
28
            [
29
                [KernelEvents::REQUEST],
30
            ],
31
        ];
32
    }
33
34 View Code Duplication
    public static function restRequestUrisProvider()
35
    {
36
        return [
37
            ['/api/ezp/v2/true'],
38
            ['/api/bundle-name/v2/true'],
39
            ['/api/MyBundle12/v2/true'],
40
            ['/api/ThisIs_Bundle123/v2/true'],
41
            ['/api/my-bundle/v1/true'],
42
            ['/api/my-bundle/v2/true'],
43
            ['/api/my-bundle/v2.7/true'],
44
            ['/api/my-bundle/v122.73/true'],
45
        ];
46
    }
47
48 View Code Duplication
    public static function nonRestRequestsUrisProvider()
49
    {
50
        return [
51
            ['/ap/ezp/v2/false'],
52
            ['/api/bundle name/v2/false'],
53
            ['/api/My/Bundle/v2/false'],
54
            ['/api//v2/false'],
55
            ['/api/my-bundle/v/false'],
56
            ['/api/my-bundle/v2-2/false'],
57
            ['/api/my-bundle/v2 7/false'],
58
            ['/api/my-bundle/v/7/false'],
59
        ];
60
    }
61
62
    public function testOnKernelRequestNotMasterRequest()
63
    {
64
        $request = $this->performFakeRequest(self::REST_ROUTE, HttpKernelInterface::SUB_REQUEST);
65
66
        self::assertTrue($request->attributes->get('is_rest_request'));
67
    }
68
69
    public function testOnKernelRequestNotRestRequest()
70
    {
71
        $request = $this->performFakeRequest(self::NON_REST_ROUTE);
72
73
        self::assertFalse($request->attributes->get('is_rest_request'));
74
    }
75
76
    public function testOnKernelRequestRestRequest()
77
    {
78
        $request = $this->performFakeRequest(self::REST_ROUTE);
79
80
        self::assertTrue($request->attributes->get('is_rest_request'));
81
    }
82
83
    /**
84
     * @dataProvider restRequestUrisProvider
85
     */
86
    public function testRestRequestVariations($uri)
87
    {
88
        $request = $this->performFakeRequest($uri);
89
90
        self::assertTrue($request->attributes->get('is_rest_request'));
91
    }
92
93
    /**
94
     * @dataProvider nonRestRequestsUrisProvider
95
     */
96
    public function testNonRestRequestVariations($uri)
97
    {
98
        $request = $this->performFakeRequest($uri);
99
100
        self::assertFalse($request->attributes->get('is_rest_request'));
101
    }
102
103
    /**
104
     * @return RequestListener
105
     */
106
    protected function getEventListener()
107
    {
108
        return new RequestListener(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Bundle\Ez...sitorDispatcherMock()); (eZ\Bundle\EzPublishRestB...istener\RequestListener) is incompatible with the return type declared by the abstract method eZ\Bundle\EzPublishRestB...rTest::getEventListener of type eZ\Bundle\EzPublishRestB...ntListener\CsrfListener.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
109
            $this->getVisitorDispatcherMock()
110
        );
111
    }
112
113
    /**
114
     * @return AcceptHeaderVisitorDispatcher|\PHPUnit_Framework_MockObject_MockObject
115
     */
116
    public function getVisitorDispatcherMock()
117
    {
118
        return $this->getMock(
119
            'eZ\Publish\Core\REST\Server\View\AcceptHeaderVisitorDispatcher'
120
        );
121
    }
122
123
    /**
124
     * @return Request
125
     */
126
    protected function performFakeRequest($uri, $type = HttpKernelInterface::MASTER_REQUEST)
127
    {
128
        $event = new GetResponseEvent(
129
            $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
130
            Request::create($uri),
131
            $type
132
        );
133
134
        $this->getEventListener()->onKernelRequest($event);
135
136
        return $event->getRequest();
137
    }
138
}
139