Drupal Testing: PHP: 02: PHPUnit Setup

https://phpunit.de/getting-started/phpunit-7.html

 

Note: Run composer.phar install often. In PHPUnit newly added classes weren't visible in the classpath until the install was run.

 

PHPUnit required  this CLI string... probably needs '/' for php,

then '\' for the OS filespecs

C:/xampp/htdocs/vendor/bin/phpunit --bootstrap W:\w\git\behatphpfacebook\vendor\autoload.php W:\w\git\behatphpfacebook\tests\EmailTest

 

Running Composer Install After Adding PHPUnit to Json File

C:\xampp\htdocs
λ php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
Nothing to install or update
Generating autoload files

  [RuntimeException]
  Could not scan for classes inside "src/" which does not appear to be a file nor a folder

install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--no-suggest] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--ignore-platform-reqs] [--] [<packages>]...

Running Composer Install Again After Adding '/src' to site root

C:\xampp\htdocs
λ php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
Nothing to install or update
Generating autoload files

 


Running Composer Install After Removing composer.lock ( to pick up phpunit )

Before Removing composer.lock

C:\xampp\htdocs
λ php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
Nothing to install or update
Generating autoload files

After Removing composer.lock

C:\xampp\htdocs
λ php composer.phar install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 27 installs, 0 updates, 0 removals
  - Installing sebastian/version (2.0.1): Downloading (100%)
  - Installing sebastian/resource-operations (1.0.0): Downloading (100%)
  - Installing sebastian/recursion-context (3.0.0): Downloading (100%)
  - Installing sebastian/object-reflector (1.1.1): Downloading (100%)
  - Installing sebastian/object-enumerator (3.0.3): Downloading (100%)
  - Installing sebastian/global-state (2.0.0): Downloading (100%)
  - Installing sebastian/exporter (3.1.0): Downloading (100%)
  - Installing sebastian/environment (3.1.0): Downloading (100%)
  - Installing sebastian/diff (3.0.1): Downloading (100%)
  - Installing sebastian/comparator (3.0.2): Downloading (100%)
  - Installing phpunit/php-timer (2.0.0): Downloading (100%)
  - Installing phpunit/php-text-template (1.2.1): Downloading (100%)
  - Installing phpunit/php-file-iterator (2.0.2): Downloading (100%)
  - Installing theseer/tokenizer (1.1.0): Downloading (100%)
  - Installing sebastian/code-unit-reverse-lookup (1.0.1): Downloading (100%)
  - Installing phpunit/php-token-stream (3.0.0): Downloading (100%)
  - Installing phpunit/php-code-coverage (6.0.7): Downloading (100%)
  - Installing doctrine/instantiator (1.1.0): Downloading (100%)
  - Installing webmozart/assert (1.3.0): Downloading (100%)
  - Installing phpdocumentor/reflection-common (1.0.1): Downloading (100%)
  - Installing phpdocumentor/type-resolver (0.4.0): Downloading (100%)
  - Installing phpdocumentor/reflection-docblock (4.3.0): Downloading (100%)
  - Installing phpspec/prophecy (1.8.0): Downloading (100%)
  - Installing phar-io/version (2.0.1): Downloading (100%)
  - Installing phar-io/manifest (1.0.3): Downloading (100%)
  - Installing myclabs/deep-copy (1.8.1): Downloading (100%)
  - Installing phpunit/phpunit (7.3.5): Downloading (100%)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.6.0)
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0)
phpunit/phpunit suggests installing ext-soap (*)
phpunit/phpunit suggests installing ext-xdebug (*)
Writing lock file
Generating autoload files

 


Placing phpunit.phar in PATH as PHPUNIT

THIS IS OPTIONAL FOR WHAT IS TAKING PLACE ON THIS PAGE

While this is probably recommended to provide alternate means of running PHPUnit tests, composer uses the instance of phpunit in its /vendor/phpunit folder

 


Place Email class in /src folder

C:\xampp\htdocs\src\Email.php

Source Code

<?php
declare(strict_types=1);

final class Email
{
    private $email;

    private function __construct(string $email)
    {
        $this->ensureIsValidEmail($email);

        $this->email = $email;
    }

    public static function fromString(string $email): self
    {
        return new self($email);
    }

    public function __toString(): string
    {
        return $this->email;
    }

    private function ensureIsValidEmail(string $email): void
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new InvalidArgumentException(
                sprintf(
                    '"%s" is not a valid email address',
                    $email
                )
            );
        }
    }
}

Place EmailTest Class in /tests folder

C:\xampp\htdocs\tests\EmailTest.php

Source Code

<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class EmailTest extends TestCase
{
    public function testCanBeCreatedFromValidEmailAddress(): void
    {
        $this->assertInstanceOf(
            Email::class,
            Email::fromString('user@example.com')
        );
    }

    public function testCannotBeCreatedFromInvalidEmailAddress(): void
    {
        $this->expectException(InvalidArgumentException::class);

        Email::fromString('invalid');
    }

    public function testCanBeUsedAsString(): void
    {
        $this->assertEquals(
            'user@example.com',
            Email::fromString('user@example.com')
        );
    }
}

Run Tests

C:\xampp\htdocs
λ C:/xampp/htdocs/vendor/bin/phpunit --bootstrap C:\xampp\htdocs\vendor\autoload.php C:\xampp\htdocs\tests\EmailTest
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 46 ms, Memory: 4.00MB

OK (3 tests, 3 assertions)

Run Tests as -testdox

C:\xampp\htdocs
λ C:/xampp/htdocs/vendor/bin/phpunit --bootstrap C:\xampp\htdocs\vendor\autoload.php --testdox tests
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.

Email
 ✔ Can be created from valid email address
 ✔ Cannot be created from invalid email address
 ✔ Can be used as string

Time: 43 ms, Memory: 4.00MB

OK (3 tests, 3 assertions)

 


Errors Were Thrown When Having Files in Incorrect Locations

C:\xampp\htdocs\vendor\phpunit\phpunit
λ C:/xampp/htdocs/vendor/bin/phpunit --bootstrap C:\xampp\htdocs\vendor\autoload.php C:\xampp\htdocs\vendor\phpunit\phpunit\tests\EmailTest
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.2.9
Configuration: C:\xampp\htdocs\vendor\phpunit\phpunit\phpunit.xml

EEE                                                                 3 / 3 (100%)

Time: 55 ms, Memory: 4.00MB

There were 3 errors:

1) EmailTest::testCanBeCreatedFromValidEmailAddress
include(C:\xampp\htdocs\vendor\composer/../../src/Email.php): failed to open stream: No such file or directory

C:\xampp\htdocs\vendor\composer\ClassLoader.php:444
C:\xampp\htdocs\vendor\composer\ClassLoader.php:444
C:\xampp\htdocs\vendor\composer\ClassLoader.php:322
C:\xampp\htdocs\vendor\phpunit\phpunit\tests\EmailTest.php:12
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestCase.php:1150
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestCase.php:844
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestResult.php:665
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestCase.php:798
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestSuite.php:750
C:\xampp\htdocs\vendor\phpunit\phpunit\src\TextUI\TestRunner.php:586
C:\xampp\htdocs\vendor\phpunit\phpunit\src\TextUI\Command.php:203
C:\xampp\htdocs\vendor\phpunit\phpunit\src\TextUI\Command.php:159

2) EmailTest::testCannotBeCreatedFromInvalidEmailAddress
include(C:\xampp\htdocs\vendor\composer/../../src/Email.php): failed to open stream: No such file or directory

C:\xampp\htdocs\vendor\composer\ClassLoader.php:444
C:\xampp\htdocs\vendor\composer\ClassLoader.php:444
C:\xampp\htdocs\vendor\composer\ClassLoader.php:322
C:\xampp\htdocs\vendor\phpunit\phpunit\tests\EmailTest.php:20
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestCase.php:1150
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestCase.php:844
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestResult.php:665
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestCase.php:798
C:\xampp\htdocs\vendor\phpunit\phpunit\src\Framework\TestSuite.php:750
C:\xampp\htdocs\vendor\phpunit\phpunit\src\TextUI\TestRunner.php:586
C:\xampp\htdocs\vendor\phpunit\phpunit\src\TextUI\Command.php:203
C:\xampp\htdocs\vendor\phpunit\phpunit\src\TextUI\Command.php:159

 

  • Error indicates MISSING EMAIL CLASS

 

 

 

 

Tags