1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Mado\QueryBundle\Objects\Operator; |
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @coversDefaultClass \Mado\QueryBundle\Objects\Operator |
8
|
|
|
*/ |
9
|
|
|
class OperatorTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @covers ::__construct |
13
|
|
|
* @covers ::getDefault |
14
|
|
|
* @covers ::ensureMetaIsDefined |
15
|
|
|
* @covers ::getMeta |
16
|
|
|
*/ |
17
|
|
|
public function testIsEqualByDefault() |
18
|
|
|
{ |
19
|
|
|
$op = Operator::getDefault(); |
20
|
|
|
$this->assertEquals('=', $op->getMeta()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @covers ::fromRawValue |
25
|
|
|
* @expectedException \RuntimeException |
26
|
|
|
* @expectedExceptionMessage Raw operator must contain `meta` parameter |
27
|
|
|
*/ |
28
|
|
|
public function testCreationIsInvalidWithoutMetaParameter() |
29
|
|
|
{ |
30
|
|
|
Operator::fromRawValue([ |
31
|
|
|
// invalid raw value |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @covers ::__construct |
37
|
|
|
* @covers ::fromRawValue |
38
|
|
|
* @covers ::ensureMetaIsDefined |
39
|
|
|
* @covers ::getMeta |
40
|
|
|
*/ |
41
|
|
|
public function testCreationIsValidWithMetaParameter() |
42
|
|
|
{ |
43
|
|
|
$op = Operator::fromRawValue([ |
44
|
|
|
'meta' => '=', |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
$this->assertEquals( |
48
|
|
|
'=', |
49
|
|
|
$op->getMeta() |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @covers ::fromRawValue |
55
|
|
|
* @covers ::__construct |
56
|
|
|
* @covers ::getSubstitutionPattern |
57
|
|
|
* @covers ::haveSubstitutionPattern |
58
|
|
|
* @expectedException \RuntimeException |
59
|
|
|
* @expectedExceptionMessage Oops! Current operator have not substitution pattern. |
60
|
|
|
*/ |
61
|
|
|
public function testThrowExceptionIfSubstitutionPatternIsRequsted() |
62
|
|
|
{ |
63
|
|
|
$op = Operator::fromRawValue([ |
64
|
|
|
'meta' => '=', |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$op->getSubstitutionPattern(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @covers ::fromRawValue |
72
|
|
|
* @covers ::__construct |
73
|
|
|
* @covers ::getSubstitutionPattern |
74
|
|
|
* @covers ::haveSubstitutionPattern |
75
|
|
|
*/ |
76
|
|
|
public function testProvideSubstitutionPatternIfDefinedInConstructor() |
77
|
|
|
{ |
78
|
|
|
$op = Operator::fromRawValue([ |
79
|
|
|
'meta' => '=', |
80
|
|
|
'substitution_pattern' => 'foo', |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
$this->assertEquals( |
84
|
|
|
'foo', |
85
|
|
|
$op->getSubstitutionPattern() |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @covers ::fromFilteringObject |
91
|
|
|
* @covers ::__construct |
92
|
|
|
* @covers ::getDefault |
93
|
|
|
*/ |
94
|
|
|
public function testReturnDefaultOperatorWheneverFilteringObjectHasNoOperator() |
95
|
|
|
{ |
96
|
|
|
$this->filteringObject = $this |
|
|
|
|
97
|
|
|
->getMockBuilder('Mado\QueryBundle\Objects\FilteringObject') |
98
|
|
|
->disableOriginalConstructor() |
99
|
|
|
->getMock(); |
100
|
|
|
$this->filteringObject->expects($this->once()) |
101
|
|
|
->method('hasOperator') |
102
|
|
|
->will($this->returnValue(false)); |
103
|
|
|
|
104
|
|
|
$op = Operator::fromFilteringObject($this->filteringObject); |
105
|
|
|
|
106
|
|
|
$this->assertEquals( |
107
|
|
|
$op, |
108
|
|
|
Operator::getDefault() |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @covers ::fromFilteringObject |
114
|
|
|
* @covers ::__construct |
115
|
|
|
* @covers ::getDefault |
116
|
|
|
*/ |
117
|
|
|
public function testDontReturnDefaultOperatorWheneverFilteringObjectHasOperator() |
118
|
|
|
{ |
119
|
|
|
$this->filteringObject = $this |
|
|
|
|
120
|
|
|
->getMockBuilder('Mado\QueryBundle\Objects\FilteringObject') |
121
|
|
|
->disableOriginalConstructor() |
122
|
|
|
->getMock(); |
123
|
|
|
$this->filteringObject->expects($this->once()) |
124
|
|
|
->method('hasOperator') |
125
|
|
|
->will($this->returnValue(true)); |
126
|
|
|
$this->filteringObject->expects($this->once()) |
127
|
|
|
->method('getOperator') |
128
|
|
|
->will($this->returnValue('list')); |
129
|
|
|
|
130
|
|
|
$op = Operator::fromFilteringObject($this->filteringObject); |
131
|
|
|
|
132
|
|
|
$this->assertNotEquals($op, Operator::getDefault()); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|