{"id":15284054,"url":"https://github.com/vasekpurchart/phing-tester","last_synced_at":"2025-04-12T23:21:10.279Z","repository":{"id":18930627,"uuid":"85604009","full_name":"VasekPurchart/Phing-Tester","owner":"VasekPurchart","description":"Helper tool for writing Phing component tests using PHPUnit","archived":false,"fork":false,"pushed_at":"2025-03-10T02:14:44.000Z","size":64,"stargazers_count":3,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T17:21:18.917Z","etag":null,"topics":["phing","phpunit","testing"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VasekPurchart.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-20T17:06:24.000Z","updated_at":"2024-12-04T13:00:07.000Z","dependencies_parsed_at":"2024-12-04T11:24:40.299Z","dependency_job_id":"bfe05d85-658c-4ff5-88a1-790b26b73e58","html_url":"https://github.com/VasekPurchart/Phing-Tester","commit_stats":{"total_commits":50,"total_committers":1,"mean_commits":50.0,"dds":0.0,"last_synced_commit":"42624b74c121c5842b3bb4c51c67c77b8bd18742"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VasekPurchart%2FPhing-Tester","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VasekPurchart%2FPhing-Tester/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VasekPurchart%2FPhing-Tester/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VasekPurchart%2FPhing-Tester/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VasekPurchart","download_url":"https://codeload.github.com/VasekPurchart/Phing-Tester/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248644141,"owners_count":21138561,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["phing","phpunit","testing"],"created_at":"2024-09-30T14:48:55.413Z","updated_at":"2025-04-12T23:21:10.255Z","avatar_url":"https://github.com/VasekPurchart.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Phing Tester\n============\n\n[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.\n\nUsage\n-----\n\nWith Phing Tester you can write a XML buildfile as usual with Phing and then run its targets from PHP code:\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003c!-- phing-tester-example.xml --\u003e\n\u003cproject name=\"PhingTesterExample\" default=\"lorem-ipsum\"\u003e\n\n\t\u003ctarget name=\"lorem-ipsum\"\u003e\n\t\t\u003cecho\u003eLorem ipsum dolor sit amet\u003c/echo\u003e\n\t\u003c/target\u003e\n\n\u003c/project\u003e\n```\n\n```php\n\u003c?php\n\nuse VasekPurchart\\Phing\\PhingTester\\PhingTester;\n\n$tester = new PhingTester(__DIR__ . '/phing-tester-example.xml');\n$tester-\u003eexecuteTarget('lorem-ipsum');\n```\n\n### Testing output\n\nIn 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:\n\n```php\n\u003c?php\n\n$tester-\u003eassertLogMessage('Lorem ipsum');\n```\n\n`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.\n\n### Testing errors\n\nIf 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:\n\n```php\n\u003c?php\n\n$this-\u003eexpectException(\\BuildException::class);\n$this-\u003eexpectExceptionMessage('My error');\n```\n\nBut 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:\n\n```php\n\u003c?php\n\n$tester-\u003eexpectFailedBuild($target);\n\n$tester-\u003eassertLogMessage('Fail message', $target, Project::MSG_ERR);\n```\n\nIf you need to check the thrown exception thoroughly, you can pass a callback to `expectFailedBuild` and you will get the exception as parameter:\n\n```php\n\u003c?php\n\n$tester-\u003eexpectFailedBuild($target, function (\\BuildException $e) use ($target) {\n\t$this-\u003eassertRegExp(sprintf('~%s.+not.+exist~', $target), $e-\u003egetMessage());\n});\n```\n\n### Testing properties\n\nWhen writing Phing tests you might want to check the state of some properties or use them for writing other assertions:\n\n```php\n\u003c?php\n\n$this-\u003eassertSame('bar', $tester-\u003egetProject()-\u003egetProperty('foo'));\n```\n\n### Convention\n\nThere 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:\n\n1) Match one TestCase with one buildfile.\n2) 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).\n3) When asserting log output, try to be more specific - use target name and output priority to minimalize false positives/negatives.\n\nThe pair can then look something like:\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003c!-- phing-tester-convention-example.xml --\u003e\n\u003cproject name=\"PhingTesterConventionExample\" default=\"test\"\u003e\n\n\t\u003ctarget name=\"testFoo\"\u003e\n\t\t\u003cecho\u003eFoo\u003c/echo\u003e\n\t\u003c/target\u003e\n\n\t\u003ctarget name=\"testBar\"\u003e\n\t\t\u003cecho\u003eBar\u003c/echo\u003e\n\t\u003c/target\u003e\n\n\u003c/project\u003e\n```\n\n```php\n\u003c?php\n\nuse VasekPurchart\\Phing\\PhingTester\\PhingTester;\n\nclass PhingTesterConventionExampleTest extends \\PHPUnit\\Framework\\TestCase\n{\n\n\tpublic function testFoo()\n\t{\n\t\t$tester = new PhingTester(__DIR__ . '/phing-tester-convention-example.xml');\n\t\t$target = __FUNCTION__;\n\t\t$tester-\u003eexecuteTarget($target);\n\n\t\t$tester-\u003eassertLogMessage('Foo', $target, Project::MSG_INFO);\n\t}\n\n\tpublic function testBar()\n\t{\n\t\t$tester = new PhingTester(__DIR__ . '/phing-tester-convention-example.xml');\n\t\t$target = __FUNCTION__;\n\t\t$tester-\u003eexecuteTarget($target);\n\n\t\t$tester-\u003eassertLogMessage('Bar', $target, Project::MSG_INFO);\n\t}\n\n}\n```\n\nInstallation\n------------\n\n1) Install package [`vasek-purchart/phing-tester`](https://packagist.org/packages/vasek-purchart/phing-tester) with [Composer](https://getcomposer.org/):\n\n```bash\ncomposer require --dev vasek-purchart/phing-tester\n```\n\n2) Add Phing initialization to your tests bootstrap file ([example in this repo](tests/bootstrap.php)):\n\n```php\nPhing::startup();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvasekpurchart%2Fphing-tester","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvasekpurchart%2Fphing-tester","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvasekpurchart%2Fphing-tester/lists"}