AbstractResolveEndpointActionTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 159
Duplicated Lines 34.59 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
dl 55
loc 159
c 0
b 0
f 0
wmc 17
lcom 2
cbo 10
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
createAction() 0 1 ?
A setEndpointResolver() 0 11 2
A createContext() 0 24 4
A criteriaSetShouldHaveBindingCriteria() 11 11 2
A criteriaSetShouldHaveDescriptorTypeCriteria() 11 11 2
A criteriaSetShouldHaveServiceTypeCriteria() 11 11 2
A criteriaSetShouldHaveIndexCriteria() 11 11 2
A criteriaSetShouldHaveLocationCriteria() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace LightSaml\Tests\Action\Profile\Outbound\Message;
4
5
use LightSaml\Action\Profile\Outbound\Message\ResolveEndpointBaseAction;
6
use LightSaml\Context\Profile\ProfileContext;
7
use LightSaml\Criteria\CriteriaSet;
8
use LightSaml\Model\Metadata\Endpoint;
9
use LightSaml\Model\Metadata\EntityDescriptor;
10
use LightSaml\Model\Protocol\SamlMessage;
11
use LightSaml\Profile\Profiles;
12
use LightSaml\Resolver\Endpoint\Criteria\BindingCriteria;
13
use LightSaml\Resolver\Endpoint\Criteria\DescriptorTypeCriteria;
14
use LightSaml\Resolver\Endpoint\Criteria\IndexCriteria;
15
use LightSaml\Resolver\Endpoint\Criteria\LocationCriteria;
16
use LightSaml\Resolver\Endpoint\Criteria\ServiceTypeCriteria;
17
use LightSaml\Resolver\Endpoint\EndpointResolverInterface;
18
use LightSaml\Tests\BaseTestCase;
19
use Psr\Log\LoggerInterface;
20
21
abstract class AbstractResolveEndpointActionTest extends BaseTestCase
22
{
23
    /** @var ResolveEndpointBaseAction|\PHPUnit_Framework_MockObject_MockObject */
24
    protected $action;
25
26
    /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
27
    protected $logger;
28
29
    /** @var  EndpointResolverInterface|\PHPUnit_Framework_MockObject_MockObject */
30
    protected $endpointResolver;
31
32
    /**
33
     *
34
     */
35
    protected function setUp() : void
36
    {
37
        $this->logger = $this->getLoggerMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getLoggerMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Psr\Log\LoggerInt..._MockObject_MockObject> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        $this->endpointResolver = $this->getMockBuilder(EndpointResolverInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\L...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<LightSaml\Resolve..._MockObject_MockObject> of property $endpointResolver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
        $this->action = $this->createAction($this->logger, $this->endpointResolver);
0 ignored issues
show
Documentation introduced by
$this->logger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>.

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...
Documentation introduced by
$this->endpointResolver is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<LightSaml\Resolve...pointResolverInterface>.

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...
40
    }
41
42
    /**
43
     * @param LoggerInterface           $logger
44
     * @param EndpointResolverInterface $endpointResolver
45
     *
46
     * @return ResolveEndpointBaseAction
47
     */
48
    abstract protected function createAction(LoggerInterface $logger, EndpointResolverInterface $endpointResolver);
49
50
    /**
51
     * @param bool     $shouldBeCalled
52
     * @param callable $callback
53
     */
54
    protected function setEndpointResolver($shouldBeCalled, $callback)
55
    {
56
        if ($shouldBeCalled) {
57
            $this->endpointResolver->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<LightSaml\Resolve...pointResolverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
                ->method('resolve')
59
                ->willReturnCallback($callback);
60
        } else {
61
            $this->endpointResolver->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<LightSaml\Resolve...pointResolverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
                ->method('resolve');
63
        }
64
    }
65
66
    /**
67
     * @param string           $ownRole
68
     * @param SamlMessage      $inboundMessage
69
     * @param Endpoint         $endpoint
70
     * @param EntityDescriptor $partyEntityDescriptor
71
     * @param string           $profileId
72
     *
73
     * @return \LightSaml\Context\Profile\ProfileContext
74
     */
75
    protected function createContext(
76
        $ownRole = ProfileContext::ROLE_IDP,
77
        SamlMessage $inboundMessage = null,
78
        Endpoint $endpoint = null,
79
        EntityDescriptor $partyEntityDescriptor = null,
80
        $profileId = Profiles::SSO_IDP_RECEIVE_AUTHN_REQUEST
81
    ) {
82
        $context = $this->getProfileContext($profileId, $ownRole);
83
84
        if ($endpoint) {
85
            $context->getEndpointContext()->setEndpoint($endpoint);
86
        }
87
88
        if (null == $partyEntityDescriptor) {
89
            $partyEntityDescriptor = EntityDescriptor::load(__DIR__.'/../../../../../../../resources/sample/EntityDescriptor/idp2-ed-formatted.xml');
90
        }
91
        $context->getPartyEntityContext()->setEntityDescriptor($partyEntityDescriptor);
92
93
        if ($inboundMessage) {
94
            $context->getInboundContext()->setMessage($inboundMessage);
95
        }
96
97
        return $context;
98
    }
99
100
    /**
101
     * @param CriteriaSet $criteriaSet
102
     * @param array       $bindings
103
     */
104 View Code Duplication
    protected function criteriaSetShouldHaveBindingCriteria(CriteriaSet $criteriaSet, array $bindings)
0 ignored issues
show
Duplication introduced by
This method 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...
105
    {
106
        if (empty($bindings)) {
107
            $this->assertFalse($criteriaSet->has(BindingCriteria::class));
108
        } else {
109
            $this->assertTrue($criteriaSet->has(BindingCriteria::class));
110
            /** @var BindingCriteria $criteria */
111
            $criteria = $criteriaSet->getSingle(BindingCriteria::class);
112
            $this->assertEquals($bindings, $criteria->getAllBindings());
113
        }
114
    }
115
116
    /**
117
     * @param CriteriaSet $criteriaSet
118
     * @param string      $value
119
     */
120 View Code Duplication
    protected function criteriaSetShouldHaveDescriptorTypeCriteria(CriteriaSet $criteriaSet, $value)
0 ignored issues
show
Duplication introduced by
This method 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...
121
    {
122
        if ($value) {
123
            $this->assertTrue($criteriaSet->has(DescriptorTypeCriteria::class));
124
            /** @var DescriptorTypeCriteria $criteria */
125
            $criteria = $criteriaSet->getSingle(DescriptorTypeCriteria::class);
126
            $this->assertEquals($value, $criteria->getDescriptorType());
127
        } else {
128
            $this->assertFalse($criteriaSet->has(DescriptorTypeCriteria::class));
129
        }
130
    }
131
132
    /**
133
     * @param CriteriaSet $criteriaSet
134
     * @param string      $value
135
     */
136 View Code Duplication
    protected function criteriaSetShouldHaveServiceTypeCriteria(CriteriaSet $criteriaSet, $value)
0 ignored issues
show
Duplication introduced by
This method 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...
137
    {
138
        if ($value) {
139
            $this->assertTrue($criteriaSet->has(ServiceTypeCriteria::class));
140
            /** @var ServiceTypeCriteria $criteria */
141
            $criteria = $criteriaSet->getSingle(ServiceTypeCriteria::class);
142
            $this->assertEquals($value, $criteria->getServiceType());
143
        } else {
144
            $this->assertFalse($criteriaSet->has(ServiceTypeCriteria::class));
145
        }
146
    }
147
148
    /**
149
     * @param CriteriaSet $criteriaSet
150
     * @param string      $value
151
     */
152 View Code Duplication
    protected function criteriaSetShouldHaveIndexCriteria(CriteriaSet $criteriaSet, $value)
0 ignored issues
show
Duplication introduced by
This method 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...
153
    {
154
        if ($value) {
155
            $this->assertTrue($criteriaSet->has(IndexCriteria::class));
156
            /** @var IndexCriteria $criteria */
157
            $criteria = $criteriaSet->getSingle(IndexCriteria::class);
158
            $this->assertEquals($value, $criteria->getIndex());
159
        } else {
160
            $this->assertFalse($criteriaSet->has(IndexCriteria::class));
161
        }
162
    }
163
164
    /**
165
     * @param CriteriaSet $criteriaSet
166
     * @param string      $value
167
     */
168 View Code Duplication
    protected function criteriaSetShouldHaveLocationCriteria(CriteriaSet $criteriaSet, $value)
0 ignored issues
show
Duplication introduced by
This method 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...
169
    {
170
        if ($value) {
171
            $this->assertTrue($criteriaSet->has(LocationCriteria::class));
172
            /** @var LocationCriteria $criteria */
173
            $criteria = $criteriaSet->getSingle(LocationCriteria::class);
174
            $this->assertEquals($value, $criteria->getLocation());
175
        } else {
176
            $this->assertFalse($criteriaSet->has(LocationCriteria::class));
177
        }
178
    }
179
}
180