|
| 1 | +How to write a test case |
| 2 | +======================== |
| 3 | + |
| 4 | +We are using [PHPUnit][] for testing. |
| 5 | + |
| 6 | +[PHPUnit]:https://github.com/sebastianbergmann/phpunit/ |
| 7 | + |
| 8 | +This directory contains test cases for phpctags. The structure is shown as |
| 9 | +below. |
| 10 | + |
| 11 | +``` |
| 12 | +tests/ |
| 13 | + |--examples/ |
| 14 | + | |-- <example_identifier>.example.php |
| 15 | + | |-- <example_identifier>.example.define.php |
| 16 | + | |-- ... |
| 17 | + |--testcases/ |
| 18 | + | |-- <testcase_identifier>.testcase.php |
| 19 | + | |-- ... |
| 20 | + |--bootstrap.php |
| 21 | + |--PHPCtagsTest.php |
| 22 | + |--PHPCtagsTestCase.php |
| 23 | +```` |
| 24 | +
|
| 25 | +`examples/` directory holds the example file for testing named as |
| 26 | +`<example_identifier>.example.php` and its token definition file named |
| 27 | +as `<example_identifier>.example.define.php`. The `<example_identifier>` |
| 28 | +should be unique and only contians characters in the range of `[0-9a-zA-Z_]`, |
| 29 | +especially should not contain a dot. |
| 30 | +
|
| 31 | +File `<example_identifiler>.example.define.php` contains only one function |
| 32 | +named as `e_<example_identifier>_define` which returns an array contains |
| 33 | +information about tokens in the corresponding example file. This array consists |
| 34 | +of a set of arrays ordered by the occurrence order of the tokens in the example |
| 35 | +file. Each of these arrays consits of the following keys. |
| 36 | +
|
| 37 | +* `name` |
| 38 | +
|
| 39 | + Required, name of the token |
| 40 | +
|
| 41 | +* `line` |
| 42 | +
|
| 43 | + Required, line number of the token |
| 44 | +
|
| 45 | +* `kind` |
| 46 | +
|
| 47 | + Required, kind descriptor of the token in one character. |
| 48 | + For a full definition kind descriptor, please see `PHPCtags::$mKinds`. |
| 49 | +
|
| 50 | +* `scope` |
| 51 | +
|
| 52 | + Optional, scope definition of the token |
| 53 | +
|
| 54 | +* `access` |
| 55 | +
|
| 56 | + Optional, access restriction for the token |
| 57 | +
|
| 58 | +For example, assuming we have the following code in an example file named as |
| 59 | +`foo.example.php`. |
| 60 | +
|
| 61 | +``` |
| 62 | +<?php |
| 63 | +class Foo { |
| 64 | + public function bar() { |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | +
|
| 69 | +Then, our `foo.example.define.php` should contain the following code which |
| 70 | +reflect the token information about the example code above and will be used |
| 71 | +to generate the expected result. |
| 72 | +
|
| 73 | +``` |
| 74 | +<?php |
| 75 | +function e_foo_define() |
| 76 | +{ |
| 77 | + return array( |
| 78 | + array( |
| 79 | + 'name'=>'Foo', |
| 80 | + 'line'=>'2', |
| 81 | + 'kind'=>'c', |
| 82 | + ), |
| 83 | + array( |
| 84 | + 'name'=>'bar', |
| 85 | + 'line'=>'3', |
| 86 | + 'kind'=>'m', |
| 87 | + 'scope'=>'class:Foo', |
| 88 | + 'access'=>'public', |
| 89 | + ), |
| 90 | + ); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +After finishing adding the example files, it is the time to add a test case for |
| 95 | +this example. All test cases are located at `testcases/` directory and named |
| 96 | +as `<testcase_identifier>.<testcase_description>.testcase.php`. All test case |
| 97 | +classes should extend from the basic abstract class `PHPCtagsTestCase` and be |
| 98 | +named as `t_<testcase_identifier>`. Only `$mExample` property must be assigned |
| 99 | +in the `__construt` function of the test case class. The other two properties |
| 100 | +`$mFormat` and `$mOptions` have default value predifined in the `__construct` |
| 101 | +function of the base `PHPCtagsTestCase` class which could be easily overwrited. |
| 102 | + |
| 103 | +Here is the test case class for the previous example and should be stored in |
| 104 | +file `test_foo.testcase.php`. |
| 105 | + |
| 106 | +``` |
| 107 | +<?php |
| 108 | +class t_test_foo extends PHPCtagsTestCase { |
| 109 | +
|
| 110 | + public function __construct() |
| 111 | + { |
| 112 | + parent::__construct(); |
| 113 | + $this->mExample = 'foo'; |
| 114 | + } |
| 115 | + |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +There is no naming restriction for `<example_identifier>` and |
| 120 | +`<testcase_identifier>`. Generally, the identifier for a bug fix reported in |
| 121 | +the issue list should be named as `bugfix_<issue number>`. |
| 122 | + |
| 123 | +We are done now. Now the `PHPCtagsTest` class will automatically find these |
| 124 | +test cases and use them to feed the `testExport()` method by a means called |
| 125 | +__[Parameterized Test][]__. |
| 126 | + |
| 127 | +[Parameterized Test]:http://xunitpatterns.com/Parameterized%20Test.html |
0 commit comments