1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
5
|
|
|
* |
6
|
|
|
* (c) Tobias Nyholm <[email protected]> |
7
|
|
|
* Kacper Gunia <[email protected]> |
8
|
|
|
* Peter Gribanov <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace tests\Happyr\DoctrineSpecification\Operand; |
15
|
|
|
|
16
|
|
|
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
17
|
|
|
use Happyr\DoctrineSpecification\Operand\Field; |
18
|
|
|
use Happyr\DoctrineSpecification\Operand\Operand; |
19
|
|
|
use Happyr\DoctrineSpecification\Operand\Value; |
20
|
|
|
use PhpSpec\ObjectBehavior; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @mixin ArgumentToOperandConverter |
24
|
|
|
*/ |
25
|
|
|
class ArgumentToOperandConverterSpec extends ObjectBehavior |
26
|
|
|
{ |
27
|
|
|
public function it_is_a_converter() |
28
|
|
|
{ |
29
|
|
|
$this->shouldBeAnInstanceOf(ArgumentToOperandConverter::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function it_not_convert_operand_to_field(Operand $operand) |
33
|
|
|
{ |
34
|
|
|
$this->toField($operand)->shouldReturn($operand); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function it_convert_argument_to_field() |
38
|
|
|
{ |
39
|
|
|
$this->toField('foo')->shouldBeAnInstanceOf(Field::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function it_not_convert_operand_to_value(Operand $operand) |
43
|
|
|
{ |
44
|
|
|
$this->toValue($operand)->shouldReturn($operand); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function it_convert_argument_to_value() |
48
|
|
|
{ |
49
|
|
|
$this->toValue('foo')->shouldBeAnInstanceOf(Value::class); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function it_is_all_arguments_a_operands(Operand $first, Operand $second) |
53
|
|
|
{ |
54
|
|
|
$arguments = [$first, $second]; |
55
|
|
|
$this->isAllOperands($arguments)->shouldReturn(true); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function it_is_not_all_arguments_a_operands(Operand $first, Operand $second) |
59
|
|
|
{ |
60
|
|
|
$arguments = [$first, 'foo', $second]; |
61
|
|
|
$this->isAllOperands($arguments)->shouldReturn(false); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function it_no_nothing_to_convert() |
65
|
|
|
{ |
66
|
|
|
$this->convert([])->shouldReturn([]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function it_a_convertible_field() |
70
|
|
|
{ |
71
|
|
|
$subject = $this->convert(['foo']); |
72
|
|
|
$subject->shouldBeArray(); |
73
|
|
|
$subject->shouldHaveCount(1); |
74
|
|
|
$subject->shouldHaveField(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function it_a_already_converted_field(Operand $field) |
78
|
|
|
{ |
79
|
|
|
$this->convert([$field])->shouldReturn([$field]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function it_a_convertible_field_and_value() |
83
|
|
|
{ |
84
|
|
|
$subject = $this->convert(['foo', 'bar']); |
85
|
|
|
$subject->shouldBeArray(); |
86
|
|
|
$subject->shouldHaveCount(2); |
87
|
|
|
$subject->shouldHaveField(); |
88
|
|
|
$subject->shouldHaveValue(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function it_a_already_converted_value(Operand $field, Operand $value) |
92
|
|
|
{ |
93
|
|
|
$this->convert([$field, $value])->shouldReturn([$field, $value]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function it_a_already_converted_value2(Operand $value) |
97
|
|
|
{ |
98
|
|
|
$subject = $this->convert(['foo', $value]); |
99
|
|
|
$subject->shouldBeArray(); |
100
|
|
|
$subject->shouldHaveCount(2); |
101
|
|
|
$subject->shouldHaveField(); |
102
|
|
|
$subject->shouldHaveOperandAt(1); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
public function it_a_convertible_arguments(Operand $first, Operand $second) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$subject = $this->convert(['foo', $first, $second, 'bar']); |
108
|
|
|
$subject->shouldBeArray(); |
109
|
|
|
$subject->shouldHaveCount(4); |
110
|
|
|
$subject->shouldHaveField(); |
111
|
|
|
$subject->shouldHaveValue(); |
112
|
|
|
$subject->shouldHaveOperandAt(1); |
113
|
|
|
$subject->shouldHaveOperandAt(2); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function it_a_convertible_arguments2(Field $field, Operand $operand, Value $value) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$subject = $this->convert([$field, $operand, 'foo', $value]); |
119
|
|
|
$subject->shouldBeArray(); |
120
|
|
|
$subject->shouldHaveCount(4); |
121
|
|
|
$subject->shouldHaveField(); |
122
|
|
|
$subject->shouldHaveValue(); |
123
|
|
|
$subject->shouldHaveOperandAt(1); |
124
|
|
|
$subject->shouldHaveValueAt(2); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getMatchers() |
128
|
|
|
{ |
129
|
|
|
return [ |
130
|
|
|
'haveField' => function ($subject) { |
131
|
|
|
return $subject[0] instanceof Field; |
132
|
|
|
}, |
133
|
|
|
'haveValue' => function ($subject) { |
134
|
|
|
return $subject[count($subject) - 1] instanceof Value; |
135
|
|
|
}, |
136
|
|
|
'haveValueAt' => function ($subject, $position) { |
137
|
|
|
return $subject[$position] instanceof Value; |
138
|
|
|
}, |
139
|
|
|
'haveOperandAt' => function ($subject, $position) { |
140
|
|
|
return $subject[$position] instanceof Operand; |
141
|
|
|
}, |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
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.