{"id":1561,"date":"2021-05-30T19:08:10","date_gmt":"2021-05-30T19:08:10","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=1561"},"modified":"2021-05-31T17:46:28","modified_gmt":"2021-05-31T17:46:28","slug":"phpunit","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/php\/phpunit\/","title":{"rendered":"PHPUnit"},"content":{"rendered":"\n<p>To install phpunit framework make sure you have installed composer<br>If not follow instructions from this link <a rel=\"noreferrer noopener\" href=\"http:\/\/codeinsightacademy.com\/blog\/php\/composer\/\" target=\"_blank\">http:\/\/codeinsightacademy.com\/blog\/php\/composer\/<\/a><\/p>\n\n\n\n<h3 class=\"has-text-align-center\">Installation<\/h3>\n\n\n\n<p>Install php-xml<br>As we are going to use phpunit.xml we need to install php-xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install php-xml\n#To install specific version\nsudo apt install php7.4-xml<\/code><\/pre>\n\n\n\n<p>Install mbstring<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install php-mbstring<\/code><\/pre>\n\n\n\n<p>Create composer.json file in project root directory<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\"autoload\" : {}\n}<\/code><\/pre>\n\n\n\n<p>Run one of the composer command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer dump-autoload -o\nOR\ncomposer update<\/code><\/pre>\n\n\n\n<p>Install sluggable<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require cviebrock\/eloquent-sluggable<\/code><\/pre>\n\n\n\n<p>Install intl<br>This is required when you are using Code Igniter framework<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install php-intl\n#OR specific version\napt-get install php7.4-intl<\/code><\/pre>\n\n\n\n<p>Install php unit framework<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require phpunit\/phpunit ^9<\/code><\/pre>\n\n\n\n<h3 class=\"has-text-align-center\">Create function<\/h3>\n\n\n\n<p>global_functions.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nfunction add($x = 0, $y = 0) {\n        return $x + $y;\n}<\/code><\/pre>\n\n\n\n<p>index.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nrequire_once(\"vendor\/autoload.php\");\n\necho add(5, 6);<\/code><\/pre>\n\n\n\n<p>composer.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n <strong>   \"autoload\": {\n            \"files\": &#91;\"global_functions.php\"]\n    },<\/strong>\n    \"require\": {\n        \"cviebrock\/eloquent-sluggable\": \"^8.0\",\n        \"phpunit\/phpunit\": \"^9\"\n    }\n}<\/code><\/pre>\n\n\n\n<p>Run index.php file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php index.php<\/code><\/pre>\n\n\n\n<h3 class=\"has-text-align-center\">Write Testcase<\/h3>\n\n\n\n<p>phpunit.xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n\n&lt;phpunit bootstrap = \"vendor\/autoload.php\"\n colors = \"true\"&gt;\n    &lt;testsuites&gt;\n        &lt;testsuite name=\"Sample test suite\"&gt;\n            <strong>&lt;directory&gt;tests&lt;\/directory&gt;<\/strong>\n        &lt;\/testsuite&gt;\n    &lt;\/testsuites&gt;\n\n&lt;\/phpunit&gt;<\/code><\/pre>\n\n\n\n<p>tests\/GlobalFunctionsTest.php<br>NOTE: MAKE SURE TO GIVE test PREFIX TO YOUR TEST FUNCTION<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nclass GlobalFunctionsTest extends \\PHPUnit\\Framework\\TestCase {\n        public function <strong>test<\/strong>Add() {\n                $result = add(5, 6);\n                $this-&gt;assertEquals(11, $result);\n        }\n}<\/code><\/pre>\n\n\n\n<p>Get all assertions functions from here<br><a rel=\"noreferrer noopener\" href=\"https:\/\/phpunit.readthedocs.io\/en\/9.5\/assertions.html\" target=\"_blank\">https:\/\/phpunit.readthedocs.io\/en\/9.5\/assertions.html<\/a><\/p>\n\n\n\n<p>Run TestCase from root directory<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/vendor\/bin\/phpunit<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-22.png\"><img loading=\"lazy\" width=\"739\" height=\"157\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-22.png\" alt=\"\" class=\"wp-image-1562\" srcset=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-22.png 739w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-22-300x64.png 300w\" sizes=\"(max-width: 739px) 100vw, 739px\" \/><\/a><figcaption>testcase output<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/vendor\/bin\/phpunit --testdox<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-23.png\"><img loading=\"lazy\" width=\"820\" height=\"172\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-23.png\" alt=\"\" class=\"wp-image-1563\" srcset=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-23.png 820w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-23-300x63.png 300w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-23-768x161.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption>output with function names and result<\/figcaption><\/figure>\n\n\n\n<p>project directory structure<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tree -I \"vendor\"<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-24.png\"><img loading=\"lazy\" width=\"299\" height=\"166\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/05\/image-24.png\" alt=\"\" class=\"wp-image-1564\"\/><\/a><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>To install phpunit framework make sure you have installed composerIf not follow instructions from this link http:\/\/codeinsightacademy.com\/blog\/php\/composer\/ Installation Install php-xmlAs we are going to use phpunit.xml we need to install php-xml Install mbstring Create composer.json file in project root directory Run one of the composer command Install sluggable Install intlThis is required when you are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1561"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=1561"}],"version-history":[{"count":5,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1561\/revisions"}],"predecessor-version":[{"id":1576,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1561\/revisions\/1576"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=1561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=1561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=1561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}