{"id":5646,"date":"2019-10-08T08:48:59","date_gmt":"2019-10-08T13:48:59","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=5646"},"modified":"2019-10-08T08:48:59","modified_gmt":"2019-10-08T13:48:59","slug":"testing-procedural-code-with-phpunit","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=5646","title":{"rendered":"Testing Procedural Code with PHPUnit"},"content":{"rendered":"<p>You <em>can<\/em> use PHPUnit to test procedural code &#8212; in this case, I&#8217;m testing the output of a website. I have some Selenium tests for UI components but wanted to use the shell executor for functional testing. In the test code, you can populate the _SERVER and _POST (or _GET) arrays and simulate the web environment.<\/p>\n<pre>&lt;?php\r\n    namespace phpUnitTests\\CircuitSearch;\r\n    class CircuitExportTest extends \\PHPUnit_Framework_TestCase{\r\n        private function _execute(array $paramsPost = array(), array $paramsServer = array() ) {\r\n            $_POST = $paramsPost;\r\n            $_SERVER = $paramsServer;\r\n            ob_start();\r\n            include \"..\/..\/myWebSitePage.php\";\r\n            return ob_get_clean();\r\n        }\r\n        public function testUsageLogging(){\r\n            $argsPost = array('strInput'=&gt;'SearchValue', 'strReportFormat'=&gt;'JSON');\r\n            $argsServer = array(\"DOCUMENT_ROOT\" =&gt; '\/path\/to\/website\/code\/html\/', \"HOSTNAME\" =&gt; getHostByName(),\r\n                                \"SERVER_ADDR\" =&gt; getHostByName(php_uname('n')), \"PWD\" =&gt; '\/path\/to\/website\/code\/html\/subcomponent\/path');\r\n            $this-&gt;assertEquals('{}', $this-&gt;_execute($argsPost, $argsServer));\r\n        }\r\n    }\r\n?&gt;<\/pre>\n<p>Running the test, my web output is compared to the static string in assertEquals. In this case, I am searching for a non-existent item, nothing is returned, and I <em>expect<\/em> to get empty braces. I could use <a href=\"https:\/\/phpunit.readthedocs.io\/en\/8.3\/assertions.html#assertregexp\" target=\"_blank\" rel=\"noopener noreferrer\">AssertsRegExp<\/a> or or <a href=\"https:\/\/phpunit.readthedocs.io\/en\/8.3\/assertions.html#assertstringcontainsstring\" target=\"_blank\" rel=\"noopener noreferrer\">AssertsStringContainsString<\/a> to verify the specifics of a real result set.<\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=5647\" rel=\"attachment wp-att-5647\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5647\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/10\/PHPUnitTestingResults.png\" alt=\"\" width=\"979\" height=\"261\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/10\/PHPUnitTestingResults.png 979w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/10\/PHPUnitTestingResults-300x80.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/10\/PHPUnitTestingResults-768x205.png 768w\" sizes=\"auto, (max-width: 979px) 100vw, 979px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use PHPUnit to test procedural code &#8212; in this case, I&#8217;m testing the output of a website. I have some Selenium tests for UI components but wanted to use the shell executor for functional testing. In the test code, you can populate the _SERVER and _POST (or _GET) arrays and simulate the web &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[660,35,829,828],"class_list":["post-5646","post","type-post","status-publish","format-standard","hentry","category-coding","tag-ci-cd","tag-php","tag-phpunit","tag-testing"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5646"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5646\/revisions"}],"predecessor-version":[{"id":5648,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5646\/revisions\/5648"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}