1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Collections\Tests\Asserters; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Collections\SetInterface; |
14
|
|
|
use Cubiche\Core\Specification\Criteria; |
15
|
|
|
use mageekguy\atoum\exceptions\logic as LogicException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* SetAsserter class. |
19
|
|
|
* |
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
class SetAsserter extends CollectionAsserter |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $assertAll; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function setWith($value, $checkType = true) |
33
|
|
|
{ |
34
|
|
|
parent::setWith($value, $checkType); |
35
|
|
|
|
36
|
|
|
if ($checkType === true) { |
37
|
|
|
if ($this->value instanceof SetInterface) { |
38
|
|
|
$this->pass(); |
39
|
|
|
} else { |
40
|
|
|
$this->fail($this->getLocale()->_('%s is not a list', $this)); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $value |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function contains($value) |
53
|
|
|
{ |
54
|
|
|
$collection = $this->valueAsCollection(); |
55
|
|
|
if ($collection->findOne(Criteria::eq($value)) !== null) { |
56
|
|
|
$this->pass(); |
57
|
|
|
} else { |
58
|
|
|
$this->fail($this->getLocale()->_('The set not contain the value %s', $value)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array|\Traversable $values |
66
|
|
|
* |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function containsValues($values) |
70
|
|
|
{ |
71
|
|
|
foreach ($values as $value) { |
72
|
|
|
$this->contains($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $value |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function notContains($value) |
84
|
|
|
{ |
85
|
|
|
$collection = $this->valueAsCollection(); |
86
|
|
|
if ($collection->findOne(Criteria::eq($value)) !== null) { |
87
|
|
|
$this->fail($this->getLocale()->_('The set contain an element with this value %s', $value)); |
88
|
|
|
} else { |
89
|
|
|
$this->pass(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @throws LogicException |
97
|
|
|
* |
98
|
|
|
* @return \Cubiche\Core\Collections\SetInterface |
99
|
|
|
*/ |
100
|
|
|
protected function valueAsCollection() |
101
|
|
|
{ |
102
|
|
|
$value = $this->valueIsSet()->getValue(); |
103
|
|
|
if ($value instanceof SetInterface) { |
104
|
|
|
return $value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new LogicException('Set is undefined'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.