{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Unit Testing","type":"basic","slug":"supporting-structure-unit-testing","excerpt":"","body":"# Unit Testing\n\nUnit testing and along functional testing is where we test the stability of our code. We've imported the PHPUnit as the main unit testing tool of Slayer.\n\n## Index\n- [Example Scenario](#example-scenario)\n- [The Tool](#the-tool)\n\n\n---\n\n\n<a name=\"example-scenario\"></a>\n# Example Scenario\n\nLet us assume that we have a large project that doesn't apply any Unit/Functional Testing or any kind of BDD/TDD/DDD implementations.\n\nNow you modified a certain class, and that class is our \"Model\", let us assume we have User class, which handles the login process, then we changed the field from ``email`` to ``username``.\n\nNow you committed your code and you didn't check if it affects some of your components. Now from our controller, we still have the query who relies on ``email`` field, you've accidentally deployed your code from the live/production website, now that's the problem...\n\nTo eliminate this kind of process, while your project is still small, we suggests to apply a unit testing upon adding a new feature in your code.\n\n\n---\n\n\n<a name=\"the-tool\"></a>\n# The Tool\n\nHave you heard about PHPUnit, Behat, PHPSpec, Codeception? Great! Basically, we can't push all those testing tools on our project. However, we initially added PHPUnit as it is more minimal and the most/majority to use of them all.\n\nIn your project folder, there is a folder ``tests`` which will be your testing folder, you must have a ``phpunit.xml`` where in the settings lives in it.\n\nBy default, all the test classes must have a suffix ``Test.php``, for example we can rename ``ControllersTest.php``. The default function must have prefix ``test``, something like ``testUserLogin()``, ``testTheUserLists()`` and etc.\n\nrun this to your console pointing to root project:\n```shell\n./vendor/bin/phpunit\n```\n\nor:\n\n```shell\n./vendor/bin/phpunit -c ./phpunit.xml\n```\n\nTry to explore more by reading the [PHPUnit documentation](https://phpunit.de/manual/current/en/index.html)","updates":[],"order":5,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"56e26e7d7d54ba0e004635a3","parentDoc":null,"project":"56c111095abfe40d00be875a","__v":10,"createdAt":"2016-03-11T07:06:37.943Z","user":"56c1105874f0b417004baadc","version":{"version":"1.3.0","version_clean":"1.3.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["56c1110a5abfe40d00be875e","56c413a254b6030d00ec299d","56c4275048213b1700af6e33","56c42826c0c4630d004e86cb","56c4282cbc41330d009f2607","56c4284ad1f6d91700d3697e","56e271c195d1c60e00a969ee"],"_id":"56c111095abfe40d00be875d","releaseDate":"2016-02-14T23:43:05.566Z","__v":7,"createdAt":"2016-02-14T23:43:05.566Z","project":"56c111095abfe40d00be875a"},"category":{"sync":{"isSync":false,"url":""},"pages":["56e26e3a8d79b50e0031d8f6","56e26e42353e060e00b96826","56e26e4d95d1c60e00a969db","56e26e555ab0871700957c04","56e26e6f5ab0871700957c06","56e26e7d7d54ba0e004635a3","56e26e85353e060e00b96828"],"title":"Supporting Structure","slug":"supporting-structure","order":3,"from_sync":false,"reference":false,"_id":"56c42826c0c4630d004e86cb","__v":7,"createdAt":"2016-02-17T07:58:30.542Z","project":"56c111095abfe40d00be875a","version":"56c111095abfe40d00be875d"},"githubsync":""}
# Unit Testing Unit testing and along functional testing is where we test the stability of our code. We've imported the PHPUnit as the main unit testing tool of Slayer. ## Index - [Example Scenario](#example-scenario) - [The Tool](#the-tool) --- <a name="example-scenario"></a> # Example Scenario Let us assume that we have a large project that doesn't apply any Unit/Functional Testing or any kind of BDD/TDD/DDD implementations. Now you modified a certain class, and that class is our "Model", let us assume we have User class, which handles the login process, then we changed the field from ``email`` to ``username``. Now you committed your code and you didn't check if it affects some of your components. Now from our controller, we still have the query who relies on ``email`` field, you've accidentally deployed your code from the live/production website, now that's the problem... To eliminate this kind of process, while your project is still small, we suggests to apply a unit testing upon adding a new feature in your code. --- <a name="the-tool"></a> # The Tool Have you heard about PHPUnit, Behat, PHPSpec, Codeception? Great! Basically, we can't push all those testing tools on our project. However, we initially added PHPUnit as it is more minimal and the most/majority to use of them all. In your project folder, there is a folder ``tests`` which will be your testing folder, you must have a ``phpunit.xml`` where in the settings lives in it. By default, all the test classes must have a suffix ``Test.php``, for example we can rename ``ControllersTest.php``. The default function must have prefix ``test``, something like ``testUserLogin()``, ``testTheUserLists()`` and etc. run this to your console pointing to root project: ```shell ./vendor/bin/phpunit ``` or: ```shell ./vendor/bin/phpunit -c ./phpunit.xml ``` Try to explore more by reading the [PHPUnit documentation](https://phpunit.de/manual/current/en/index.html)