Skip to content

Commit 420d8ca

Browse files
Closes #3422
1 parent bcba5f4 commit 420d8ca

File tree

3 files changed

+207
-1
lines changed

3 files changed

+207
-1
lines changed

ChangeLog-7.5.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ All notable changes of the PHPUnit 7.5 release series are documented in this fil
1010
* 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()`
1111
* Implemented [#3391](https://github.com/sebastianbergmann/phpunit/issues/3391): Added a `TestHook` that fires after each test, regardless of result
1212
* Implemented [#3417](https://github.com/sebastianbergmann/phpunit/pull/3417): Refinements related to test suite sorting and TestDox result printer
13+
* Implemented [#3422](https://github.com/sebastianbergmann/phpunit/issues/3422): Added `assertStringContainsString()`, `assertStringContainsStringIgnoringCase()`, `assertStringNotContainsString()`, `assertStringNotContainsStringIgnoringCase()`, `assertIterableContains()`, `assertIterableContainsSame()`, `assertIterableNotContains()`, and `assertIterableNotContainsSame()`
1314

1415
### Deprecated
1516

16-
* 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.
17+
* 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.
1718
* 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.
1819
* 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.
20+
* 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.
1921
* 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.
2022

2123
[7.5.0]: https://github.com/sebastianbergmann/phpunit/compare/7.4...7.5.0

src/Framework/Assert.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,69 @@ public static function assertArrayNotHasKey($key, $array, string $message = ''):
164164
static::assertThat($array, $constraint, $message);
165165
}
166166

167+
public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void
168+
{
169+
$constraint = new StringContains($needle, false);
170+
171+
static::assertThat($haystack, $constraint, $message);
172+
}
173+
174+
public static function assertStringContainsStringIgnoringCase(string $needle, string $haystack, string $message = ''): void
175+
{
176+
$constraint = new StringContains($needle, true);
177+
178+
static::assertThat($haystack, $constraint, $message);
179+
}
180+
181+
public static function assertStringNotContainsString(string $needle, string $haystack, string $message = ''): void
182+
{
183+
$constraint = new LogicalNot(new StringContains($needle));
184+
185+
static::assertThat($haystack, $constraint, $message);
186+
}
187+
188+
public static function assertStringNotContainsStringIgnoringCase(string $needle, string $haystack, string $message = ''): void
189+
{
190+
$constraint = new LogicalNot(new StringContains($needle, true));
191+
192+
static::assertThat($haystack, $constraint, $message);
193+
}
194+
195+
public static function assertIterableContains($needle, iterable $haystack, string $message = ''): void
196+
{
197+
$constraint = new TraversableContains($needle, false, false);
198+
199+
static::assertThat($haystack, $constraint, $message);
200+
}
201+
202+
public static function assertIterableContainsSame($needle, iterable $haystack, string $message = ''): void
203+
{
204+
$constraint = new TraversableContains($needle, true, true);
205+
206+
static::assertThat($haystack, $constraint, $message);
207+
}
208+
209+
public static function assertIterableNotContains($needle, iterable $haystack, string $message = ''): void
210+
{
211+
$constraint = new LogicalNot(new TraversableContains($needle, false, false));
212+
213+
static::assertThat($haystack, $constraint, $message);
214+
}
215+
216+
public static function assertIterableNotContainsSame($needle, iterable $haystack, string $message = ''): void
217+
{
218+
$constraint = new LogicalNot(new TraversableContains($needle, true, true));
219+
220+
static::assertThat($haystack, $constraint, $message);
221+
}
222+
167223
/**
168224
* Asserts that a haystack contains a needle.
169225
*
170226
* @throws ExpectationFailedException
171227
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
228+
*
229+
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/3422
172230
*/
173231
public static function assertContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
174232
{
@@ -229,6 +287,8 @@ public static function assertAttributeContains($needle, string $haystackAttribut
229287
*
230288
* @throws ExpectationFailedException
231289
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
290+
*
291+
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/3422
232292
*/
233293
public static function assertNotContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
234294
{

tests/unit/Framework/AssertTest.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,6 +2952,150 @@ public function testLogicalXor(): void
29522952
);
29532953
}
29542954

2955+
public function testStringContainsStringCanBeAsserted(): void
2956+
{
2957+
$this->assertStringContainsString('bar', 'foobarbaz');
2958+
2959+
try {
2960+
$this->assertStringContainsString('barbara', 'foobarbaz');
2961+
} catch (AssertionFailedError $e) {
2962+
return;
2963+
}
2964+
2965+
$this->fail();
2966+
}
2967+
2968+
public function testStringNotContainsStringCanBeAsserted(): void
2969+
{
2970+
$this->assertStringNotContainsString('barbara', 'foobarbaz');
2971+
2972+
try {
2973+
$this->assertStringNotContainsString('bar', 'foobarbaz');
2974+
} catch (AssertionFailedError $e) {
2975+
return;
2976+
}
2977+
2978+
$this->fail();
2979+
}
2980+
2981+
public function testStringContainsStringCanBeAssertedIgnoringCase(): void
2982+
{
2983+
$this->assertStringContainsStringIgnoringCase('BAR', 'foobarbaz');
2984+
2985+
try {
2986+
$this->assertStringContainsStringIgnoringCase('BARBARA', 'foobarbaz');
2987+
} catch (AssertionFailedError $e) {
2988+
return;
2989+
}
2990+
2991+
$this->fail();
2992+
}
2993+
2994+
public function testStringNotContainsStringCanBeAssertedIgnoringCase(): void
2995+
{
2996+
$this->assertStringNotContainsStringIgnoringCase('BARBARA', 'foobarbaz');
2997+
2998+
try {
2999+
$this->assertStringNotContainsStringIgnoringCase('BAR', 'foobarbaz');
3000+
} catch (AssertionFailedError $e) {
3001+
return;
3002+
}
3003+
3004+
$this->fail();
3005+
}
3006+
3007+
public function testIterableContainsObjectCanBeAsserted(): void
3008+
{
3009+
$object = new \stdClass;
3010+
$iterable = [$object];
3011+
3012+
$this->assertIterableContains(new \stdClass, $iterable);
3013+
3014+
try {
3015+
$this->assertIterableContains(new class {
3016+
}, $iterable);
3017+
} catch (AssertionFailedError $e) {
3018+
return;
3019+
}
3020+
3021+
$this->fail();
3022+
}
3023+
3024+
public function testIterableNotContainsObjectCanBeAsserted(): void
3025+
{
3026+
$object = new \stdClass;
3027+
$iterable = [$object];
3028+
3029+
$this->assertIterableNotContains(new class {
3030+
}, $iterable);
3031+
3032+
try {
3033+
$this->assertIterableNotContains(new \stdClass, $iterable);
3034+
} catch (AssertionFailedError $e) {
3035+
return;
3036+
}
3037+
3038+
$this->fail();
3039+
}
3040+
3041+
public function testIterableContainsSameObjectCanBeAsserted(): void
3042+
{
3043+
$object = new \stdClass;
3044+
$iterable = [$object];
3045+
3046+
$this->assertIterableContainsSame($object, $iterable);
3047+
3048+
try {
3049+
$this->assertIterableContainsSame(new \stdClass, $iterable);
3050+
} catch (AssertionFailedError $e) {
3051+
return;
3052+
}
3053+
3054+
$this->fail();
3055+
}
3056+
3057+
public function testIterableNotContainsSameObjectCanBeAsserted(): void
3058+
{
3059+
$object = new \stdClass;
3060+
$iterable = [$object];
3061+
3062+
$this->assertIterableNotContainsSame(new \stdClass, $iterable);
3063+
3064+
try {
3065+
$this->assertIterableNotContainsSame($object, $iterable);
3066+
} catch (AssertionFailedError $e) {
3067+
return;
3068+
}
3069+
3070+
$this->fail();
3071+
}
3072+
3073+
public function testIterableContainsSameNonObjectCanBeAsserted(): void
3074+
{
3075+
$this->assertIterableContainsSame(1, [1]);
3076+
3077+
try {
3078+
$this->assertIterableContainsSame('1', [1]);
3079+
} catch (AssertionFailedError $e) {
3080+
return;
3081+
}
3082+
3083+
$this->fail();
3084+
}
3085+
3086+
public function testIterableNotContainsSameNonObjectCanBeAsserted(): void
3087+
{
3088+
$this->assertIterableNotContainsSame('1', [1]);
3089+
3090+
try {
3091+
$this->assertIterableNotContainsSame(1, [1]);
3092+
} catch (AssertionFailedError $e) {
3093+
return;
3094+
}
3095+
3096+
$this->fail();
3097+
}
3098+
29553099
protected function sameValues(): array
29563100
{
29573101
$object = new \SampleClass(4, 8, 15);

0 commit comments

Comments
 (0)