The <vendor>/<name>
of this project will be poing/eviternity
. Use your own package vendor/name.
mkdir -p poing/eviternity
cd poing/eviternity
composer init
Skipping through the settings, you should find at least the following in your composer.json
.
{
"name": "poing/eviternity",
"require": {}
}
Since Laravel 5.7 is currently available, we'll use version 3.7 of orchestra/testbench. See the version compatibility for other Laravel versions.
composer require --dev --prefer-dist orchestra/testbench 3.7
The above command will add the require-dev
section, as shown below.
{
"name": "poing/eviternity",
- "require": {}
+ "require": {},
+ "require-dev": {
+ "orchestra/testbench": "3.7"
+ }
}
We'll create the src
directory for the package files, and tests
for our testing.
mkdir -p src tests
You should have the following directory structure.
.
├── composer.json
├── src
└── tests
To autoload
the package and test files, we'll add the following.
{
"name": "poing/eviternity",
"require": {}
"require": {},
"require-dev": {
"orchestra/testbench": "3.7"
- }
+ },
+ "autoload": {
+ "psr-4": {
+ "Poing\\Eviternity\\": "src/"
+ }
+ },
+ "autoload-dev": {
+ "psr-4": {
+ "Poing\\Eviternity\\Tests\\": "tests/"
+ }
+ }
}
I use the full namespace
for my tests. You could just call it Tests\\
Our first test will use phpunit
, just to confirm basic testing works.
<?php
namespace Poing\Eviternity\Tests;
use PHPUnit\Framework\TestCase;
class BasicTest extends TestCase
{
public function testBasic()
{
$this->assertTrue(true);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuite name="Basic Test" >
<file>./tests/BasicTest.php</file>
</testsuite>
</phpunit>
.
├── composer.json
├── phpunit.xml
├── src
└── tests
└── BasicTest.php
You can run the test with either of the following phpunit
commands:
phpunit
./vendor/bin/phpunit
PHPUnit 7.5.4 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 157 ms, Memory: 4.00MB
OK (1 test, 1 assertion)
Replace PHPUnit\Framework\TestCase
with Orchestra\Testbench\TestCase
in your tests.
<?php
namespace Poing\Eviternity\Tests;
- use PHPUnit\Framework\TestCase;
+ use Orchestra\Testbench\TestCase;
class ...
<?php
namespace Poing\Eviternity\Tests;
use Orchestra\Testbench\TestCase;
class OrchestraTest extends TestCase
{
public function testBasic()
{
$this->assertTrue(true);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuite name="Basic Tests" >
<file>./tests/BasicTest.php</file>
+ <file>./tests/OrchestraTest.php</file>
</testsuite>
</phpunit>
Do not use the global phpunit
. It will fail! It needs the PSR-4 auto-loaded namespace information.
Use ./vendor/bin/phpunit
from this point forward. To avoid having to rewrite your code.
PHPUnit 7.5.4 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 556 ms, Memory: 10.00MB
OK (2 tests, 2 assertions)