Skip to content

Instantly share code, notes, and snippets.

@StanAngeloff
Created March 4, 2015 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StanAngeloff/60356fca93d3de6e1cb1 to your computer and use it in GitHub Desktop.
Save StanAngeloff/60356fca93d3de6e1cb1 to your computer and use it in GitHub Desktop.
Using annotations to control input for PHPUnit test cases

Using annotations in PHPUnit is a non-obtrusive way to control input for test cases. E.g., a custom annotation @httpMockWillRejectWith404 can be added which will instruct a mocked HTTP server to return a 404 code for all requests.

<?php
final class ClassTest extends \PHPUnit_Framework_TestCase
{
# [..]
/**
* {@inheritdoc}
*/
protected function runTest()
{
$annotations = $this->getAnnotations();
if (isset ($annotations['method']['myAnnotation'])) {
# Set up mock objects accordingly...
}
return parent::runTest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment