### Test Class with setUp() and tearDown() Source: https://docs.phpunit.de/en/10.5/fixtures.html This example demonstrates a basic test class using setUp() to initialize a test fixture and tearDown() to clean it up. The setUp() method is called before each test method, and tearDown() is called after. ```PHP assertSame( 'the-result', $this->example->doSomething() ); } protected function setUp(): void { $this->example = new Example( $this->createStub(Collaborator::class) ); } protected function tearDown(): void { $this->example = null; } } ``` -------------------------------- ### Install Phive Source: https://docs.phpunit.de/en/10.5/installation.html Download and set up Phive, the PHAR Installation and Verification Environment. This involves downloading the phive.phar, verifying its signature, and making it executable. ```bash wget https://phar.io/releases/phive.phar wget https://phar.io/releases/phive.phar.asc gpg --keyserver hkps.pool.sks-keyservers.net --recv-keys 0x9B2D5D79 gpg --verify phive.phar.asc phive.phar chmod +x phive.phar mv phive.phar /usr/local/bin/phive ``` -------------------------------- ### Concrete Test Case Extending Abstract with setUp() Source: https://docs.phpunit.de/en/10.5/fixtures.html A concrete test case class that extends an abstract test case class with a setUp() method. It is crucial to call parent::setUp() to ensure the parent's setup logic is executed. ```PHP assertIsList([1 => 'foo', '3' => 'bar']); } } ``` -------------------------------- ### Invoke Local PHPUnit Installation Source: https://docs.phpunit.de/en/10.5/installation.html Executes the locally installed PHPUnit binary to run tests or check the version. Assumes PHPUnit was installed via Composer. ```bash ./vendor/bin/phpunit --version ``` -------------------------------- ### Install PHP 8.1 using Homebrew on macOS Source: https://docs.phpunit.de/en/10.5/installation.html Installs PHP version 8.1 using the homebrew-php formulae on macOS. ```bash brew install shivammathur/php/php@8.1 ``` -------------------------------- ### Install Xdebug Extension using Homebrew on macOS Source: https://docs.phpunit.de/en/10.5/installation.html Installs and enables the Xdebug extension for debugging on macOS. ```bash brew install xdebug@8.1 ``` -------------------------------- ### Test Execution Output Source: https://docs.phpunit.de/en/10.5/writing-tests-for-phpunit.html Example output from running the basic Greeter test, showing a successful test run with one assertion. ```text ./tools/phpunit tests/GreeterTest.php PHPUnit 10.5.48 by Sebastian Bergmann and contributors. Runtime: PHP 8.4.10 . 1 / 1 (100%) Time: 00:00, Memory: 22.94 MB OK (1 test, 1 assertion) ``` -------------------------------- ### Install PCOV Extension using Homebrew on macOS Source: https://docs.phpunit.de/en/10.5/installation.html Installs and enables the PCOV extension for code coverage on macOS. ```bash brew install pcov@8.1 ``` -------------------------------- ### PHPUnit assertIsResource() Example Source: https://docs.phpunit.de/en/10.5/assertions.html Use assertIsResource() to verify if a variable is a resource. This example shows a failure when null is asserted as a resource. ```php assertIsResource(null); } } ``` -------------------------------- ### Tap PHP Homebrew Formulae on macOS Source: https://docs.phpunit.de/en/10.5/installation.html Adds the necessary Homebrew repositories for installing PHP and its extensions on macOS. ```bash brew tap shivammathur/php brew tap shivammathur/extensions ``` -------------------------------- ### Setup Fixtures Before Each Test Method Source: https://docs.phpunit.de/en/10.5/annotations.html The `@before` annotation specifies methods to be executed before each test method within a class. This is useful for setting up test-specific fixtures. ```php foo = 'foo'; $expected->bar = 'bar'; $actual = new stdClass(); $actual->foo = 'bar'; $actual->baz = 'bar'; $this->assertEquals($expected, $actual); } } ```