Skip to content

Commit 4d6a8c6

Browse files
Merge branch '11.5'
2 parents 01dccc7 + 7976d56 commit 4d6a8c6

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"tests/unit/Framework/Assert/assertContainsOnlyInstancesOfTest.php",
8484
"tests/unit/Framework/Assert/assertContainsOnlyIntTest.php",
8585
"tests/unit/Framework/Assert/assertContainsOnlyIterableTest.php",
86+
"tests/unit/Framework/Assert/assertContainsOnlyNullTest.php",
8687
"tests/unit/Framework/Assert/assertContainsOnlyNumericTest.php",
8788
"tests/unit/Framework/Assert/assertContainsOnlyObjectTest.php",
8889
"tests/unit/Framework/Assert/assertContainsOnlyResourceTest.php",

src/Framework/Assert.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,24 @@ final public static function assertContainsOnlyIterable(iterable $haystack, stri
440440
);
441441
}
442442

443+
/**
444+
* Asserts that a haystack contains only values of type null.
445+
*
446+
* @param iterable<mixed> $haystack
447+
*
448+
* @throws ExpectationFailedException
449+
*/
450+
final public static function assertContainsOnlyNull(iterable $haystack, string $message = ''): void
451+
{
452+
self::assertThat(
453+
$haystack,
454+
TraversableContainsOnly::forNativeType(
455+
NativeType::Null,
456+
),
457+
$message,
458+
);
459+
}
460+
443461
/**
444462
* Asserts that a haystack contains only values of type numeric.
445463
*
@@ -757,6 +775,26 @@ final public static function assertContainsNotOnlyIterable(iterable $haystack, s
757775
);
758776
}
759777

778+
/**
779+
* Asserts that a haystack does not contain only values of type null.
780+
*
781+
* @param iterable<mixed> $haystack
782+
*
783+
* @throws ExpectationFailedException
784+
*/
785+
final public static function assertContainsNotOnlyNull(iterable $haystack, string $message = ''): void
786+
{
787+
self::assertThat(
788+
$haystack,
789+
new LogicalNot(
790+
TraversableContainsOnly::forNativeType(
791+
NativeType::Null,
792+
),
793+
),
794+
$message,
795+
);
796+
}
797+
760798
/**
761799
* Asserts that a haystack does not contain only values of type numeric.
762800
*

src/Framework/Assert/Functions.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,24 @@ function assertContainsOnlyIterable(iterable $haystack, string $message = ''): v
401401
}
402402
}
403403

404+
if (!function_exists('PHPUnit\Framework\assertContainsOnlyNull')) {
405+
/**
406+
* Asserts that a haystack contains only values of type null.
407+
*
408+
* @param iterable<mixed> $haystack
409+
*
410+
* @throws ExpectationFailedException
411+
*
412+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
413+
*
414+
* @see Assert::assertContainsOnlyNull
415+
*/
416+
function assertContainsOnlyNull(iterable $haystack, string $message = ''): void
417+
{
418+
Assert::assertContainsOnlyNull(...func_get_args());
419+
}
420+
}
421+
404422
if (!function_exists('PHPUnit\Framework\assertContainsOnlyNumeric')) {
405423
/**
406424
* Asserts that a haystack contains only values of type numeric.
@@ -659,6 +677,24 @@ function assertContainsNotOnlyIterable(iterable $haystack, string $message = '')
659677
}
660678
}
661679

680+
if (!function_exists('PHPUnit\Framework\assertContainsNotOnlyNull')) {
681+
/**
682+
* Asserts that a haystack does not contain only values of type null.
683+
*
684+
* @param iterable<mixed> $haystack
685+
*
686+
* @throws ExpectationFailedException
687+
*
688+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
689+
*
690+
* @see Assert::assertContainsNotOnlyNull
691+
*/
692+
function assertContainsNotOnlyNull(iterable $haystack, string $message = ''): void
693+
{
694+
Assert::assertContainsNotOnlyNull(...func_get_args());
695+
}
696+
}
697+
662698
if (!function_exists('PHPUnit\Framework\assertContainsNotOnlyNumeric')) {
663699
/**
664700
* Asserts that a haystack does not contain only values of type numeric.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Framework;
11+
12+
use PHPUnit\Framework\Attributes\CoversMethod;
13+
use PHPUnit\Framework\Attributes\DataProviderExternal;
14+
use PHPUnit\Framework\Attributes\Small;
15+
use PHPUnit\Framework\Attributes\TestDox;
16+
17+
#[CoversMethod(Assert::class, 'assertContainsNotOnlyNull')]
18+
#[TestDox('assertContainsNotOnlyNull()')]
19+
#[Small]
20+
final class assertContainsNotOnlyNullTest extends TestCase
21+
{
22+
#[DataProviderExternal(assertContainsOnlyNullTest::class, 'failureProvider')]
23+
public function testSucceedsWhenConstraintEvaluatesToTrue(iterable $haystack): void
24+
{
25+
$this->assertContainsNotOnlyNull($haystack);
26+
}
27+
28+
#[DataProviderExternal(assertContainsOnlyNullTest::class, 'successProvider')]
29+
public function testFailsWhenConstraintEvaluatesToFalse(iterable $haystack): void
30+
{
31+
$this->expectException(AssertionFailedError::class);
32+
33+
$this->assertContainsNotOnlyNull($haystack);
34+
}
35+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Framework;
11+
12+
use PHPUnit\Framework\Attributes\CoversMethod;
13+
use PHPUnit\Framework\Attributes\DataProvider;
14+
use PHPUnit\Framework\Attributes\Small;
15+
use PHPUnit\Framework\Attributes\TestDox;
16+
17+
#[CoversMethod(Assert::class, 'assertContainsOnlyNull')]
18+
#[TestDox('assertContainsOnlyNull()')]
19+
#[Small]
20+
final class assertContainsOnlyNullTest extends TestCase
21+
{
22+
/**
23+
* @return non-empty-list<array{0: iterable}>
24+
*/
25+
public static function successProvider(): array
26+
{
27+
return [
28+
[[null]],
29+
];
30+
}
31+
32+
/**
33+
* @return non-empty-list<array{0: iterable}>
34+
*/
35+
public static function failureProvider(): array
36+
{
37+
return [
38+
[[true]],
39+
];
40+
}
41+
42+
#[DataProvider('successProvider')]
43+
public function testSucceedsWhenConstraintEvaluatesToTrue(iterable $haystack): void
44+
{
45+
$this->assertContainsOnlyNull($haystack);
46+
}
47+
48+
#[DataProvider('failureProvider')]
49+
public function testFailsWhenConstraintEvaluatesToFalse(iterable $haystack): void
50+
{
51+
$this->expectException(AssertionFailedError::class);
52+
53+
$this->assertContainsOnlyNull($haystack);
54+
}
55+
}

0 commit comments

Comments
 (0)