Completed
Push — master ( c5bd76...a04aa9 )
by Ivannis Suárez
04:38
created

SelectorsTests   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 168
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 32
loc 168
wmc 9
lcom 1
cbo 2
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A newDefaultTestedInstance() 0 4 1
A acceptVisitorDataProvider() 0 6 1
A testSelector() 0 21 1
A testAddSelector() 16 16 1
A testAddNamespace() 16 16 1
A testApply() 0 10 1
B testAcceptSelectorVisitor() 0 29 1
A testStaticCall() 0 9 1
A staticCallDataProvider() 0 17 1

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
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Selector\Tests\Units;
11
12
use Cubiche\Core\Selector\Composite;
13
use Cubiche\Core\Selector\Count;
14
use Cubiche\Core\Selector\Custom;
15
use Cubiche\Core\Selector\Key;
16
use Cubiche\Core\Selector\Method;
17
use Cubiche\Core\Selector\Property;
18
use Cubiche\Core\Selector\SelectorFactoryInterface;
19
use Cubiche\Core\Selector\Selectors;
20
use Cubiche\Core\Selector\Value;
21
use Cubiche\Core\Visitor\VisitorInterface;
22
23
/**
24
 * Selector Builder Tests Class.
25
 *
26
 * @author Ivannis Suárez Jerez <[email protected]>
27
 */
28
class SelectorsTests extends SelectorTestCase
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function newDefaultTestedInstance()
34
    {
35
        return Selectors::key('foo')->key('bar');
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function acceptVisitorDataProvider()
42
    {
43
        return array(
44
            array($this->newMockInstance(VisitorInterface::class), 'visit', 'accept'),
45
        );
46
    }
47
48
    /**
49
     * Test apply.
50
     */
51
    public function testSelector()
52
    {
53
        $this
54
            /* @var \Cubiche\Core\Selector\Selectors $builder */
55
            ->given($builder = $this->newDefaultTestedInstance())
56
            ->then()
57
                /* @var \Cubiche\Core\Selector\Composite $selector */
58
                ->object($selector = $builder->selector())
59
                    ->isInstanceOf(Composite::class)
60
                /* @var \Cubiche\Core\Selector\Key $foo */
61
                ->object($foo = $selector->valueSelector())
0 ignored issues
show
Bug introduced by
The method valueSelector() does not exist on Cubiche\Core\Selector\SelectorInterface. Did you maybe mean select()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
                    ->isInstanceOf(Key::class)
63
                ->string($foo->name())
64
                    ->isEqualTo('foo')
65
                    /* @var \Cubiche\Core\Selector\Key $foo */
66
                ->object($bar = $selector->applySelector())
0 ignored issues
show
Bug introduced by
The method applySelector() does not exist on Cubiche\Core\Selector\SelectorInterface. Did you maybe mean select()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67
                    ->isInstanceOf(Key::class)
68
                ->string($bar->name())
69
                    ->isEqualTo('bar')
70
        ;
71
    }
72
73
    /**
74
     * Test addSelector.
75
     */
76 View Code Duplication
    public function testAddSelector()
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...
77
    {
78
        $this
79
            /* @var \Cubiche\Core\Selector\Selectors $builder */
80
            ->given($factory = $this->newMockInstance(SelectorFactoryInterface::class))
81
            ->when(function () use ($factory) {
82
                Selectors::setFactory($factory);
0 ignored issues
show
Documentation introduced by
$factory is of type object|null, but the function expects a object<Cubiche\Core\Sele...lectorFactoryInterface>.

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...
83
                Selectors::addSelector('foo', 'bar');
84
            })
85
            ->then()
86
                ->mock($factory)
87
                    ->call('addSelector')
88
                        ->withArguments('foo', 'bar')
89
                        ->once()
90
        ;
91
    }
92
93
    /**
94
     * Test addSelector.
95
     */
96 View Code Duplication
    public function testAddNamespace()
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...
97
    {
98
        $this
99
            /* @var \Cubiche\Core\Selector\Selectors $builder */
100
            ->given($factory = $this->newMockInstance(SelectorFactoryInterface::class))
101
            ->when(function () use ($factory) {
102
                Selectors::setFactory($factory);
0 ignored issues
show
Documentation introduced by
$factory is of type object|null, but the function expects a object<Cubiche\Core\Sele...lectorFactoryInterface>.

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...
103
                Selectors::addNamespace('foo');
104
            })
105
            ->then()
106
                ->mock($factory)
107
                    ->call('addNamespace')
108
                        ->withArguments('foo')
109
                        ->once()
110
        ;
111
    }
112
113
    /**
114
     * Test apply.
115
     */
116
    public function testApply()
117
    {
118
        $this
119
            /* @var \Cubiche\Core\Selector\Selectors $builder */
120
            ->given($builder = $this->newDefaultTestedInstance())
121
            ->then()
122
                ->string($builder->apply(array('foo' => array('bar' => 'baz'))))
123
                    ->isEqualTo('baz')
124
        ;
125
    }
126
127
    /**
128
     * Test acceptSelectorVisitor.
129
     */
130
    public function testAcceptSelectorVisitor()
131
    {
132
        $this
133
            ->given(
134
                $visitorMock = $this->newMockVisitorInterface(),
135
                $shouldVisitMethod = 'visitComposite'
136
            )
137
            ->calling($visitorMock)
138
                ->methods(
139
                    function ($method) use ($shouldVisitMethod) {
140
                        return $method === \strtolower($shouldVisitMethod);
141
                    }
142
                )
143
                ->return = 25
144
            ;
145
146
        $this
147
                /* @var \Cubiche\Core\Selector\Selectors $builder */
148
                ->given($builder = $this->newDefaultTestedInstance())
149
                ->when($result = $builder->acceptSelectorVisitor($visitorMock))
0 ignored issues
show
Documentation introduced by
$visitorMock is of type object|null, but the function expects a object<Cubiche\Core\Sele...lectorVisitorInterface>.

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...
150
                ->then()
151
                    ->mock($visitorMock)
152
                        ->call($shouldVisitMethod)
153
                        ->withArguments($builder->selector())
154
                    ->once()
155
                    ->integer($result)
156
                        ->isEqualTo(25)
157
            ;
158
    }
159
160
    /**
161
     * Test __staticCall.
162
     *
163
     * @dataProvider staticCallDataProvider
164
     */
165
    public function testStaticCall(Selectors $builder, $expectedClass)
166
    {
167
        $this
168
            ->given($builder, $expectedClass)
169
            ->then()
170
                ->object($builder->selector())
171
                    ->isInstanceOf($expectedClass)
172
        ;
173
    }
174
175
    /**
176
     * @return string[][]
177
     */
178
    protected function staticCallDataProvider()
179
    {
180
        return array(
181
            array(Selectors::key('foo'), Key::class),
182
            array(Selectors::property('foo'), Property::class),
183
            array(Selectors::method('foo'), Method::class),
184
            array(Selectors::custom(function () {
185
186
            }), Custom::class),
187
            array(Selectors::count(), Count::class),
188
            array(Selectors::value('foo'), Value::class),
189
            array(Selectors::composite(
190
                Selectors::value(array()),
191
                Selectors::count()
192
            ), Composite::class),
193
        );
194
    }
195
}
196