Skip to content

Commit

Permalink
Closes #3426
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Nov 25, 2019
1 parent 1dfe63c commit ef8e524
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 520 deletions.
1 change: 1 addition & 0 deletions ChangeLog-9.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes of the PHPUnit 9.0 release series are documented in this fil
* Implemented [#3333](https://github.com/sebastianbergmann/phpunit/issues/3333): Remove annotation(s) for expecting exceptions
* Implemented [#3334](https://github.com/sebastianbergmann/phpunit/issues/3334): Drop support for PHP 7.2
* Implemented [#3339](https://github.com/sebastianbergmann/phpunit/issues/3339): Remove assertions (and helper methods) that operate on (non-public) attributes
* Implemented [#3426](https://github.com/sebastianbergmann/phpunit/issues/3426): Clean up `assertContains()` and `assertNotContains()`

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

107 changes: 6 additions & 101 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
use PHPUnit\Framework\Constraint\StringEndsWith;
use PHPUnit\Framework\Constraint\StringMatchesFormatDescription;
use PHPUnit\Framework\Constraint\StringStartsWith;
use PHPUnit\Framework\Constraint\TraversableContains;
use PHPUnit\Framework\Constraint\TraversableContainsEqual;
use PHPUnit\Framework\Constraint\TraversableContainsIdentical;
use PHPUnit\Framework\Constraint\TraversableContainsOnly;
Expand Down Expand Up @@ -177,51 +176,9 @@ public static function assertArrayNotHasKey($key, $array, string $message = ''):
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws Exception
*/
public static function assertContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
public static function assertContains($needle, iterable $haystack, string $message = ''): void
{
// @codeCoverageIgnoreStart
if (\is_string($haystack)) {
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.');
}

if (!$checkForObjectIdentity) {
self::createWarning('The optional $checkForObjectIdentity parameter of assertContains() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertContainsEquals() instead.');
}

if ($checkForNonObjectIdentity) {
self::createWarning('The optional $checkForNonObjectIdentity parameter of assertContains() is deprecated and will be removed in PHPUnit 9.');
}

if ($ignoreCase) {
self::createWarning('The optional $ignoreCase parameter of assertContains() is deprecated and will be removed in PHPUnit 9.');
}
// @codeCoverageIgnoreEnd

if (\is_array($haystack) ||
(\is_object($haystack) && $haystack instanceof Traversable)) {
$constraint = new TraversableContains(
$needle,
$checkForObjectIdentity,
$checkForNonObjectIdentity
);
} elseif (\is_string($haystack)) {
if (!\is_string($needle)) {
throw InvalidArgumentException::create(
1,
'string'
);
}

$constraint = new StringContains(
$needle,
$ignoreCase
);
} else {
throw InvalidArgumentException::create(
2,
'array, traversable or string'
);
}
$constraint = new TraversableContainsIdentical($needle);

static::assertThat($haystack, $constraint, $message);
}
Expand All @@ -240,55 +197,11 @@ public static function assertContainsEquals($needle, iterable $haystack, string
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws Exception
*/
public static function assertNotContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
public static function assertNotContains($needle, iterable $haystack, string $message = ''): void
{
// @codeCoverageIgnoreStart
if (\is_string($haystack)) {
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.');
}

if (!$checkForObjectIdentity) {
self::createWarning('The optional $checkForObjectIdentity parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertNotContainsEquals() instead.');
}

if ($checkForNonObjectIdentity) {
self::createWarning('The optional $checkForNonObjectIdentity parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9.');
}

if ($ignoreCase) {
self::createWarning('The optional $ignoreCase parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9.');
}
// @codeCoverageIgnoreEnd

if (\is_array($haystack) ||
(\is_object($haystack) && $haystack instanceof Traversable)) {
$constraint = new LogicalNot(
new TraversableContains(
$needle,
$checkForObjectIdentity,
$checkForNonObjectIdentity
)
);
} elseif (\is_string($haystack)) {
if (!\is_string($needle)) {
throw InvalidArgumentException::create(
1,
'string'
);
}

$constraint = new LogicalNot(
new StringContains(
$needle,
$ignoreCase
)
);
} else {
throw InvalidArgumentException::create(
2,
'array, traversable or string'
);
}
$constraint = new LogicalNot(
new TraversableContainsIdentical($needle)
);

static::assertThat($haystack, $constraint, $message);
}
Expand Down Expand Up @@ -2619,14 +2532,6 @@ public static function isNan(): IsNan
return new IsNan;
}

/**
* @deprecated Use containsEqual() or containsIdentical() instead
*/
public static function contains($value, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): TraversableContains
{
return new TraversableContains($value, $checkForObjectIdentity, $checkForNonObjectIdentity);
}

public static function containsEqual($value): TraversableContainsEqual
{
return new TraversableContainsEqual($value);
Expand Down
10 changes: 2 additions & 8 deletions src/Framework/Assert/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
use PHPUnit\Framework\Constraint\StringEndsWith;
use PHPUnit\Framework\Constraint\StringMatchesFormatDescription;
use PHPUnit\Framework\Constraint\StringStartsWith;
use PHPUnit\Framework\Constraint\TraversableContains;
use PHPUnit\Framework\Constraint\TraversableContainsEqual;
use PHPUnit\Framework\Constraint\TraversableContainsIdentical;
use PHPUnit\Framework\Constraint\TraversableContainsOnly;
Expand Down Expand Up @@ -127,7 +126,7 @@ function assertArrayNotHasKey($key, $array, string $message = ''): void
*
* @see Assert::assertContains
*/
function assertContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
function assertContains($needle, iterable $haystack, string $message = ''): void
{
Assert::assertContains(...\func_get_args());
}
Expand All @@ -146,7 +145,7 @@ function assertContainsEquals($needle, iterable $haystack, string $message = '')
*
* @see Assert::assertNotContains
*/
function assertNotContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
function assertNotContains($needle, iterable $haystack, string $message = ''): void
{
Assert::assertNotContains(...\func_get_args());
}
Expand Down Expand Up @@ -1936,11 +1935,6 @@ function isNan(): IsNan
return Assert::isNan(...\func_get_args());
}

function contains($value, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): TraversableContains
{
return Assert::contains(...\func_get_args());
}

function containsEqual($value): TraversableContainsEqual
{
return Assert::containsEqual(...\func_get_args());
Expand Down
70 changes: 8 additions & 62 deletions src/Framework/Constraint/TraversableContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,20 @@
*/
namespace PHPUnit\Framework\Constraint;

use SplObjectStorage;

/**
* Constraint that asserts that the Traversable it is applied to contains
* a given value.
*
* @deprecated Use TraversableContainsEqual or TraversableContainsIdentical instead
*/
final class TraversableContains extends Constraint
abstract class TraversableContains extends Constraint
{
/**
* @var bool
*/
private $checkForObjectIdentity;

/**
* @var bool
*/
private $checkForNonObjectIdentity;

/**
* @var mixed
*/
private $value;

public function __construct($value, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false)
public function __construct($value)
{
$this->checkForObjectIdentity = $checkForObjectIdentity;
$this->checkForNonObjectIdentity = $checkForNonObjectIdentity;
$this->value = $value;
$this->value = $value;
}

/**
Expand All @@ -48,52 +32,9 @@ public function __construct($value, bool $checkForObjectIdentity = true, bool $c
*/
public function toString(): string
{
if (\is_string($this->value) && \strpos($this->value, "\n") !== false) {
return 'contains "' . $this->value . '"';
}

return 'contains ' . $this->exporter()->export($this->value);
}

/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $other value or object to evaluate
*/
protected function matches($other): bool
{
if ($other instanceof SplObjectStorage) {
return $other->contains($this->value);
}

if (\is_object($this->value)) {
foreach ($other as $element) {
if ($this->checkForObjectIdentity && $element === $this->value) {
return true;
}

/* @noinspection TypeUnsafeComparisonInspection */
if (!$this->checkForObjectIdentity && $element == $this->value) {
return true;
}
}
} else {
foreach ($other as $element) {
if ($this->checkForNonObjectIdentity && $element === $this->value) {
return true;
}

/* @noinspection TypeUnsafeComparisonInspection */
if (!$this->checkForNonObjectIdentity && $element == $this->value) {
return true;
}
}
}

return false;
}

/**
* Returns the description of the failure
*
Expand All @@ -112,4 +53,9 @@ protected function failureDescription($other): string
$this->toString()
);
}

protected function value()
{
return $this->value;
}
}
49 changes: 3 additions & 46 deletions src/Framework/Constraint/TraversableContainsEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,8 @@
* Constraint that asserts that the Traversable it is applied to contains
* a given value (using non-strict comparison).
*/
final class TraversableContainsEqual extends Constraint
final class TraversableContainsEqual extends TraversableContains
{
/**
* @var mixed
*/
private $value;

public function __construct($value)
{
$this->value = $value;
}

/**
* Returns a string representation of the constraint.
*
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function toString(): string
{
if (\is_string($this->value) && \strpos($this->value, "\n") !== false) {
return 'contains "' . $this->value . '"';
}

return 'contains ' . $this->exporter()->export($this->value);
}

/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
Expand All @@ -50,35 +26,16 @@ public function toString(): string
protected function matches($other): bool
{
if ($other instanceof SplObjectStorage) {
return $other->contains($this->value);
return $other->contains($this->value());
}

foreach ($other as $element) {
/* @noinspection TypeUnsafeComparisonInspection */
if ($this->value == $element) {
if ($this->value() == $element) {
return true;
}
}

return false;
}

/**
* Returns the description of the failure
*
* The beginning of failure messages is "Failed asserting that" in most
* cases. This method should return the second part of that sentence.
*
* @param mixed $other evaluated value or object
*
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
protected function failureDescription($other): string
{
return \sprintf(
'%s %s',
\is_array($other) ? 'an array' : 'a traversable',
$this->toString()
);
}
}
Loading

0 comments on commit ef8e524

Please sign in to comment.