I want to create a mock to replace a resource: Show
Table of Contents
I got this warning:
How can I resolve this deprecation? yivi 37.3k18 gold badges92 silver badges119 bronze badges asked Nov 30, 2020 at 14:14 From now on we are supposed to use either
This is explained in the PR (linked from the method PHP doc directly, as seen here). answered Nov 30, 2020 at 14:26 4 This appendix lists the various assertion methods that are available. Static vs. Non-Static Usage of Assertion Methods¶PHPUnit’s assertions are implemented in The assertion methods are declared static and can be invoked from any context using A common question, especially from developers new to PHPUnit, is whether using For most people it just “feels right” to use assertArrayHasKey()¶
Reports an error identified by
Example 1.1 Usage of assertArrayHasKey()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class ArrayHasKeyTest extends TestCase { public function testFailure(): void { $this->assertArrayHasKey('foo', ['bar' => 'baz']); } } $ phpunit ArrayHasKeyTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) ArrayHasKeyTest::testFailure Failed asserting that an array has the key 'foo'. /home/sb/ArrayHasKeyTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertClassHasAttribute()¶
Reports an error identified by
Example 1.2 Usage of assertClassHasAttribute()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class ClassHasAttributeTest extends TestCase { public function testFailure(): void { $this->assertClassHasAttribute('foo', stdClass::class); } } $ phpunit ClassHasAttributeTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) ClassHasAttributeTest::testFailure Failed asserting that class "stdClass" has attribute "foo". /home/sb/ClassHasAttributeTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertClassHasStaticAttribute()¶
Reports an error identified by
Example 1.3 Usage of assertClassHasStaticAttribute()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class ClassHasStaticAttributeTest extends TestCase { public function testFailure(): void { $this->assertClassHasStaticAttribute('foo', stdClass::class); } } $ phpunit ClassHasStaticAttributeTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) ClassHasStaticAttributeTest::testFailure Failed asserting that class "stdClass" has static attribute "foo". /home/sb/ClassHasStaticAttributeTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertContains()¶
Reports an error identified by
Example 1.4 Usage of assertContains()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class ContainsTest extends TestCase { public function testFailure(): void { $this->assertContains(4, [1, 2, 3]); } } $ phpunit ContainsTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) ContainsTest::testFailure Failed asserting that an array contains 4. /home/sb/ContainsTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertStringContainsString()¶
Reports an error
identified by
Example 1.5 Usage of assertStringContainsString()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class StringContainsStringTest extends TestCase { public function testFailure(): void { $this->assertStringContainsString('foo', 'bar'); } } $ phpunit StringContainsStringTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 37 ms, Memory: 6.00 MB There was 1 failure: 1) StringContainsStringTest::testFailure Failed asserting that 'bar' contains "foo". /home/sb/StringContainsStringTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertStringContainsStringIgnoringCase()¶
Reports an error identified by Differences in casing are ignored when
Example 1.6 Usage of assertStringContainsStringIgnoringCase()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class StringContainsStringIgnoringCaseTest extends TestCase { public function testFailure(): void { $this->assertStringContainsStringIgnoringCase('foo', 'bar'); } } $ phpunit StringContainsStringIgnoringCaseTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 40 ms, Memory: 6.00 MB There was 1 failure: 1) StringContainsStringTest::testFailure Failed asserting that 'bar' contains "foo". /home/sb/StringContainsStringIgnoringCaseTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertContainsOnly()¶
Reports an error identified by
Example 1.7 Usage of assertContainsOnly()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class ContainsOnlyTest extends TestCase { public function testFailure(): void { $this->assertContainsOnly('string', ['1', '2', 3]); } } $ phpunit ContainsOnlyTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) ContainsOnlyTest::testFailure Failed asserting that Array ( 0 => '1' 1 => '2' 2 => 3 ) contains only values of type "string". /home/sb/ContainsOnlyTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertContainsOnlyInstancesOf()¶
Reports an error identified by Example 1.8 Usage of assertContainsOnlyInstancesOf()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class ContainsOnlyInstancesOfTest extends TestCase { public function testFailure(): void { $this->assertContainsOnlyInstancesOf( Foo::class, [new Foo, new Bar, new Foo] ); } } $ phpunit ContainsOnlyInstancesOfTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) ContainsOnlyInstancesOfTest::testFailure Failed asserting that Array ([0]=> Bar Object(...)) is an instance of class "Foo". /home/sb/ContainsOnlyInstancesOfTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertCount()¶
Reports an error identified by
Example 1.9 Usage of assertCount()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class CountTest extends TestCase { public function testFailure(): void { $this->assertCount(0, ['foo']); } } $ phpunit CountTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) CountTest::testFailure Failed asserting that actual size 1 matches expected size 0. /home/sb/CountTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertDirectoryExists()¶
Reports
an error identified by
Example 1.10 Usage of assertDirectoryExists()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class DirectoryExistsTest extends TestCase { public function testFailure(): void { $this->assertDirectoryExists('/path/to/directory'); } } $ phpunit DirectoryExistsTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) DirectoryExistsTest::testFailure Failed asserting that directory "/path/to/directory" exists. /home/sb/DirectoryExistsTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertDirectoryIsReadable()¶
Reports an error identified by
Example 1.11 Usage of assertDirectoryIsReadable()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class DirectoryIsReadableTest extends TestCase { public function testFailure(): void { $this->assertDirectoryIsReadable('/path/to/directory'); } } $ phpunit DirectoryIsReadableTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) DirectoryIsReadableTest::testFailure Failed asserting that "/path/to/directory" is readable. /home/sb/DirectoryIsReadableTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertDirectoryIsWritable()¶
Reports an error identified by
Example 1.12 Usage of assertDirectoryIsWritable()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class DirectoryIsWritableTest extends TestCase { public function testFailure(): void { $this->assertDirectoryIsWritable('/path/to/directory'); } } $ phpunit DirectoryIsWritableTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) DirectoryIsWritableTest::testFailure Failed asserting that "/path/to/directory" is writable. /home/sb/DirectoryIsWritableTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertEmpty()¶
Reports an error identified by
Example 1.13 Usage of assertEmpty()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class EmptyTest extends TestCase { public function testFailure(): void { $this->assertEmpty(['foo']); } } $ phpunit EmptyTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) EmptyTest::testFailure Failed asserting that an array is empty. /home/sb/EmptyTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertEquals()¶
Reports an error identified by
Example 1.14 Usage of assertEquals()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class EqualsTest extends TestCase { public function testFailure(): void { $this->assertEquals(1, 0); } public function testFailure2(): void { $this->assertEquals('bar', 'baz'); } public function testFailure3(): void { $this->assertEquals("foo\nbar\nbaz\n", "foo\nbah\nbaz\n"); } } $ phpunit EqualsTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. FFF Time: 0 seconds, Memory: 5.25Mb There were 3 failures: 1) EqualsTest::testFailure Failed asserting that 0 matches expected 1. /home/sb/EqualsTest.php:6 2) EqualsTest::testFailure2 Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'bar' +'baz' /home/sb/EqualsTest.php:11 3) EqualsTest::testFailure3 Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ 'foo -bar +bah baz ' /home/sb/EqualsTest.php:16 FAILURES! Tests: 3, Assertions: 3, Failures: 3. More specialized comparisons are used for specific argument types for
Reports an error identified by Example 1.15 Usage of assertEquals() with DOMDocument objects¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class EqualsTest extends TestCase { public function testFailure(): void { $expected = new DOMDocument; $expected->loadXML('<foo><bar/></foo>'); $actual = new DOMDocument; $actual->loadXML('<bar><foo/></bar>'); $this->assertEquals($expected, $actual); } } $ phpunit EqualsTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) EqualsTest::testFailure Failed asserting that two DOM documents are equal. --- Expected +++ Actual @@ @@ <?xml version="1.0"?> -<foo> - <bar/> -</foo> +<bar> + <foo/> +</bar> /home/sb/EqualsTest.php:12 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
Reports an error identified by Example 1.16 Usage of assertEquals() with objects¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class EqualsTest extends TestCase { public function testFailure(): void { $expected = new stdClass; $expected->foo = 'foo'; $expected->bar = 'bar'; $actual = new stdClass; $actual->foo = 'bar'; $actual->baz = 'bar'; $this->assertEquals($expected, $actual); } } $ phpunit EqualsTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) EqualsTest::testFailure Failed asserting that two objects are equal. --- Expected +++ Actual @@ @@ stdClass Object ( - 'foo' => 'foo' - 'bar' => 'bar' + 'foo' => 'bar' + 'baz' => 'bar' ) /home/sb/EqualsTest.php:14 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
Reports an error identified by Example 1.17 Usage of assertEquals() with arrays¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class EqualsTest extends TestCase { public function testFailure(): void { $this->assertEquals(['a', 'b', 'c'], ['a', 'c', 'd']); } } $ phpunit EqualsTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) EqualsTest::testFailure Failed asserting that two arrays are equal. --- Expected +++ Actual @@ @@ Array ( 0 => 'a' - 1 => 'b' - 2 => 'c' + 1 => 'c' + 2 => 'd' ) /home/sb/EqualsTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertEqualsCanonicalizing()¶
Reports an error identified by The contents of
Example 1.18 Usage of assertEqualsCanonicalizing()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class EqualsCanonicalizingTest extends TestCase { public function testFailure() { $this->assertEqualsCanonicalizing([3, 2, 1], [2, 3, 0, 1]); } } $ phpunit EqualsCanonicalizingTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 42 ms, Memory: 6.00 MB There was 1 failure: 1) EqualsCanonicalizingTest::testFailure Failed asserting that two arrays are equal. --- Expected +++ Actual @@ @@ Array ( - 0 => 1 - 1 => 2 - 2 => 3 + 0 => 0 + 1 => 1 + 2 => 2 + 3 => 3 ) /home/sb/EqualsCanonicalizingTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertEqualsIgnoringCase()¶
Reports an error identified by Differences in casing are ignored for the comparison
of
Example 1.19 Usage of assertEqualsIgnoringCase()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class EqualsIgnoringCaseTest extends TestCase { public function testFailure() { $this->assertEqualsIgnoringCase('foo', 'BAR'); } } $ phpunit EqualsIgnoringCaseTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 51 ms, Memory: 6.00 MB There was 1 failure: 1) EqualsIgnoringCaseTest::testFailure Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'foo' +'BAR' /home/sb/EqualsIgnoringCaseTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertEqualsWithDelta()¶
Reports an error identified by Please read “What Every Computer Scientist Should Know About Floating-Point Arithmetic” to
understand why
Example 1.20 Usage of assertEqualsWithDelta()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class EqualsWithDeltaTest extends TestCase { public function testFailure() { $this->assertEqualsWithDelta(1.0, 1.5, 0.1); } } $ phpunit EqualsWithDeltaTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 41 ms, Memory: 6.00 MB There was 1 failure: 1) EqualsWithDeltaTest::testFailure Failed asserting that 1.5 matches expected 1.0. /home/sb/EqualsWithDeltaTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertObjectEquals()¶
Reports an error identified by It is a bad practice to use The most common use case for custom comparators are Value Objects. These objects usually have an Example 1.21 Usage of assertObjectEquals()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class SomethingThatUsesEmailTest extends TestCase { public function testSomething(): void { $a = new Email(''); $b = new Email(''); $c = new Email(''); // This passes $this->assertObjectEquals($a, $b); // This fails $this->assertObjectEquals($a, $c); } } Example 1.22 Email value object with equals() method¶ <?php declare(strict_types=1); final class Email { private string $email; public function __construct(string $email) { $this->ensureIsValidEmail($email); $this->email = $email; } public function asString(): string { return $this->email; } public function equals(self $other): bool { return $this->asString() === $other->asString(); } private function ensureIsValidEmail(string $email): void { // ... } } $ phpunit EqualsTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 00:00.017, Memory: 4.00 MB There was 1 failure: 1) SomethingThatUsesEmailTest::testSomething Failed asserting that two objects are equal. The objects are not equal according to Email::equals(). /home/sb/SomethingThatUsesEmailTest.php:16 FAILURES! Tests: 1, Assertions: 2, Failures: 1. Please note:
If any of the aforementioned assumptions is not fulfilled or if assertFalse()¶
Reports an error identified by
Example 1.23 Usage of assertFalse()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class FalseTest extends TestCase { public function testFailure(): void { $this->assertFalse(true); } } $ phpunit FalseTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) FalseTest::testFailure Failed asserting that true is false. /home/sb/FalseTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertFileEquals()¶
Reports an error identified by
Example 1.24 Usage of assertFileEquals()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class FileEqualsTest extends TestCase { public function testFailure(): void { $this->assertFileEquals('/home/sb/expected', '/home/sb/actual'); } } $ phpunit FileEqualsTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) FileEqualsTest::testFailure Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'expected +'actual ' /home/sb/FileEqualsTest.php:6 FAILURES! Tests: 1, Assertions: 3, Failures: 1. assertFileExists()¶
Reports an error identified by
Example 1.25 Usage of assertFileExists()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class FileExistsTest extends TestCase { public function testFailure(): void { $this->assertFileExists('/path/to/file'); } } $ phpunit FileExistsTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) FileExistsTest::testFailure Failed asserting that file "/path/to/file" exists. /home/sb/FileExistsTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertFileIsReadable()¶
Reports an error identified by
Example 1.26 Usage of assertFileIsReadable()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class FileIsReadableTest extends TestCase { public function testFailure(): void { $this->assertFileIsReadable('/path/to/file'); } } $ phpunit FileIsReadableTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) FileIsReadableTest::testFailure Failed asserting that "/path/to/file" is readable. /home/sb/FileIsReadableTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertFileIsWritable()¶
Reports an error identified by
Example 1.27 Usage of assertFileIsWritable()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class FileIsWritableTest extends TestCase { public function testFailure(): void { $this->assertFileIsWritable('/path/to/file'); } } $ phpunit FileIsWritableTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) FileIsWritableTest::testFailure Failed asserting that "/path/to/file" is writable. /home/sb/FileIsWritableTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertGreaterThan()¶
Reports an error identified by Example 1.28 Usage of assertGreaterThan()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class GreaterThanTest extends TestCase { public function testFailure(): void { $this->assertGreaterThan(2, 1); } } $ phpunit GreaterThanTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) GreaterThanTest::testFailure Failed asserting that 1 is greater than 2. /home/sb/GreaterThanTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertGreaterThanOrEqual()¶
Reports an error identified by Example 1.29 Usage of assertGreaterThanOrEqual()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class GreatThanOrEqualTest extends TestCase { public function testFailure(): void { $this->assertGreaterThanOrEqual(2, 1); } } $ phpunit GreaterThanOrEqualTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) GreatThanOrEqualTest::testFailure Failed asserting that 1 is equal to 2 or is greater than 2. /home/sb/GreaterThanOrEqualTest.php:6 FAILURES! Tests: 1, Assertions: 2, Failures: 1. assertInfinite()¶
Reports an error identified by
Example 1.30 Usage of assertInfinite()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class InfiniteTest extends TestCase { public function testFailure(): void { $this->assertInfinite(1); } } $ phpunit InfiniteTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) InfiniteTest::testFailure Failed asserting that 1 is infinite. /home/sb/InfiniteTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertInstanceOf()¶
Reports an error identified by
Example 1.31 Usage of assertInstanceOf()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class InstanceOfTest extends TestCase { public function testFailure(): void { $this->assertInstanceOf(RuntimeException::class, new Exception); } } $ phpunit InstanceOfTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) InstanceOfTest::testFailure Failed asserting that Exception Object (...) is an instance of class "RuntimeException". /home/sb/InstanceOfTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsArray()¶
Reports an error identified by
Example 1.32 Usage of assertIsArray()¶ <?php use PHPUnit\Framework\TestCase; class ArrayTest extends TestCase { public function testFailure() { $this->assertIsArray(null); } } $ phpunit ArrayTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) ArrayTest::testFailure Failed asserting that null is of type "array". /home/sb/ArrayTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsBool()¶
Reports an error identified by
Example 1.33 Usage of assertIsBool()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class BoolTest extends TestCase { public function testFailure(): void { $this->assertIsBool(null); } } $ phpunit BoolTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) BoolTest::testFailure Failed asserting that null is of type "bool". /home/sb/BoolTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsCallable()¶
Reports an error identified by
Example 1.34 Usage of assertIsCallable()¶ <?php use PHPUnit\Framework\TestCase; class CallableTest extends TestCase { public function testFailure() { $this->assertIsCallable(null); } } $ phpunit CallableTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) CallableTest::testFailure Failed asserting that null is of type "callable". /home/sb/CallableTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsFloat()¶
Reports an error identified by
Example 1.35 Usage of assertIsFloat()¶ <?php use PHPUnit\Framework\TestCase; class FloatTest extends TestCase { public function testFailure() { $this->assertIsFloat(null); } } $ phpunit FloatTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) FloatTest::testFailure Failed asserting that null is of type "float". /home/sb/FloatTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsInt()¶
Reports an error identified by
Example 1.36 Usage of assertIsInt()¶ <?php use PHPUnit\Framework\TestCase; class IntTest extends TestCase { public function testFailure() { $this->assertIsInt(null); } } $ phpunit IntTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) IntTest::testFailure Failed asserting that null is of type "int". /home/sb/IntTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsIterable()¶
Reports an error identified by
Example 1.37 Usage of assertIsIterable()¶ <?php use PHPUnit\Framework\TestCase; class IterableTest extends TestCase { public function testFailure() { $this->assertIsIterable(null); } } $ phpunit IterableTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) IterableTest::testFailure Failed asserting that null is of type "iterable". /home/sb/IterableTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsNumeric()¶
Reports
an error identified by
Example 1.38 Usage of assertIsNumeric()¶ <?php use PHPUnit\Framework\TestCase; class NumericTest extends TestCase { public function testFailure() { $this->assertIsNumeric(null); } } $ phpunit NumericTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) NumericTest::testFailure Failed asserting that null is of type "numeric". /home/sb/NumericTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsObject()¶
Reports an error identified by
Example 1.39 Usage of assertIsObject()¶ <?php use PHPUnit\Framework\TestCase; class ObjectTest extends TestCase { public function testFailure() { $this->assertIsObject(null); } } $ phpunit ObjectTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) ObjectTest::testFailure Failed asserting that null is of type "object". /home/sb/ObjectTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsResource()¶
Reports an error identified by
Example 1.40 Usage of assertIsResource()¶ <?php use PHPUnit\Framework\TestCase; class ResourceTest extends TestCase { public function testFailure() { $this->assertIsResource(null); } } $ phpunit ResourceTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) ResourceTest::testFailure Failed asserting that null is of type "resource". /home/sb/ResourceTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsScalar()¶
Reports an error identified by
Example 1.41 Usage of assertIsScalar()¶ <?php use PHPUnit\Framework\TestCase; class ScalarTest extends TestCase { public function testFailure() { $this->assertIsScalar(null); } } $ phpunit ScalarTest PHPUnit |version|.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) ScalarTest::testFailure Failed asserting that null is of type "scalar". /home/sb/ScalarTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsString()¶
Reports an error identified by
Example 1.42 Usage of assertIsString()¶ <?php use PHPUnit\Framework\TestCase; class StringTest extends TestCase { public function testFailure() { $this->assertIsString(null); } } $ phpunit StringTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) StringTest::testFailure Failed asserting that null is of type "string". /home/sb/StringTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsReadable()¶
Reports an error identified by
Example 1.43 Usage of assertIsReadable()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class IsReadableTest extends TestCase { public function testFailure(): void { $this->assertIsReadable('/path/to/unreadable'); } } $ phpunit IsReadableTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) IsReadableTest::testFailure Failed asserting that "/path/to/unreadable" is readable. /home/sb/IsReadableTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertIsWritable()¶
Reports an error identified by
Example 1.44 Usage of assertIsWritable()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class IsWritableTest extends TestCase { public function testFailure(): void { $this->assertIsWritable('/path/to/unwritable'); } } $ phpunit IsWritableTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) IsWritableTest::testFailure Failed asserting that "/path/to/unwritable" is writable. /home/sb/IsWritableTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertJsonFileEqualsJsonFile()¶
Reports an error identified by Example 1.45 Usage of assertJsonFileEqualsJsonFile()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class JsonFileEqualsJsonFileTest extends TestCase { public function testFailure(): void { $this->assertJsonFileEqualsJsonFile( 'path/to/fixture/file', 'path/to/actual/file'); } } $ phpunit JsonFileEqualsJsonFileTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) JsonFileEqualsJsonFile::testFailure Failed asserting that '{"Mascot":"Tux"}' matches JSON string "["Mascott", "Tux", "OS", "Linux"]". /home/sb/JsonFileEqualsJsonFileTest.php:5 FAILURES! Tests: 1, Assertions: 3, Failures: 1. assertJsonStringEqualsJsonFile()¶
Reports an error identified by Example 1.46 Usage of assertJsonStringEqualsJsonFile()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class JsonStringEqualsJsonFileTest extends TestCase { public function testFailure(): void { $this->assertJsonStringEqualsJsonFile( 'path/to/fixture/file', json_encode(['Mascot' => 'ux']) ); } } $ phpunit JsonStringEqualsJsonFileTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) JsonStringEqualsJsonFile::testFailure Failed asserting that '{"Mascot":"ux"}' matches JSON string "{"Mascott":"Tux"}". /home/sb/JsonStringEqualsJsonFileTest.php:5 FAILURES! Tests: 1, Assertions: 3, Failures: 1. assertJsonStringEqualsJsonString()¶
Reports an error identified by Example 1.47 Usage of assertJsonStringEqualsJsonString()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class JsonStringEqualsJsonStringTest extends TestCase { public function testFailure(): void { $this->assertJsonStringEqualsJsonString( json_encode(['Mascot' => 'Tux']), json_encode(['Mascot' => 'ux']) ); } } $ phpunit JsonStringEqualsJsonStringTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) JsonStringEqualsJsonStringTest::testFailure Failed asserting that two objects are equal. --- Expected +++ Actual @@ @@ stdClass Object ( - 'Mascot' => 'Tux' + 'Mascot' => 'ux' ) /home/sb/JsonStringEqualsJsonStringTest.php:5 FAILURES! Tests: 1, Assertions: 3, Failures: 1. assertLessThan()¶
Reports an error identified by Example 1.48 Usage of assertLessThan()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class LessThanTest extends TestCase { public function testFailure(): void { $this->assertLessThan(1, 2); } } $ phpunit LessThanTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) LessThanTest::testFailure Failed asserting that 2 is less than 1. /home/sb/LessThanTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertLessThanOrEqual()¶
Reports an error identified by Example 1.49 Usage of assertLessThanOrEqual()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class LessThanOrEqualTest extends TestCase { public function testFailure(): void { $this->assertLessThanOrEqual(1, 2); } } $ phpunit LessThanOrEqualTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) LessThanOrEqualTest::testFailure Failed asserting that 2 is equal to 1 or is less than 1. /home/sb/LessThanOrEqualTest.php:6 FAILURES! Tests: 1, Assertions: 2, Failures: 1. assertNan()¶
Reports an error identified by Example 1.50 Usage of assertNan()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class NanTest extends TestCase { public function testFailure(): void { $this->assertNan(1); } } $ phpunit NanTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) NanTest::testFailure Failed asserting that 1 is nan. /home/sb/NanTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertNull()¶
Reports an error identified by
Example 1.51 Usage of assertNull()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class NullTest extends TestCase { public function testFailure(): void { $this->assertNull('foo'); } } $ phpunit NotNullTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) NullTest::testFailure Failed asserting that 'foo' is null. /home/sb/NotNullTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertObjectHasAttribute()¶
Reports an error identified by
Example 1.52 Usage of assertObjectHasAttribute()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class ObjectHasAttributeTest extends TestCase { public function testFailure(): void { $this->assertObjectHasAttribute('foo', new stdClass); } } $ phpunit ObjectHasAttributeTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) ObjectHasAttributeTest::testFailure Failed asserting that object of class "stdClass" has attribute "foo". /home/sb/ObjectHasAttributeTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertMatchesRegularExpression()¶
Reports an error identified by
Example 1.53 Usage of assertMatchesRegularExpression()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class RegExpTest extends TestCase { public function testFailure(): void { $this->assertMatchesRegularExpression('/foo/', 'bar'); } } $ phpunit RegExpTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) RegExpTest::testFailure Failed asserting that 'bar' matches PCRE pattern "/foo/". /home/sb/RegExpTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertStringMatchesFormat()¶
Reports an error identified by
Example 1.54 Usage of assertStringMatchesFormat()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class StringMatchesFormatTest extends TestCase { public function testFailure(): void { $this->assertStringMatchesFormat('%i', 'foo'); } } $ phpunit StringMatchesFormatTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) StringMatchesFormatTest::testFailure Failed asserting that 'foo' matches PCRE pattern "/^[+-]?d+$/s". /home/sb/StringMatchesFormatTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. The format string may contain the following placeholders:
assertStringMatchesFormatFile()¶
Reports an error identified by
Example 1.55 Usage of assertStringMatchesFormatFile()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class StringMatchesFormatFileTest extends TestCase { public function testFailure(): void { $this->assertStringMatchesFormatFile('/path/to/expected.txt', 'foo'); } } $ phpunit StringMatchesFormatFileTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) StringMatchesFormatFileTest::testFailure Failed asserting that 'foo' matches PCRE pattern "/^[+-]?d+ $/s". /home/sb/StringMatchesFormatFileTest.php:6 FAILURES! Tests: 1, Assertions: 2, Failures: 1. assertSame()¶
Reports an error identified by
Example 1.56 Usage of assertSame()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class SameTest extends TestCase { public function testFailure(): void { $this->assertSame('2204', 2204); } } $ phpunit SameTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) SameTest::testFailure Failed asserting that 2204 is identical to '2204'. /home/sb/SameTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
Reports an
error identified by Example 1.57 Usage of assertSame() with objects¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class SameTest extends TestCase { public function testFailure(): void { $this->assertSame(new stdClass, new stdClass); } } $ phpunit SameTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) SameTest::testFailure Failed asserting that two variables reference the same object. /home/sb/SameTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertSameSize()¶
Reports an error identified by
Example 1.58 Usage of assertSameSize()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class SameSizeTest extends TestCase { public function testFailure(): void { $this->assertSameSize([1, 2], [1]); } } $ phpunit StringEndsWithTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 second, Memory: 4.00Mb There was 1 failure: 1) SameSizeTest::testFailure Failed asserting that actual size 1 matches expected size 2. /home/sb/SameSizeTest.php:8 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertStringEndsWith()¶
Reports an error identified by
Example 1.59 Usage of assertStringEndsWith()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class StringEndsWithTest extends TestCase { public function testFailure(): void { $this->assertStringEndsWith('suffix', 'foo'); } } $ phpunit StringEndsWithTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 1 second, Memory: 5.00Mb There was 1 failure: 1) StringEndsWithTest::testFailure Failed asserting that 'foo' ends with "suffix". /home/sb/StringEndsWithTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertStringEqualsFile()¶
Reports an error identified by
Example 1.60 Usage of assertStringEqualsFile()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class StringEqualsFileTest extends TestCase { public function testFailure(): void { $this->assertStringEqualsFile('/home/sb/expected', 'actual'); } } $ phpunit StringEqualsFileTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) StringEqualsFileTest::testFailure Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'expected -' +'actual' /home/sb/StringEqualsFileTest.php:6 FAILURES! Tests: 1, Assertions: 2, Failures: 1. assertStringStartsWith()¶
Reports an error identified by
Example 1.61 Usage of assertStringStartsWith()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class StringStartsWithTest extends TestCase { public function testFailure(): void { $this->assertStringStartsWith('prefix', 'foo'); } } $ phpunit StringStartsWithTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) StringStartsWithTest::testFailure Failed asserting that 'foo' starts with "prefix". /home/sb/StringStartsWithTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertThat()¶More complex assertions can be formulated using the
Reports an error identified by Example 1.62 Usage of assertThat()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class BiscuitTest extends TestCase { public function testEquals(): void { $theBiscuit = new Biscuit('Ginger'); $myBiscuit = new Biscuit('Ginger'); $this->assertThat( $theBiscuit, $this->logicalNot( $this->equalTo($myBiscuit) ) ); } } Table 1.1 shows the available
assertTrue()¶
Reports an error identified by
Example 1.63 Usage of assertTrue()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class TrueTest extends TestCase { public function testFailure(): void { $this->assertTrue(false); } } $ phpunit TrueTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) TrueTest::testFailure Failed asserting that false is true. /home/sb/TrueTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1. assertXmlFileEqualsXmlFile()¶
Reports an error identified by
Example 1.64 Usage of assertXmlFileEqualsXmlFile()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class XmlFileEqualsXmlFileTest extends TestCase { public function testFailure(): void { $this->assertXmlFileEqualsXmlFile( '/home/sb/expected.xml', '/home/sb/actual.xml'); } } $ phpunit XmlFileEqualsXmlFileTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) XmlFileEqualsXmlFileTest::testFailure Failed asserting that two DOM documents are equal. --- Expected +++ Actual @@ @@ <?xml version="1.0"?> <foo> - <bar/> + <baz/> </foo> /home/sb/XmlFileEqualsXmlFileTest.php:7 FAILURES! Tests: 1, Assertions: 3, Failures: 1. assertXmlStringEqualsXmlFile()¶
Reports an error identified by
Example 1.65 Usage of assertXmlStringEqualsXmlFile()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class XmlStringEqualsXmlFileTest extends TestCase { public function testFailure(): void { $this->assertXmlStringEqualsXmlFile( '/home/sb/expected.xml', '<foo><baz/></foo>'); } } $ phpunit XmlStringEqualsXmlFileTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) XmlStringEqualsXmlFileTest::testFailure Failed asserting that two DOM documents are equal. --- Expected +++ Actual @@ @@ <?xml version="1.0"?> <foo> - <bar/> + <baz/> </foo> /home/sb/XmlStringEqualsXmlFileTest.php:7 FAILURES! Tests: 1, Assertions: 2, Failures: 1. assertXmlStringEqualsXmlString()¶
Reports an error identified by
Example 1.66 Usage of assertXmlStringEqualsXmlString()¶ <?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class XmlStringEqualsXmlStringTest extends TestCase { public function testFailure(): void { $this->assertXmlStringEqualsXmlString( '<foo><bar/></foo>', '<foo><baz/></foo>'); } } $ phpunit XmlStringEqualsXmlStringTest PHPUnit 9.5.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) XmlStringEqualsXmlStringTest::testFailure Failed asserting that two DOM documents are equal. --- Expected +++ Actual @@ @@ <?xml version="1.0"?> <foo> - <bar/> + <baz/> </foo> /home/sb/XmlStringEqualsXmlStringTest.php:7 FAILURES! Tests: 1, Assertions: 1, Failures: 1. |