nystudio107 /
craft-twig-sandbox
| 1 | <?php |
||
| 2 | |||
| 3 | use craft\events\ExceptionEvent; |
||
| 4 | use craft\web\ErrorHandler; |
||
| 5 | use markhuot\craftpest\test\TestCase; |
||
| 6 | use yii\base\Event; |
||
|
0 ignored issues
–
show
|
|||
| 7 | |||
| 8 | /* |
||
| 9 | |-------------------------------------------------------------------------- |
||
| 10 | | Test Case |
||
| 11 | |-------------------------------------------------------------------------- |
||
| 12 | | |
||
| 13 | | The closure you provide to your test functions is always bound to a specific PHPUnit test |
||
| 14 | | case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may |
||
| 15 | | need to change it using the "uses()" function to bind a different classes or traits. |
||
| 16 | | |
||
| 17 | */ |
||
| 18 | |||
| 19 | uses(TestCase::class) |
||
| 20 | ->beforeEach(function() { |
||
| 21 | // Ensure exceptions are thrown, so we can catch them in our tests. |
||
| 22 | Event::on(ErrorHandler::class, ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, |
||
| 23 | function(ExceptionEvent $event) { |
||
| 24 | throw $event->exception; |
||
| 25 | } |
||
| 26 | ); |
||
| 27 | }) |
||
| 28 | ->in('./'); |
||
| 29 | |||
| 30 | /* |
||
| 31 | |-------------------------------------------------------------------------- |
||
| 32 | | Expectations |
||
| 33 | |-------------------------------------------------------------------------- |
||
| 34 | | |
||
| 35 | | When you're writing tests, you often need to check that values meet certain conditions. The |
||
| 36 | | "expect()" function gives you access to a set of "expectations" methods that you can use |
||
| 37 | | to assert different things. Of course, you may extend the Expectation API at any time. |
||
| 38 | | |
||
| 39 | */ |
||
| 40 | |||
| 41 | /* |
||
| 42 | |-------------------------------------------------------------------------- |
||
| 43 | | Constants |
||
| 44 | |-------------------------------------------------------------------------- |
||
| 45 | */ |
||
| 46 | |||
| 47 | /* |
||
| 48 | |-------------------------------------------------------------------------- |
||
| 49 | | Functions |
||
| 50 | |-------------------------------------------------------------------------- |
||
| 51 | | |
||
| 52 | | While Pest is very powerful out-of-the-box, you may have some testing code specific to your |
||
| 53 | | project that you don't want to repeat in every file. Here you can also expose helpers as |
||
| 54 | | global functions to help you to reduce the number of lines of code in your test files. |
||
| 55 | | |
||
| 56 | */ |
||
| 57 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: