Skip to content

Commit

Permalink
upgrade php-code-coverage; improve readability of paths; isolate exam…
Browse files Browse the repository at this point in the history
…ple.php from the test-suite; basic test for code-coverage output; require PHP 8.1; drop guzzle test-dependencey in favor of a simpler PSR-18 client; some cleanup and docs
  • Loading branch information
mindplay-dk committed Dec 5, 2023
1 parent 6501bc0 commit 0a5d138
Show file tree
Hide file tree
Showing 9 changed files with 528 additions and 617 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/.idea
.*
!.git*
/vendor/
/test/build/**/*
42 changes: 24 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mindplay/testies
================

[![PHP Version](https://img.shields.io/badge/php-8.1%2B-blue.svg)](https://packagist.org/packages/mindplay/readable)

Yeah, testies: a lightweight library of functions for quick, simple unit-testing.

Tries to honor the [Go language philosophy of testing](http://golang.org/doc/faq#How_do_I_write_a_unit_test) - paraphrasing:
Expand All @@ -9,9 +11,9 @@ Tries to honor the [Go language philosophy of testing](http://golang.org/doc/faq
> mechanisms, but PHP already has all those capabilities; why recreate them? We'd rather write tests in PHP; it's one
> fewer language to learn and the approach keeps the tests straightforward and easy to understand.
The primary test API is procedural-style functions in the `test` namespace.
The primary test API is a set of functions in the `mindplay\testies` namespace.

Internally, the procedural API is backed by a pair of simple driver and configuration classes - these are left
Internally, the API is backed by a pair of simple "driver" and configuration classes - these are left
as open as possible, and you should feel comfortable extending these and tailoring them specifically to suit the
test-requirements for whatever you're testing.

Expand Down Expand Up @@ -52,7 +54,7 @@ exit(run()); // exits with errorlevel (for CI tools etc.)
You can call `test()` as many times as you need to - the tests will queue up, and execute when you call `run()`.


### Procedural API
### API

The following functions are available in the `mindplay\testies` namespace:

Expand All @@ -71,7 +73,7 @@ format($value, $detailed = false); # format a value for use in diagnosti
```

Rather than providing hundreds of assertion functions, you perform assertions using PHP expressions,
often in concert with helper, and built-in standard functions in PHP - some examples:
often in concert with your own helper functions, or built-in standard functions in PHP - some examples:

```php
test(
Expand Down Expand Up @@ -119,7 +121,7 @@ test(
);
```

You can use the same approach to group multiple assertions:
You can use the same approach to group multiple assertions for reuse:

```php
function checkValue($value) {
Expand All @@ -136,35 +138,40 @@ test(
);
```

Keep in mind that diagnostic output always refers to the line number in the test-closure that
Note that the diagnostic output will always refer to the line number in the test-closure that
generated the assertion result.


## Test Server

*This feature is very rough at the moment.*
⚠️ *This feature is still rough.*

As of release 5.4, PHP provides a [built-in development web server](http://php.net/manual/en/features.commandline.webserver.php).
PHP provides a [built-in development web server](http://php.net/manual/en/features.commandline.webserver.php).

For test scenarios where the code needs to run out-of-process (such as [mockery](https://packagist.org/packages/mockery/mockery),
[phake](https://packagist.org/packages/phake/phake) or [phpunit](https://packagist.org/packages/phpunit/phpunit-mock-objects))
or if you need to check the response from a script (e.g. using [guzzle](https://packagist.org/packages/guzzlehttp/guzzle)),
we provide a simple wrapper class to launch and shut down a server:
For basic integration tests, a simple wrapper class to launch and shut down a server is provided - the
following example uses `nyholm/psr7` and the `zaphyr-org/http-client` client library:

```php
use GuzzleHttp\Client;
use Nyholm\Psr7\Factory\Psr17Factory;
use Zaphyr\HttpClient\Client;
use function mindplay\testies\{test, ok, eq};

$server = new TestServer(__DIR__, 8088);

test(
'Can get home page',
function () {
$client = new Client();
$server = new TestServer(__DIR__, 8088);

$http = new Psr17Factory();

$client = new Client($http, $http);

$response = $client->get('http://127.0.0.1:8088/index.php');
$response = $client->sendRequest($http->createRequest("GET", "http://127.0.0.1:8088/index.php"));

eq($response->getStatusCode(), 200);
ok(strpos($response->getBody(), '<title>Welcome</title>') !== false);

ok(strpos($response->getBody(), '<title>Welcome</title>') !== false, "it should capture the response body");
}
);
```
Expand All @@ -179,7 +186,6 @@ of clients, on the other hand, is recommended, as sharing client state could lea

## Options


A few simple configuration options are provided via the `configure()` function, which provides access
to the current instance of `TestConfiguration`.

Expand Down Expand Up @@ -261,7 +267,7 @@ class MyTestDriver extends TestDriver
// ...
}

// Head off your test by selecting your custom driver object:
// Boostrap your test by selecting your custom driver:

configure(new TestConfiguration(new TestDriver));
```
Expand Down
16 changes: 12 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@
}
],
"require": {
"php": "^7.3 || ^8.0"
"php": "^8.1",
"mindplay/readable": "^1.2@dev"
},
"require-dev": {
"phpunit/php-code-coverage": "^9.2.5",
"guzzlehttp/guzzle": "^6.3.3"
"phpunit/php-code-coverage": "^10.1.7",
"nyholm/psr7": "^1.8",
"zaphyr-org/http-client": "^1.0"
},
"suggest": {
"phpunit/php-code-coverage": "^9.2.5"
"phpunit/php-code-coverage": "^10.1.7"
},
"autoload": {
"files": ["src/test.func.php"],
"psr-4": {
"mindplay\\testies\\": "src/"
}
},
"scripts": {
"test": "composer capture && XDEBUG_MODE=coverage php -d 'zend.exception_ignore_args=0' test/test.php",
"example": "XDEBUG_MODE=coverage php -d 'zend.exception_ignore_args=0' test/example.php",
"capture": "XDEBUG_MODE=coverage php -d 'zend.exception_ignore_args=0' test/example.php > test/build/actual-output.txt",
"snapshot": "XDEBUG_MODE=coverage php -d 'zend.exception_ignore_args=0' test/example.php > test/expected-output.txt"
}
}
Loading

0 comments on commit 0a5d138

Please sign in to comment.