Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer the question in the title, <a href="http://book.cakephp.org/2.0/en/development/testing.html#viewing-code-coverage" rel="nofollow">CakePHP can use XDebug </a> to pull out your test's code coverage. If I remember correctly, it embeds the code coverage render into the test suite.</p> <p>As a general comment on the example you've given above, IMHO I would even go to the extent of testing the controller when no option is passed to ensure it responds in the manner expected, even if it throws an exception.</p> <h3>Update</h3> <p>My apologies for not reading @Nick's question properly.</p> <p>To my knowledge, CakePHP doesn't pull up the code coverage of <code>.ctp</code> files. That does not mean however the XDebug doesn't generate it and you can probably use your IDE (I use PhpStorm which has a <a href="http://blog.jetbrains.com/webide/2012/02/new-in-4-0-code-coverage-for-phpunit/" rel="nofollow">tool</a>) or the XDebug itself to pull back the code coverage directly. Granted, this doesn't make testing as smooth as using the test suite within CakePHP alone.</p> <p>Alternatively, you can test against the rendered view. According to the <a href="http://book.cakephp.org/2.0/en/development/testing.html#choosing-the-return-type" rel="nofollow">documentation</a> it is possible to specify the return type when testing actions. Therefore one possible solution would be compare either the <code>view</code> or <code>contents</code> against a pre-rendered file. There is a danger with this however because any invisible characters (<code>'\n', '\r', '\t'</code>) may cause the assert to fail even though logically, the target and result markup are identical.</p> <p>One example in the <a href="http://book.cakephp.org/2.0/en/development/testing.html#a-more-complex-example" rel="nofollow">documentation</a> shows how you can assert values using regex allowing you to inspect specific areas of the document for validity. You could also use PHP's <a href="http://www.php.net/manual/en/book.dom.php" rel="nofollow">DOM classes</a> to traverse the document instead.</p> <p>Good luck!</p> <h3>Update 21:21 09/05/2013</h3> <p>CakePHP's test suite is build upon PHP Unit which in turn, uses XDebug to generate code coverage. I had a look around the source and inspected the <code>BaseCoverageReport</code> class and discovered that coverage is generated for any framework files used running the test, including the unit under test (e.g. your controller). This suggests to me that code coverage is not selectively turned on and that it is generated for all files including the view template. With that said, I failed to find the template as one of the files it had generated code coverage for. However, I did note that code coverage wasn't generated for the <code>View</code> class which means that nothing is being rendered. It was at this point I was getting a little confused since I would imagine that something would have to be rendered in order to access the <code>view</code> or <code>contents</code> properties of the controller test case. On closer inspection, it turns out these values were empty so it seems something may be wrong with my setup.</p> <p>What I suggest you do is get hold of an open source IDE which supports debugging like <a href="http://projects.eclipse.org/projects/tools.pdt" rel="nofollow">PHP Development Tools for Eclipse</a> and stick a break-point in your code and follow the thread through the framework. This will give you greater insight into how views are rendered when testing controllers and if so, will help track down the code coverage. I personally would have thought code coverage for templates would be quite useful and I'm surprised that the functionality doesn't exist. If you do choose to modify the framework's source, it might be worth cloning CakePHP in GitHub and then add a pull request so they can merge in your changes into the main branch.</p> <p>I'm sorry I couldn't be of any more help, I took my best shot at it!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload