pluf / test
A pluf mock object and test utilities
Installs: 5 366
Dependents: 31
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 2
Requires
- phpunit/phpunit: 7.x
- pluf/core: 6.x
- dev-master
- 6.x-dev
- 6.0.23
- 6.0.22
- 6.0.21
- 6.0.20
- 6.0.19
- 6.0.18
- 6.0.17
- 6.0.16
- 6.0.15
- 6.0.14
- 6.0.13
- 6.0.12
- 6.0.11
- 6.0.10
- 6.0.9
- 6.0.8
- 6.0.7
- 6.0.6
- 6.0.5
- 6.0.0
- 5.13.x-dev
- 5.13.1
- 5.13.0
- 5.12.7
- 5.12.6
- 5.12.4
- 5.12.3
- 5.12.2
- 5.12.1
- 5.12.0
- 5.11.16
- 5.11.15
- 5.11.14
- 5.11.13
- 5.11.12
- 5.11.11
- 5.11.10
- 5.11.9
- 5.11.8
- 5.11.7
- 5.11.6
- 5.11.5
- 5.11.4
- 5.11.3
- 5.11.2
- 5.11.1
- 5.11.0
- 5.10.2
- 5.10.1
- 5.10.0
- 5.9.6
- 5.9.5
- 5.9.4
- 5.9.3
- 5.9.2
- 5.9.1
- 5.9.0
- 5.8.6
- 5.8.5
- 5.8.4
- 5.8.3
- 5.8.2
- 5.8.1
- 5.8.0
- 5.7.2
- 5.7.1
- 5.7.0
- 5.6.0
- 5.5.3
- 5.5.2
- 5.5.1
- 5.5.0
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.1
- 5.0.0
- 4.0.15
- 4.0.14
- 4.0.13
- 4.0.12
- 4.0.11
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 3.0.11
- 3.0.8
- 3.0.7
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.0
- 2.1.10
- 2.1.9
- 2.1.7
- dev-develop
- dev-release/6.0.20
- dev-feature/psr4
This package is auto-updated.
Last update: 2024-10-22 08:29:50 UTC
README
This tutorial assumes that you use PHP 7.3 or PHP 7.4. You will learn how to write simple unit tests as well as how to download and run PlufTest.
PlufTest is based on PHPUnit 8.
Install
You can add Pluf/Test as a local, per-project, development-time dependency to your project using Composer:
➜ composer require --dev pluf/test ^6
➜ ./vendor/bin/pluftest --version
Pluf/Test 6.0.0 by pluf.ir and contributors.
The example shown above assumes that composer is on your $PATH.
Your composer.json should look similar to this:
{
"autoload": {
"classmap": [
"src/"
]
},
"require-dev": {
"pluf/test": "^9"
}
}
Test Code
suppose there is a code
src/Email.php
with content:
<?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
)
);
}
}
}
Create a file:
tests/EmailTest.php
And create the test class
<?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')
);
}
}
The test is ready
Note: Do not put tests in a namespace.
Run the test
./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/EmailTest
PHPUnit 8.0.0 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 70 ms, Memory: 10.00MB
OK (3 tests, 3 assertions)
--bootstrap vendor/autoload.php instructs the PHPUnit command-line test runner to include vendor/autoload.php before the tests are run.
tests/EmailTest instructs the PHPUnit command-line test runner to execute the tests of the EmailTest class that is declared in tests/EmailTest.php.
Using tests instead of tests/EmailTest would instruct the PHPUnit command-line test runner to execute all tests found declared in *Test.php sourcecode files in the tests directory.
Contributing
If you would like to contribute to Pluf, please read the README and CONTRIBUTING documents.
The most important guidelines are described as follows:
All code contributions - including those of people having commit access - must go through a pull request and approved by a core developer before being merged. This is to ensure proper review of all the code.
Fork the project, create a feature branch, and send us a pull request.
To ensure a consistent code base, you should make sure the code follows the PSR-2 Coding Standards.