# Tests and verify
phpunit, requests answered in the test's process, and what verify runs before a change is done.
- **You will have:** the team project's suite, three kinds of test in it -- a class alone, a route asked for as a
browser would, a database of the tests' own -- and the habits that keep it green.
- **You need:** **Entities and a database** done.
## 1. Done means `phpunit` passes
In a project, a change is done when its test suite passes, run from the project's folder:
```bash
phpunit
```
There is no other check to run: the suite asks every page and route you changed for itself. The project came
with its suite set up; three of its files matter.
**`phpunit.xml`** collects the tests of `tests/unit`, not of `tests/`:
```xml from=phpunit.xml
tests/unit
```
A test class is named `TestThing`, not `ThingTest`. That is why the suite cannot pick its files by a `Test` suffix,
and takes every `.php` of `tests/unit` instead: `tests/bootstrap.php` and the helpers beside it stay out.
**`tests/bootstrap.php`** starts the application in dev mode, like a checkout, then sends the log to a file:
```php from=tests/bootstrap.php
\MMO\app()->setStream(sys_get_temp_dir() . '/acme.portal-tests.log');
```
The suite fails any test that prints something. Without this line, the log would print, and every test would
fail. When a test fails for a reason you do not see, that file holds the request's log.
## 2. A class on its own
The fastest test calls a class directly: no request, no database. `tests/unit/TestMember.php` checks what the
team's API relies on, `Member::Check()`:
```php from=tests/unit/TestMember.php
class TestMember extends TestCase {
public function testAValidMemberHasNoError(): void {
$this->assertSame([], Member::Check(['name' => 'Ada Lovelace', 'role' => 'sales']));
//a member with no role gets the entity's default one
$this->assertSame([], Member::Check(['name' => 'Ada Lovelace']));
}
public function testEveryWrongFieldIsNamed(): void {
$errors = Member::Check(['name' => ' ', 'role' => 'pirate']);
$this->assertSame(['name', 'role'], array_keys($errors));
$this->assertStringContainsString('engineering', $errors['role']);
}
}
```
Name each test after what it proves: when it fails, its name says what broke.
## 3. A request, as a browser sends it
`$this->http()` hands the application a request inside the test: the same routing, controllers, pages and
templates as the web server, with no server. It returns the response: its status, its headers, its body.
```php from=tests/unit/TestTeam.php
protected function json(string $method, string $uri, array $body = []): array {
$response = $this->http($method, $uri, $body);
return [$response->getStatusCode(), json_decode((string)$response->getBody(), true)['data']];
}
```
A `GET` takes its query string in the url (`team.json?role=sales`); a `POST` takes what a form would send as its
third argument. Test the refusals as much as the successes: they are what a user hits first.
```php from=tests/unit/TestTeam.php
[$status, $data] = $this->json('POST', 'team.json', ['name' => '', 'role' => 'pirate']);
$this->assertSame(422, $status);
$this->assertSame(['name', 'role'], array_keys($data['errors']));
```
## 4. A database of the tests' own
The tests write: never to the database you develop with. Before the first request, point the connection elsewhere,
here a database in memory, empty at each run:
```php from=tests/unit/TestTeam.php
static public function setUpBeforeClass(): void {
//a database of the tests' own, in memory: built empty, gone when they end, never the one of db/
\MMO\config(['db' => ['filename' => ':memory:']]);
}
```
**The tests share one process.** What one test wrote, the next one reads, since the database and the modules
(`Database::Shared()`) live as long as the run. So a test does not count on an empty table: it counts what is
there before, then checks the difference.
## 5. Run one test, read a failure
```bash
phpunit tests/unit/TestMember.php
phpunit --testdox
```
The first runs one file. `--testdox` lists every test as a sentence, from its name:
```text
Member (AcmePortal\tests\unit\Member)
✔ A valid member has no error
✔ Every wrong field is named
```
A failure gives the test, what was expected, what came, and the line. Here, a test expecting only `name` to be
wrong:
```text
1) AcmePortal\tests\unit\TestTrap::testFails
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
Array &0 [
0 => 'name',
+ 1 => 'role',
]
```
An `echo` or a `var_dump()` left in the code fails its test too, as "risky": "Test code or tested code printed
unexpected output". Remove it, or write to the log.
## 6. Before a commit
The project's git hook, `scripts/git/pre-commit`, refuses a commit that carries an obvious credential: a password,
a key. It is turned on by `mmo.php project`; on a clone of your project, turn it on once:
```bash
git config core.hooksPath scripts/git
```
## 7. verify, for the framework's libraries
When you change a **library** of the framework rather than your project -- the guides of **Extending the
framework** do -- "done" asks more, and one command runs it all from the hub:
```bash
php tools/mmo.php verify --lib=pdo
```
It lints every php file you changed, refuses a new one without `declare(strict_types=1)`, looks for a secret you
added, runs phpstan and its security rules on your lines, then the test suite of every library you touched. It
writes nothing, and exits with an error on any problem. A project has none of that machinery: its `phpunit` is the check.
## Common mistakes
- **`ThingTest` instead of `TestThing`.** The file is collected, but the house convention is the other way round:
keep one.
- **A test in `tests/` instead of `tests/unit/`.** It is never run, and never fails.
- **`echo` or `var_dump()` while debugging.** The test turns risky and the suite fails. Read the log file instead.
- **A test that needs another to run first.** It passes alone and fails in the suite, or the other way round.
Each test sets up what it needs.
- **Testing on the database of `db/`.** Point `db` elsewhere before the first request.
## Going further
- Next: **Deploy**.
- [Testing](https://www.mmoframework.fr/en/documentation/mmo.core/Testing.md): the suite's files, `http()` and
its examples.