Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vasekpurchart/phing-tester
Helper tool for writing Phing component tests using PHPUnit
https://github.com/vasekpurchart/phing-tester
phing phpunit testing
Last synced: 3 months ago
JSON representation
Helper tool for writing Phing component tests using PHPUnit
- Host: GitHub
- URL: https://github.com/vasekpurchart/phing-tester
- Owner: VasekPurchart
- License: other
- Created: 2017-03-20T17:06:24.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-06-12T02:58:05.000Z (over 1 year ago)
- Last Synced: 2024-10-14T14:40:50.410Z (3 months ago)
- Topics: phing, phpunit, testing
- Language: PHP
- Size: 53.7 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
Phing Tester
============[Phing](https://github.com/phingofficial/phing/) is a build system which you can extend by writing PHP code. This is really useful, especially for PHP projects so that you do not need any additional technologies and can potentially reuse existing code. But when you are writing such extensions, you should also test them. And because the nature of the build system is printing output, manipulating files and other "system" stuff, then writing isolated tests becomes an issue. Phing Tester should help with this task by providing a way to run Phing targets from PHPUnit tests as if they were run from the command line itself.
Usage
-----With Phing Tester you can write a XML buildfile as usual with Phing and then run its targets from PHP code:
```xml
Lorem ipsum dolor sit amet
```
```php
executeTarget('lorem-ipsum');
```### Testing output
In the example above the target `lorem-ipsum` is executed in the context of the buildfile and then you can check whatever the target was supposed to achieve, in this case it should print out a message. For testing purposes it would be problematic if the actual output would be printed out, so there are Phing Tester method for output checking such as:
```php
assertLogMessage('Lorem ipsum');
````assertLogMessage` searches all log messages if any contains the given string. You can also search for log messages using regexp. It is handy to refine the results by specifying target which should produce the message and/or its priority.
### Testing errors
If the build fails a `\BuildException` is thrown as usual in Phing. If you only want to test the exception and its message you can use standard PHPUnits tools:
```php
expectException(\BuildException::class);
$this->expectExceptionMessage('My error');
```But if you want to also check the state after the target execution, this is not enough since the code in the test is interrupted by the exception. If you want to do that, use `expectFailedBuild` instead of `executeTarget` and you can continue with asserts:
```php
expectFailedBuild($target);$tester->assertLogMessage('Fail message', $target, Project::MSG_ERR);
```If you need to check the thrown exception thoroughly, you can pass a callback to `expectFailedBuild` and you will get the exception as parameter:
```php
expectFailedBuild($target, function (\BuildException $e) use ($target) {
$this->assertRegExp(sprintf('~%s.+not.+exist~', $target), $e->getMessage());
});
```### Testing properties
When writing Phing tests you might want to check the state of some properties or use them for writing other assertions:
```php
assertSame('bar', $tester->getProject()->getProperty('foo'));
```### Convention
There is no enforced convention for using this tool, you can use it in tests however you feel right, but this approach works for me in terms of readability the most:
1) Match one TestCase with one buildfile.
2) Targets in the buildfile represent individual tests and their name matches (if there is no need to reuse the targets or use more targets for one test).
3) When asserting log output, try to be more specific - use target name and output priority to minimalize false positives/negatives.The pair can then look something like:
```xml
Foo
Bar
```
```php
executeTarget($target);$tester->assertLogMessage('Foo', $target, Project::MSG_INFO);
}public function testBar()
{
$tester = new PhingTester(__DIR__ . '/phing-tester-convention-example.xml');
$target = __FUNCTION__;
$tester->executeTarget($target);$tester->assertLogMessage('Bar', $target, Project::MSG_INFO);
}}
```Installation
------------1) Install package [`vasek-purchart/phing-tester`](https://packagist.org/packages/vasek-purchart/phing-tester) with [Composer](https://getcomposer.org/):
```bash
composer require --dev vasek-purchart/phing-tester
```2) Add Phing initialization to your tests bootstrap file ([example in this repo](tests/bootstrap.php)):
```php
Phing::startup();
```