Completed
Push — v0.10 ( f42b97...309689 )
by Simonas
8s
created

EncapsulationTestAwareTrait::testSetterGetter()   C

Complexity

Conditions 10
Paths 48

Size

Total Lines 63
Code Lines 38

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 63
rs 6.3636
cc 10
eloc 38
nc 48
nop 5

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Test;
13
14
/**
15
 * A helper for testing setters/getters.
16
 *
17
 * Trait EncapsulationTestAwareTrait
18
 */
19
trait EncapsulationTestAwareTrait
20
{
21
    /**
22
     * @var string[] List of fields that should not be checked for tests.
23
     */
24
    private $ignoredFields = [];
25
26
    /**
27
     * @var mixed Stub object to test.
28
     */
29
    private $stub = null;
30
31
    /**
32
     * @return mixed
33
     */
34
    public function getStub()
35
    {
36
        return $this->stub;
37
    }
38
39
    /**
40
     * @param mixed $stub
41
     */
42
    public function setStub($stub)
43
    {
44
        $this->stub = $stub;
45
    }
46
47
    /**
48
     * Returns list of fields to test. Works as data provider.
49
     *
50
     * @return array
51
     */
52
    abstract public function getFieldsData();
53
54
    /**
55
     * Returns entity class name.
56
     *
57
     * @return string
58
     */
59
    abstract public function getClassName();
60
61
    /**
62
     * Returns list of fields that should not be checked for tests.
63
     *
64
     * @return array
65
     */
66
    protected function getIgnoredFields()
67
    {
68
        return $this->ignoredFields;
69
    }
70
71
    /**
72
     * Set list of fields that should not be checked for tests.
73
     *
74
     * @param string[] $fields
75
     */
76
    protected function setIgnoredFields(array $fields)
77
    {
78
        $this->ignoredFields = $fields;
79
    }
80
81
    /**
82
     * Set list of fields that should not be checked for tests.
83
     *
84
     * @param string $field
85
     */
86
    protected function addIgnoredField($field)
87
    {
88
        $this->ignoredFields[] = $field;
89
    }
90
91
    /**
92
     * Tests field setter and getter.
93
     *
94
     * @param string        $field
95
     * @param null|string   $type
96
     * @param null|string   $addMethod
97
     * @param null|string   $removeMethod
98
     * @param null|string[] $additionalSetter
99
     *
100
     * @dataProvider getFieldsData()
101
     */
102
    public function testSetterGetter(
103
        $field,
104
        $type = null,
105
        $addMethod = null,
106
        $removeMethod = null,
107
        $additionalSetter = null
108
    ) {
109
        /** @var \PHPUnit_Framework_TestCase|EncapsulationTestAwareTrait $this */
110
111
        $objectClass = $this->getClassName();
112
113
        $setter = 'set' . ucfirst($field);
114
        $getter = 'get' . ucfirst($field);
115
116
        if ($type === 'boolean') {
117
            $getter = 'is' . ucfirst($field);
118
        }
119
120
        $stub = $this->getStub();
121
122
        if ($stub === null) {
123
            $stub = $this->getMockForAbstractClass($objectClass);
0 ignored issues
show
Bug introduced by
It seems like getMockForAbstractClass() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
124
        }
125
126
        $this->validate($stub, $getter, $setter, $addMethod, $removeMethod, $additionalSetter);
127
128
        $expectedObject = $this->getExpectedVariable($type);
129
130
        if ($type && class_exists($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
131
            $hash = spl_object_hash($expectedObject);
132
133
            if ($addMethod) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $addMethod of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
134
                $stub->$addMethod($expectedObject);
135
136
                foreach ($stub->$getter() as $collectionObject) {
137
                    $this->assertEquals($hash, spl_object_hash($collectionObject));
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
138
                }
139
            }
140
141
            if ($removeMethod) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $removeMethod of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
142
                $stub->$removeMethod($expectedObject);
143
                $this->assertEquals(0, count($stub->$getter()));
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
144
            }
145
146
            $stub->$setter($expectedObject);
147
            $this->assertEquals($hash, spl_object_hash($stub->$getter()));
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
148
        } else {
149
            $stub->$setter($expectedObject);
150
            $this->assertEquals($expectedObject, $stub->$getter());
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
151
152
            if ($addMethod) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $addMethod of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
153
                $stub->$addMethod($this->getExpectedVariable(null));
154
                $this->assertEquals(2, count($stub->$getter()));
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
155
            }
156
        }
157
158
        if ($additionalSetter) {
159
            $stub = $this->getMockForAbstractClass($objectClass);
0 ignored issues
show
Bug introduced by
It seems like getMockForAbstractClass() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
160
            $setter = key($additionalSetter);
161
            $stub->$setter($additionalSetter[$setter][0]);
162
            $this->assertEquals($additionalSetter[$setter][1], $stub->$getter());
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
163
        }
164
    }
165
166
    /**
167
     * Tests if all entity fields are registered.
168
     */
169
    public function testAllEntityFieldsRegistered()
170
    {
171
        /** @var \PHPUnit_Framework_TestCase|EncapsulationTestAwareTrait $this */
172
173
        $reflect = new \ReflectionClass($this->getClassName());
174
        $properties = $reflect->getProperties();
175
176
        $fields = [];
177
178
        /** @var \ReflectionProperty $property */
179
        foreach ($properties as $property) {
180
            $fields[] = $property->getName();
181
        }
182
183
        $parentClass = $reflect->getParentClass();
184
        if ($parentClass) {
185
            $parentClassProperties = $parentClass->getProperties();
186
            /** @var \ReflectionProperty $property */
187
            foreach ($parentClassProperties as $property) {
188
                $this->addIgnoredField($property->getName());
189
            }
190
        }
191
192
        $traits = $reflect->getTraits();
193
        if ($traits) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $traits of type array<string,ReflectionClass> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
194
            foreach ($traits as $trait) {
195
                $traitProperties = $trait->getProperties();
196
                /** @var \ReflectionProperty $property */
197
                foreach ($traitProperties as $property) {
198
                    $this->addIgnoredField($property->getName());
199
                }
200
            }
201
        }
202
203
        $registeredFields = [];
204
205
        foreach ($this->getFieldsData() as $data) {
206
            $registeredFields[] = $data[0];
207
        }
208
209
        $diff = array_diff($fields, $registeredFields, $this->getIgnoredFields());
210
211
        if (count($diff) !== 0) {
212
            $this->fail(
0 ignored issues
show
Bug introduced by
It seems like fail() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
213
                sprintf(
214
                    'All entity fields must be registered in test. Please check field(s) "%s".',
215
                    implode('", "', $diff)
216
                )
217
            );
218
        }
219
    }
220
221
    /**
222
     * Return expected variable for compare.
223
     *
224
     * @param null|string $type
225
     *
226
     * @return object
227
     */
228
    protected function getExpectedVariable($type)
229
    {
230
        /** @var \PHPUnit_Framework_TestCase|EncapsulationTestAwareTrait $this */
231
232
        if ($type === null || $type == 'boolean') {
233
            return rand(0, 9999);
234
        } elseif ($type == 'string') {
235
            return 'string' . rand(0, 9999);
236
        } elseif ($type == 'array') {
237
            return [rand(0, 9999)];
238
        } elseif ($type == '\DateTime') {
239
            return new \DateTime();
240
        } elseif (class_exists($type) || interface_exists($type)) {
241
            return $this->getMockForAbstractClass($type);
0 ignored issues
show
Bug introduced by
It seems like getMockForAbstractClass() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
242
        }
243
244
        return null;
245
    }
246
247
    /**
248
     * Validate class before test.
249
     *
250
     * @param \PHPUnit_Framework_MockObject_MockObject $stub
251
     * @param null|string                              $getter
252
     * @param null|string                              $setter
253
     * @param null|string                              $addMethod
254
     * @param null|string                              $removeMethod
255
     * @param null|string[]                            $additionalSetter
256
     */
257
    protected function validate($stub, $getter, $setter, $addMethod, $removeMethod, $additionalSetter)
258
    {
259
        /** @var \PHPUnit_Framework_TestCase|EncapsulationTestAwareTrait $this */
260
261
        $this->assertTrue(method_exists($stub, $setter), "Method ${setter}() not found!");
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
262
        $this->assertTrue(method_exists($stub, $getter), "Method ${getter}() not found!");
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
263
264
        if ($addMethod) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $addMethod of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
265
            $this->assertTrue(method_exists($stub, $addMethod), "Method ${addMethod}() not found!");
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
266
        }
267
268
        if ($removeMethod) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $removeMethod of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
269
            $this->assertTrue(method_exists($stub, $removeMethod), "Method ${removeMethod}() not found!");
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
270
        }
271
272
        if ($additionalSetter) {
273
            $setter = key($additionalSetter);
274
            $this->assertTrue(method_exists($stub, $setter), "Method ${setter}() not found!");
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
275
        }
276
    }
277
}
278