Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chadicus/test-helpers
Classes designed to assist with PHPUnit testing
https://github.com/chadicus/test-helpers
Last synced: 4 days ago
JSON representation
Classes designed to assist with PHPUnit testing
- Host: GitHub
- URL: https://github.com/chadicus/test-helpers
- Owner: chadicus
- License: mit
- Created: 2014-04-30T19:57:21.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-07-05T15:00:39.000Z (over 1 year ago)
- Last Synced: 2023-12-08T16:07:26.659Z (11 months ago)
- Language: PHP
- Size: 649 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Chadicus Test Helpers
[![Latest Stable Version](https://poser.pugx.org/chadicus/test-helpers/v/stable)](https://packagist.org/packages/chadicus/test-helpers)
[![Latest Unstable Version](https://poser.pugx.org/chadicus/test-helpers/v/unstable)](https://packagist.org/packages/chadicus/test-helpers)
[![License](https://poser.pugx.org/chadicus/test-helpers/license)](https://packagist.org/packages/chadicus/test-helpers)[![Total Downloads](https://poser.pugx.org/chadicus/test-helpers/downloads)](https://packagist.org/packages/chadicus/test-helpers)
[![Daily Downloads](https://poser.pugx.org/chadicus/test-helpers/d/daily)](https://packagist.org/packages/chadicus/test-helpers)
[![Monthly Downloads](https://poser.pugx.org/chadicus/test-helpers/d/monthly)](https://packagist.org/packages/chadicus/test-helpers)## Requirements
Test Helpers requires PHP 7.3 (or later).
## Composer
To add the library as a local, per-project dependency use [Composer](http://getcomposer.org)! Simply add a dependency on
`chadicus/test-helpers` to your project's `composer.json` file such as:```sh
composer require --dev chadicus/test-helpers
```NOTE: test-helpers should never be used in production. They are meant for testing enviornments only.
## Documentation
PHP docs for the project can be found [here](http://chadicus.github.io/test-helpers).## Contact
Developers may be contacted at:* [Pull Requests](https://github.com/chadicus/test-helpers/pulls)
* [Issues](https://github.com/chadicus/test-helpers/issues)## Project Build
With a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run:```sh
composer install
composer run test
composer run lint
```
# \Chadicus\FunctionRegistrySome internal PHP functions are documented to return certain values on failure. If you're a meticulous programmer you want to account for these return values in your code and respond to them accordingly.
```php
class MyClass
{
public function doSomething()
{
$curl = curl_init();
if ($curl === false) {
throw new \Exception('curl_init() failed');
}//do something with $curl ...
}
}
```A meticulous programmer may also want to ensure their unit test code coverage is 100%.
One way to accomplish this is to use @codeCoverageIgnore annotations
```php
class MyClass
{
public function doSomething()
{
$curl = curl_init();
if ($curl === false) {
//@codeCoverageIgnoreStart
throw new \Exception('curl_init() failed');
//@codeCoverageIgnoreEnd
}//do something with $curl ...
}
}
```This gets us the code coverage but the code isn't really tested.
The `FunctionRegistry` class alows you to _mock_ an internal PHP function
```php
class MyClassTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
// prepare the curl functions for mocking
\Chadicus\FunctionRegistry::reset(__NAMESPACE__, array('curl'));
}/**
* @expectedExceptionMessage curl_init() failed
*/
public function testCurlInitFails()
{
\Chadicus\FunctionRegistry::set(
__NAMESPACE__,
'curl_init',
function () {
return false;
}
);$myClass = new MyClass();
// this will call our custom curl_init function
$myClass->doSomething();
}
}
```For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist. It is because of this behavior that we can _mock_ internal functions.