{"id":807,"date":"2012-12-03T13:23:16","date_gmt":"2012-12-03T13:23:16","guid":{"rendered":"http:\/\/www.nathankowald.com\/blog\/?p=807"},"modified":"2016-11-13T16:58:50","modified_gmt":"2016-11-13T16:58:50","slug":"how-to-unit-test-private-methods-in-php","status":"publish","type":"post","link":"https:\/\/www.nathankowald.com\/blog\/2012\/12\/how-to-unit-test-private-methods-in-php\/","title":{"rendered":"Unit test private methods in PHP"},"content":{"rendered":"<p><strong>Requires:<\/strong> PHP 5.3.2 or greater.<br \/>\nIf you&#8217;re not running 5.3.2+ you can test private methods indirectly by creating a public method, that calls your private method.<\/p>\n<div style=\"padding: 7px; background-color: #f3fceb; border: 2px solid #92DE7E; font-size:1.2em;\">\n<a href=\"#solution\">&darr; Jump to solution<\/a>\n<\/div>\n<h2>Problem<\/h2>\n<p>We have a class, <strong>StatFinder<\/strong> which contains a private method, <strong>getDomainFromEmail()<\/strong> that we want to test.<\/p>\n<pre class=\"prettyprint lang-php\">\r\n&lt;?php\r\nclass StatFinder\r\n{\r\n    \/\/ Get domain from email address\r\n    private function getDomainFromEmail($email='')\r\n    {\r\n        if ($email == '') return '';\r\n        $parts = explode('@', $email);\r\n        if ($parts === false || !isset($parts[1])) return '';\r\n\r\n        return $parts[1];\r\n    }\r\n}<\/pre>\n<p>We create a test to check that calling <strong>getEmailFromDomain(&#8216;iliketurtles@gmail.com&#8217;)<\/strong> returns the expected &#8216;<strong>gmail.com<\/strong>&#8216;. I&#8217;m using <a href=\"http:\/\/www.phpunit.de\">PHPUnit<\/a> to run the unit test.<\/p>\n<pre class=\"prettyprint lang-php\">\r\n&lt;?php\r\ninclude('..\/libraries\/StatFinder.php');\r\n\r\nclass StatFinderTest extends PHPUnit_Framework_TestCase\r\n{\r\n    public function testGetDomainFromEmail()\r\n    {\r\n        $sf = new StatFinder();\r\n        $email = 'iliketurtles@gmail.com';\r\n        $expected = 'gmail.com';\r\n        $this->assertEquals($expected, $sf->getDomainFromEmail($email));\r\n    }\r\n}<\/pre>\n<p>When run, the test fails because the method is private, so only accessible by the class that defined it.<\/p>\n<p><a href=\"http:\/\/www.nathankowald.com\/blog\/wp-content\/uploads\/2012\/12\/failed-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-854\" title=\"failed-test\" src=\"http:\/\/www.nathankowald.com\/blog\/wp-content\/uploads\/2012\/12\/failed-test-1024x171.png\" alt=\"\" width=\"614\" height=\"103\" srcset=\"https:\/\/www.nathankowald.com\/blog\/wp-content\/uploads\/2012\/12\/failed-test-1024x171.png 1024w, https:\/\/www.nathankowald.com\/blog\/wp-content\/uploads\/2012\/12\/failed-test-300x50.png 300w, https:\/\/www.nathankowald.com\/blog\/wp-content\/uploads\/2012\/12\/failed-test.png 1263w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/a><br \/>\n<em>Failed unit test &ndash; (PHPUnit running in PhpStorm)<\/em><br \/>\n<a name=\"solution\"><\/a><\/p>\n<h2>Solution<\/h2>\n<p>You can test private and protected methods by using the ReflectionMethod class, part of the <a href=\"http:\/\/php.net\/manual\/en\/book.reflection.php\">Reflection API<\/a> that comes with PHP 5.<br \/>\nThe <a href=\"http:\/\/php.net\/manual\/en\/reflectionmethod.setaccessible.php\">setAccessible<\/a> method of the ReflectionMethod class is only available from PHP 5.3.2 up so you&#8217;ll have to be running at least this to use this.<\/p>\n<pre class=\"prettyprint lang-php\">\r\n&lt;?php\r\ninclude('..\/libraries\/StatFinder.php');\r\n\r\nclass StatFinderTest extends PHPUnit_Framework_TestCase\r\n{\r\n    public function testGetDomainFromEmail()\r\n    {\r\n        $method = new ReflectionMethod('StatFinder', 'getDomainFromEmail');\r\n        $method->setAccessible(true);\r\n\r\n        $email = 'iliketurtles@gmail.com';\r\n        $expected = 'gmail.com';\r\n        $this->assertEquals($expected, $method->invoke(new StatFinder, $email));\r\n    }\r\n}<\/pre>\n<h2>Explanation<\/h2>\n<pre class=\"prettyprint lang-php\">\r\n&lt;?php\r\n$method = new ReflectionMethod('StatFinder', 'getDomainFromEmail');<\/pre>\n<p>This creates an instance of the ReflectionMethod class.<br \/>\nThe first argument, <strong>StatFinder<\/strong> is the class that contains the private method to test.<br \/>\nThe second argument, <strong>getDomainFromEmail<\/strong> is the method to test.<\/p>\n<pre class=\"prettyprint lang-php\">\r\n&lt;?php\r\n$method->setAccessible(true);<\/pre>\n<p><strong>setAccessible(true)<\/strong> allows the private method, <strong>getDomainFromEmail<\/strong> to be called from inside the test.<\/p>\n<pre class=\"prettyprint lang-php\">\r\n&lt;?php\r\n$this->assertEquals($expected, $method->invoke(new StatFinder, $email));<\/pre>\n<p><strong>invoke()<\/strong> invokes the private method.<br \/>\nThe first argument, <strong>new StatFinder<\/strong> is the object to invoke the method on. For static methods, pass null.<br \/>\nThe second argument, <strong>$email<\/strong> is the argument to pass to the method. Zero or more arguments can be passed to the method this way.<\/p>\n<p>We run our updated test and it now works.<\/p>\n<p><a href=\"http:\/\/www.nathankowald.com\/blog\/wp-content\/uploads\/2012\/12\/successful-test.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.nathankowald.com\/blog\/wp-content\/uploads\/2012\/12\/successful-test.png\" alt=\"\" title=\"successful-test\" width=\"467\" height=\"207\" class=\"alignnone size-full wp-image-891\" srcset=\"https:\/\/www.nathankowald.com\/blog\/wp-content\/uploads\/2012\/12\/successful-test.png 467w, https:\/\/www.nathankowald.com\/blog\/wp-content\/uploads\/2012\/12\/successful-test-300x132.png 300w\" sizes=\"auto, (max-width: 467px) 100vw, 467px\" \/><\/a><br \/>\n<em>Successful test<\/em><\/p>\n<h2>Related<\/h2>\n<ul class=\"lovedarticles\">\n<li><a href=\"http:\/\/sebastian-bergmann.de\/archives\/881-Testing-Your-Privates.html\">Testing Your Privates &#8211; Sebastian Bergmann<\/a><\/li>\n<li><a href=\"http:\/\/www.phptherightway.com\/#testing\">PHP: The Right Way &#8211; Testing<\/a><\/li>\n<li><a href=\"http:\/\/programmer.97things.oreilly.com\/wiki\/index.php\/The_Three_Laws_of_Test-Driven_Development\">The Three Laws of Test-Driven Development<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/105007\/should-i-test-private-methods-or-only-public-ones\">Should I test private methods or only public ones? &#8211; Stack Overflow<\/a>\n<li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>You can test private and protected methods by using the ReflectionMethod class, part of the <a href=\"http:\/\/php.net\/manual\/en\/book.reflection.php\">Reflection API<\/a> that comes with PHP 5.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[20,7],"tags":[],"class_list":["post-807","post","type-post","status-publish","format-standard","hentry","category-unit-testing","category-web-development"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/comments?post=807"}],"version-history":[{"count":116,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/807\/revisions"}],"predecessor-version":[{"id":1770,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/807\/revisions\/1770"}],"wp:attachment":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/media?parent=807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/categories?post=807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/tags?post=807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}