Skip to content

Commit ef8e524

Browse files
Closes #3426
1 parent 1dfe63c commit ef8e524

11 files changed

+194
-520
lines changed

ChangeLog-9.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes of the PHPUnit 9.0 release series are documented in this fil
99
* Implemented [#3333](https://github.com/sebastianbergmann/phpunit/issues/3333): Remove annotation(s) for expecting exceptions
1010
* Implemented [#3334](https://github.com/sebastianbergmann/phpunit/issues/3334): Drop support for PHP 7.2
1111
* Implemented [#3339](https://github.com/sebastianbergmann/phpunit/issues/3339): Remove assertions (and helper methods) that operate on (non-public) attributes
12+
* Implemented [#3426](https://github.com/sebastianbergmann/phpunit/issues/3426): Clean up `assertContains()` and `assertNotContains()`
1213

1314
[9.0.0]: https://github.com/sebastianbergmann/phpunit/compare/8.5...master
1415

src/Framework/Assert.php

+6-101
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
use PHPUnit\Framework\Constraint\StringEndsWith;
5252
use PHPUnit\Framework\Constraint\StringMatchesFormatDescription;
5353
use PHPUnit\Framework\Constraint\StringStartsWith;
54-
use PHPUnit\Framework\Constraint\TraversableContains;
5554
use PHPUnit\Framework\Constraint\TraversableContainsEqual;
5655
use PHPUnit\Framework\Constraint\TraversableContainsIdentical;
5756
use PHPUnit\Framework\Constraint\TraversableContainsOnly;
@@ -177,51 +176,9 @@ public static function assertArrayNotHasKey($key, $array, string $message = ''):
177176
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
178177
* @throws Exception
179178
*/
180-
public static function assertContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
179+
public static function assertContains($needle, iterable $haystack, string $message = ''): void
181180
{
182-
// @codeCoverageIgnoreStart
183-
if (\is_string($haystack)) {
184-
self::createWarning('Using assertContains() with string haystacks is deprecated and will not be supported in PHPUnit 9. Refactor your test to use assertStringContainsString() or assertStringContainsStringIgnoringCase() instead.');
185-
}
186-
187-
if (!$checkForObjectIdentity) {
188-
self::createWarning('The optional $checkForObjectIdentity parameter of assertContains() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertContainsEquals() instead.');
189-
}
190-
191-
if ($checkForNonObjectIdentity) {
192-
self::createWarning('The optional $checkForNonObjectIdentity parameter of assertContains() is deprecated and will be removed in PHPUnit 9.');
193-
}
194-
195-
if ($ignoreCase) {
196-
self::createWarning('The optional $ignoreCase parameter of assertContains() is deprecated and will be removed in PHPUnit 9.');
197-
}
198-
// @codeCoverageIgnoreEnd
199-
200-
if (\is_array($haystack) ||
201-
(\is_object($haystack) && $haystack instanceof Traversable)) {
202-
$constraint = new TraversableContains(
203-
$needle,
204-
$checkForObjectIdentity,
205-
$checkForNonObjectIdentity
206-
);
207-
} elseif (\is_string($haystack)) {
208-
if (!\is_string($needle)) {
209-
throw InvalidArgumentException::create(
210-
1,
211-
'string'
212-
);
213-
}
214-
215-
$constraint = new StringContains(
216-
$needle,
217-
$ignoreCase
218-
);
219-
} else {
220-
throw InvalidArgumentException::create(
221-
2,
222-
'array, traversable or string'
223-
);
224-
}
181+
$constraint = new TraversableContainsIdentical($needle);
225182

226183
static::assertThat($haystack, $constraint, $message);
227184
}
@@ -240,55 +197,11 @@ public static function assertContainsEquals($needle, iterable $haystack, string
240197
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
241198
* @throws Exception
242199
*/
243-
public static function assertNotContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
200+
public static function assertNotContains($needle, iterable $haystack, string $message = ''): void
244201
{
245-
// @codeCoverageIgnoreStart
246-
if (\is_string($haystack)) {
247-
self::createWarning('Using assertNotContains() with string haystacks is deprecated and will not be supported in PHPUnit 9. Refactor your test to use assertStringNotContainsString() or assertStringNotContainsStringIgnoringCase() instead.');
248-
}
249-
250-
if (!$checkForObjectIdentity) {
251-
self::createWarning('The optional $checkForObjectIdentity parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertNotContainsEquals() instead.');
252-
}
253-
254-
if ($checkForNonObjectIdentity) {
255-
self::createWarning('The optional $checkForNonObjectIdentity parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9.');
256-
}
257-
258-
if ($ignoreCase) {
259-
self::createWarning('The optional $ignoreCase parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9.');
260-
}
261-
// @codeCoverageIgnoreEnd
262-
263-
if (\is_array($haystack) ||
264-
(\is_object($haystack) && $haystack instanceof Traversable)) {
265-
$constraint = new LogicalNot(
266-
new TraversableContains(
267-
$needle,
268-
$checkForObjectIdentity,
269-
$checkForNonObjectIdentity
270-
)
271-
);
272-
} elseif (\is_string($haystack)) {
273-
if (!\is_string($needle)) {
274-
throw InvalidArgumentException::create(
275-
1,
276-
'string'
277-
);
278-
}
279-
280-
$constraint = new LogicalNot(
281-
new StringContains(
282-
$needle,
283-
$ignoreCase
284-
)
285-
);
286-
} else {
287-
throw InvalidArgumentException::create(
288-
2,
289-
'array, traversable or string'
290-
);
291-
}
202+
$constraint = new LogicalNot(
203+
new TraversableContainsIdentical($needle)
204+
);
292205

293206
static::assertThat($haystack, $constraint, $message);
294207
}
@@ -2619,14 +2532,6 @@ public static function isNan(): IsNan
26192532
return new IsNan;
26202533
}
26212534

2622-
/**
2623-
* @deprecated Use containsEqual() or containsIdentical() instead
2624-
*/
2625-
public static function contains($value, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): TraversableContains
2626-
{
2627-
return new TraversableContains($value, $checkForObjectIdentity, $checkForNonObjectIdentity);
2628-
}
2629-
26302535
public static function containsEqual($value): TraversableContainsEqual
26312536
{
26322537
return new TraversableContainsEqual($value);

src/Framework/Assert/Functions.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
use PHPUnit\Framework\Constraint\StringEndsWith;
4646
use PHPUnit\Framework\Constraint\StringMatchesFormatDescription;
4747
use PHPUnit\Framework\Constraint\StringStartsWith;
48-
use PHPUnit\Framework\Constraint\TraversableContains;
4948
use PHPUnit\Framework\Constraint\TraversableContainsEqual;
5049
use PHPUnit\Framework\Constraint\TraversableContainsIdentical;
5150
use PHPUnit\Framework\Constraint\TraversableContainsOnly;
@@ -127,7 +126,7 @@ function assertArrayNotHasKey($key, $array, string $message = ''): void
127126
*
128127
* @see Assert::assertContains
129128
*/
130-
function assertContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
129+
function assertContains($needle, iterable $haystack, string $message = ''): void
131130
{
132131
Assert::assertContains(...\func_get_args());
133132
}
@@ -146,7 +145,7 @@ function assertContainsEquals($needle, iterable $haystack, string $message = '')
146145
*
147146
* @see Assert::assertNotContains
148147
*/
149-
function assertNotContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
148+
function assertNotContains($needle, iterable $haystack, string $message = ''): void
150149
{
151150
Assert::assertNotContains(...\func_get_args());
152151
}
@@ -1936,11 +1935,6 @@ function isNan(): IsNan
19361935
return Assert::isNan(...\func_get_args());
19371936
}
19381937

1939-
function contains($value, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): TraversableContains
1940-
{
1941-
return Assert::contains(...\func_get_args());
1942-
}
1943-
19441938
function containsEqual($value): TraversableContainsEqual
19451939
{
19461940
return Assert::containsEqual(...\func_get_args());

src/Framework/Constraint/TraversableContains.php

+8-62
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,20 @@
99
*/
1010
namespace PHPUnit\Framework\Constraint;
1111

12-
use SplObjectStorage;
13-
1412
/**
1513
* Constraint that asserts that the Traversable it is applied to contains
1614
* a given value.
17-
*
18-
* @deprecated Use TraversableContainsEqual or TraversableContainsIdentical instead
1915
*/
20-
final class TraversableContains extends Constraint
16+
abstract class TraversableContains extends Constraint
2117
{
22-
/**
23-
* @var bool
24-
*/
25-
private $checkForObjectIdentity;
26-
27-
/**
28-
* @var bool
29-
*/
30-
private $checkForNonObjectIdentity;
31-
3218
/**
3319
* @var mixed
3420
*/
3521
private $value;
3622

37-
public function __construct($value, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false)
23+
public function __construct($value)
3824
{
39-
$this->checkForObjectIdentity = $checkForObjectIdentity;
40-
$this->checkForNonObjectIdentity = $checkForNonObjectIdentity;
41-
$this->value = $value;
25+
$this->value = $value;
4226
}
4327

4428
/**
@@ -48,52 +32,9 @@ public function __construct($value, bool $checkForObjectIdentity = true, bool $c
4832
*/
4933
public function toString(): string
5034
{
51-
if (\is_string($this->value) && \strpos($this->value, "\n") !== false) {
52-
return 'contains "' . $this->value . '"';
53-
}
54-
5535
return 'contains ' . $this->exporter()->export($this->value);
5636
}
5737

58-
/**
59-
* Evaluates the constraint for parameter $other. Returns true if the
60-
* constraint is met, false otherwise.
61-
*
62-
* @param mixed $other value or object to evaluate
63-
*/
64-
protected function matches($other): bool
65-
{
66-
if ($other instanceof SplObjectStorage) {
67-
return $other->contains($this->value);
68-
}
69-
70-
if (\is_object($this->value)) {
71-
foreach ($other as $element) {
72-
if ($this->checkForObjectIdentity && $element === $this->value) {
73-
return true;
74-
}
75-
76-
/* @noinspection TypeUnsafeComparisonInspection */
77-
if (!$this->checkForObjectIdentity && $element == $this->value) {
78-
return true;
79-
}
80-
}
81-
} else {
82-
foreach ($other as $element) {
83-
if ($this->checkForNonObjectIdentity && $element === $this->value) {
84-
return true;
85-
}
86-
87-
/* @noinspection TypeUnsafeComparisonInspection */
88-
if (!$this->checkForNonObjectIdentity && $element == $this->value) {
89-
return true;
90-
}
91-
}
92-
}
93-
94-
return false;
95-
}
96-
9738
/**
9839
* Returns the description of the failure
9940
*
@@ -112,4 +53,9 @@ protected function failureDescription($other): string
11253
$this->toString()
11354
);
11455
}
56+
57+
protected function value()
58+
{
59+
return $this->value;
60+
}
11561
}

src/Framework/Constraint/TraversableContainsEqual.php

+3-46
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,8 @@
1515
* Constraint that asserts that the Traversable it is applied to contains
1616
* a given value (using non-strict comparison).
1717
*/
18-
final class TraversableContainsEqual extends Constraint
18+
final class TraversableContainsEqual extends TraversableContains
1919
{
20-
/**
21-
* @var mixed
22-
*/
23-
private $value;
24-
25-
public function __construct($value)
26-
{
27-
$this->value = $value;
28-
}
29-
30-
/**
31-
* Returns a string representation of the constraint.
32-
*
33-
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
34-
*/
35-
public function toString(): string
36-
{
37-
if (\is_string($this->value) && \strpos($this->value, "\n") !== false) {
38-
return 'contains "' . $this->value . '"';
39-
}
40-
41-
return 'contains ' . $this->exporter()->export($this->value);
42-
}
43-
4420
/**
4521
* Evaluates the constraint for parameter $other. Returns true if the
4622
* constraint is met, false otherwise.
@@ -50,35 +26,16 @@ public function toString(): string
5026
protected function matches($other): bool
5127
{
5228
if ($other instanceof SplObjectStorage) {
53-
return $other->contains($this->value);
29+
return $other->contains($this->value());
5430
}
5531

5632
foreach ($other as $element) {
5733
/* @noinspection TypeUnsafeComparisonInspection */
58-
if ($this->value == $element) {
34+
if ($this->value() == $element) {
5935
return true;
6036
}
6137
}
6238

6339
return false;
6440
}
65-
66-
/**
67-
* Returns the description of the failure
68-
*
69-
* The beginning of failure messages is "Failed asserting that" in most
70-
* cases. This method should return the second part of that sentence.
71-
*
72-
* @param mixed $other evaluated value or object
73-
*
74-
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
75-
*/
76-
protected function failureDescription($other): string
77-
{
78-
return \sprintf(
79-
'%s %s',
80-
\is_array($other) ? 'an array' : 'a traversable',
81-
$this->toString()
82-
);
83-
}
8441
}

0 commit comments

Comments
 (0)