Skip to content

Commit

Permalink
Closes #3422
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Nov 29, 2018
1 parent bcba5f4 commit 420d8ca
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ChangeLog-7.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ All notable changes of the PHPUnit 7.5 release series are documented in this fil
* Implemented [#3368](https://github.com/sebastianbergmann/phpunit/issues/3368): Added `assertIsArray()`, `assertIsBool()`, `assertIsFloat()`, `assertIsInt()`, `assertIsNumeric()`, `assertIsObject()`, `assertIsResource()`, `assertIsString()`, `assertIsScalar()`, `assertIsCallable()`, `assertIsIterable()`, `assertIsNotArray()`, `assertIsNotBool()`, `assertIsNotFloat()`, `assertIsNotInt()`, `assertIsNotNumeric()`, `assertIsNotObject()`, `assertIsNotResource()`, `assertIsNotString()`, `assertIsNotScalar()`, `assertIsNotCallable()`, `assertIsNotIterable()` as alternatives to `assertInternalType()` and `assertNotInternalType()`
* Implemented [#3391](https://github.com/sebastianbergmann/phpunit/issues/3391): Added a `TestHook` that fires after each test, regardless of result
* Implemented [#3417](https://github.com/sebastianbergmann/phpunit/pull/3417): Refinements related to test suite sorting and TestDox result printer
* Implemented [#3422](https://github.com/sebastianbergmann/phpunit/issues/3422): Added `assertStringContainsString()`, `assertStringContainsStringIgnoringCase()`, `assertStringNotContainsString()`, `assertStringNotContainsStringIgnoringCase()`, `assertIterableContains()`, `assertIterableContainsSame()`, `assertIterableNotContains()`, and `assertIterableNotContainsSame()`

### Deprecated

* The optional parameters `$delta`, `$maxDepth`, `$canonicalize`, and `$ignoreCase` of `assertEquals()`, and `assertNotEquals` are now deprecated. There is no behavioral change in this version of PHPUnit. Using these parameters will trigger a deprecation warning in PHPUnit 8 and in PHPUnit 9 these parameters will be removed.
* The methods `assertContains()` and `assertNotContains()` are now deprecated. There is no behavioral change in this version of PHPUnit. Using these methods will trigger a deprecation warning in PHPUnit 8 and in PHPUnit 9 these methods will be removed.
* The methods `assertInternalType()` and `assertNotInternalType()` are now deprecated. There is no behavioral change in this version of PHPUnit. Using these methods will trigger a deprecation warning in PHPUnit 8 and in PHPUnit 9 these methods will be removed.
* The methods `assertAttributeContains()`, `assertAttributeNotContains()`, `assertAttributeContainsOnly()`, `assertAttributeNotContainsOnly()`, `assertAttributeCount()`, `assertAttributeNotCount()`, `assertAttributeEquals()`, `assertAttributeNotEquals()`, `assertAttributeEmpty()`, `assertAttributeNotEmpty()`, `assertAttributeGreaterThan()`, `assertAttributeGreaterThanOrEqual()`, `assertAttributeLessThan()`, `assertAttributeLessThanOrEqual()`, `assertAttributeSame()`, `assertAttributeNotSame()`, `assertAttributeInstanceOf()`, `assertAttributeNotInstanceOf()`, `assertAttributeInternalType()`, `assertAttributeNotInternalType()`, `attributeEqualTo()`, `readAttribute()`, `getStaticAttribute()`, and `getObjectAttribute()` are now deprecated. There is no behavioral change in this version of PHPUnit. Using these methods will trigger a deprecation warning in PHPUnit 8 and in PHPUnit 9 these methods will be removed.
* The optional parameters `$delta`, `$maxDepth`, `$canonicalize`, and `$ignoreCase` of `assertEquals()`, and `assertNotEquals` are now deprecated. There is no behavioral change in this version of PHPUnit. Using these parameters will trigger a deprecation warning in PHPUnit 8 and in PHPUnit 9 these parameters will be removed.
* The annotations `@expectedException`, `@expectedExceptionCode`, `@expectedExceptionMessage`, and `@expectedExceptionMessageRegExp` are now deprecated. There is no behavioral change in this version of PHPUnit. Using these annotations will trigger a deprecation warning in PHPUnit 8 and in PHPUnit 9 these annotations will be removed.

[7.5.0]: https://github.com/sebastianbergmann/phpunit/compare/7.4...7.5.0
Expand Down
60 changes: 60 additions & 0 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,69 @@ public static function assertArrayNotHasKey($key, $array, string $message = ''):
static::assertThat($array, $constraint, $message);
}

public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void
{
$constraint = new StringContains($needle, false);

static::assertThat($haystack, $constraint, $message);
}

public static function assertStringContainsStringIgnoringCase(string $needle, string $haystack, string $message = ''): void
{
$constraint = new StringContains($needle, true);

static::assertThat($haystack, $constraint, $message);
}

public static function assertStringNotContainsString(string $needle, string $haystack, string $message = ''): void
{
$constraint = new LogicalNot(new StringContains($needle));

static::assertThat($haystack, $constraint, $message);
}

public static function assertStringNotContainsStringIgnoringCase(string $needle, string $haystack, string $message = ''): void
{
$constraint = new LogicalNot(new StringContains($needle, true));

static::assertThat($haystack, $constraint, $message);
}

public static function assertIterableContains($needle, iterable $haystack, string $message = ''): void
{
$constraint = new TraversableContains($needle, false, false);

static::assertThat($haystack, $constraint, $message);
}

public static function assertIterableContainsSame($needle, iterable $haystack, string $message = ''): void
{
$constraint = new TraversableContains($needle, true, true);

static::assertThat($haystack, $constraint, $message);
}

public static function assertIterableNotContains($needle, iterable $haystack, string $message = ''): void
{
$constraint = new LogicalNot(new TraversableContains($needle, false, false));

static::assertThat($haystack, $constraint, $message);
}

public static function assertIterableNotContainsSame($needle, iterable $haystack, string $message = ''): void
{
$constraint = new LogicalNot(new TraversableContains($needle, true, true));

static::assertThat($haystack, $constraint, $message);
}

/**
* Asserts that a haystack contains a needle.
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/3422
*/
public static function assertContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
{
Expand Down Expand Up @@ -229,6 +287,8 @@ public static function assertAttributeContains($needle, string $haystackAttribut
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/3422
*/
public static function assertNotContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
{
Expand Down
144 changes: 144 additions & 0 deletions tests/unit/Framework/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,150 @@ public function testLogicalXor(): void
);
}

public function testStringContainsStringCanBeAsserted(): void
{
$this->assertStringContainsString('bar', 'foobarbaz');

try {
$this->assertStringContainsString('barbara', 'foobarbaz');
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

public function testStringNotContainsStringCanBeAsserted(): void
{
$this->assertStringNotContainsString('barbara', 'foobarbaz');

try {
$this->assertStringNotContainsString('bar', 'foobarbaz');
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

public function testStringContainsStringCanBeAssertedIgnoringCase(): void
{
$this->assertStringContainsStringIgnoringCase('BAR', 'foobarbaz');

try {
$this->assertStringContainsStringIgnoringCase('BARBARA', 'foobarbaz');
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

public function testStringNotContainsStringCanBeAssertedIgnoringCase(): void
{
$this->assertStringNotContainsStringIgnoringCase('BARBARA', 'foobarbaz');

try {
$this->assertStringNotContainsStringIgnoringCase('BAR', 'foobarbaz');
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

public function testIterableContainsObjectCanBeAsserted(): void
{
$object = new \stdClass;
$iterable = [$object];

$this->assertIterableContains(new \stdClass, $iterable);

try {
$this->assertIterableContains(new class {
}, $iterable);
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

public function testIterableNotContainsObjectCanBeAsserted(): void
{
$object = new \stdClass;
$iterable = [$object];

$this->assertIterableNotContains(new class {
}, $iterable);

try {
$this->assertIterableNotContains(new \stdClass, $iterable);
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

public function testIterableContainsSameObjectCanBeAsserted(): void
{
$object = new \stdClass;
$iterable = [$object];

$this->assertIterableContainsSame($object, $iterable);

try {
$this->assertIterableContainsSame(new \stdClass, $iterable);
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

public function testIterableNotContainsSameObjectCanBeAsserted(): void
{
$object = new \stdClass;
$iterable = [$object];

$this->assertIterableNotContainsSame(new \stdClass, $iterable);

try {
$this->assertIterableNotContainsSame($object, $iterable);
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

public function testIterableContainsSameNonObjectCanBeAsserted(): void
{
$this->assertIterableContainsSame(1, [1]);

try {
$this->assertIterableContainsSame('1', [1]);
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

public function testIterableNotContainsSameNonObjectCanBeAsserted(): void
{
$this->assertIterableNotContainsSame('1', [1]);

try {
$this->assertIterableNotContainsSame(1, [1]);
} catch (AssertionFailedError $e) {
return;
}

$this->fail();
}

protected function sameValues(): array
{
$object = new \SampleClass(4, 8, 15);
Expand Down

0 comments on commit 420d8ca

Please sign in to comment.