Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a )
by
unknown
36:29
created

RestUserRoleAssignmentTest::testVisit()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 27

Duplication

Lines 46
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 46
loc 46
rs 8.9411
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing a test 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\Publish\Core\REST\Server\Tests\Output\ValueObjectVisitor;
12
13
use eZ\Publish\Core\REST\Common\Tests\Output\ValueObjectVisitorBaseTest;
14
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
15
use eZ\Publish\Core\Repository\Values\User;
16
use eZ\Publish\Core\REST\Server\Values;
17
use eZ\Publish\Core\REST\Server\Values\ResourceRouteReference;
18
19 View Code Duplication
class RestUserRoleAssignmentTest extends ValueObjectVisitorBaseTest
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    /**
22
     * Test the RestUserRoleAssignment visitor.
23
     *
24
     * @return string
25
     */
26
    public function testVisit()
27
    {
28
        $visitor = $this->getVisitor();
29
        $generator = $this->getGenerator();
30
31
        $generator->startDocument(null);
32
33
        $userRoleAssignment = new Values\RestUserRoleAssignment(
34
            new User\UserRoleAssignment(
35
                array(
36
                    'role' => new User\Role(
37
                        array(
38
                            'id' => 42,
39
                            'identifier' => 'some-role',
40
                        )
41
                    ),
42
                )
43
            ),
44
            14
45
        );
46
47
        $this->addRouteExpectation(
48
            'ezpublish_rest_loadRoleAssignmentForUser',
49
            array(
50
                'userId' => $userRoleAssignment->id,
51
                'roleId' => $userRoleAssignment->roleAssignment->role->id,
52
            ),
53
            "/user/users/{$userRoleAssignment->id}/roles/{$userRoleAssignment->roleAssignment->role->id}"
54
        );
55
56
        $this->setVisitValueObjectExpectations([
57
            new ResourceRouteReference('ezpublish_rest_loadRole', ['roleId' => $userRoleAssignment->roleAssignment->role->id]),
0 ignored issues
show
Documentation introduced by
'ezpublish_rest_loadRole' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
        ]);
59
60
        $visitor->visit(
61
            $this->getVisitorMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getVisitorMock() targeting eZ\Publish\Core\REST\Com...eTest::getVisitorMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\REST\Com...eObjectVisitor::visit() does only seem to accept object<eZ\Publish\Core\R...\Common\Output\Visitor>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
62
            $generator,
63
            $userRoleAssignment
64
        );
65
66
        $result = $generator->endDocument(null);
67
68
        $this->assertNotNull($result);
69
70
        return $result;
71
    }
72
73
    /**
74
     * Test if result contains RoleAssignment element.
75
     *
76
     * @param string $result
77
     *
78
     * @depends testVisit
79
     */
80
    public function testResultContainsRoleAssignmentElement($result)
81
    {
82
        $this->assertXMLTag(
83
            array(
84
                'tag' => 'RoleAssignment',
85
                'children' => array(
86
                    'count' => 1,
87
                ),
88
            ),
89
            $result,
90
            'Invalid <RoleAssignment> element.',
91
            false
92
        );
93
    }
94
95
    /**
96
     * Test if result contains RoleAssignment element attributes.
97
     *
98
     * @param string $result
99
     *
100
     * @depends testVisit
101
     */
102
    public function testResultContainsRoleAssignmentAttributes($result)
103
    {
104
        $this->assertXMLTag(
105
            array(
106
                'tag' => 'RoleAssignment',
107
                'attributes' => array(
108
                    'media-type' => 'application/vnd.ez.api.RoleAssignment+xml',
109
                ),
110
            ),
111
            $result,
112
            'Invalid <RoleAssignment> attributes.',
113
            false
114
        );
115
    }
116
117
    /**
118
     * Test if result contains Role element.
119
     *
120
     * @param string $result
121
     *
122
     * @depends testVisit
123
     */
124
    public function testResultContainsRoleElement($result)
125
    {
126
        $this->assertXMLTag(
127
            array(
128
                'tag' => 'Role',
129
            ),
130
            $result,
131
            'Invalid <Role> element.',
132
            false
133
        );
134
    }
135
136
    /**
137
     * Test if result contains Role element attributes.
138
     *
139
     * @param string $result
140
     *
141
     * @depends testVisit
142
     */
143
    public function testResultContainsRoleAttributes($result)
144
    {
145
        $this->assertXMLTag(
146
            array(
147
                'tag' => 'Role',
148
                'attributes' => array(
149
                    'media-type' => 'application/vnd.ez.api.Role+xml',
150
                ),
151
            ),
152
            $result,
153
            'Invalid <Role> attributes.',
154
            false
155
        );
156
    }
157
158
    /**
159
     * Get the UserRoleAssignment visitor.
160
     *
161
     * @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\RestUserRoleAssignment
162
     */
163
    protected function internalGetVisitor()
164
    {
165
        return new ValueObjectVisitor\RestUserRoleAssignment();
166
    }
167
}
168