如何使用CakePHP测试检查视图代码覆盖率?

编程入门 行业动态 更新时间:2024-10-10 01:20:59
本文介绍了如何使用CakePHP测试检查视图代码覆盖率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

测试控制器时,我可以合法实现100%的代码覆盖率,如下所示:

100%代码覆盖的正确报告示例

控制代码

<?php App :: uses('AppController','Controller'); class UsersController extends AppController { public function example($ option = null){ if($ option =='foo'){ $ some_var ='hello'; } elseif($ option =='bar'){ $ some_var ='goodbye'; } $ this-> set(compact('option','some_var')); } }

测试代码 p>

<?php App :: uses('UsersController','Controller'); class UsersControllerTest extends ControllerTestCase { public function testExampleFoo(){ $ this-> testAction('/ users / example / foo'); $ this-> assertEquals('hello',$ this-> vars ['some_var']); } public function testExampleBar(){ $ this-> testAction('/ users / example / bar'); $ this-> assertEquals('goodbye',$ this-> vars ['some_var']); } }

但是,在我的意见中实现了100%的代码覆盖率?例如:

100%代码覆盖率报告错误示例

控制代码

<?php App :: uses('AppController','Controller'); class UsersController extends AppController { public function example($ option = null){ $ this-> set('option',$ option); } }

查看代码 p>

<?php if($ option =='foo'){ $ some_var ='hello'; } elseif($ option =='bar'){ $ some_var ='goodbye'; } if(isset($ some_var)){ echo $ some_var; }

测试代码

<?php App :: uses('UsersController','Controller'); class UsersControllerTest extends ControllerTestCase { public function testExampleFoo(){ $ result = $ this-> testAction('/ users / example / foo' array('return'=>'view')); $ this-> assertEquals('hello',$ result); } }

请注意,上述测试代码不测试/ users / example / barURL,因此视图的 elseif 从不进行测试。因此,即使100%的控制器代码经过测试,我实际上并没有实现100%的代码覆盖率(因为视图的代码不到100%被测试)。

解决方案

要回答标题中的问题, CakePHP可以使用XDebug 来拉出测试的代码覆盖率。如果我记得正确,它嵌入代码覆盖渲染到测试套件中。

作为对上面给出的例子的一般评论,IMHO我甚至会去

更新 b

对我没有正确阅读@ Nick的问题,我深表歉意。

据我所知,CakePHP不会提升 .ctp 文件。这并不意味着XDebug不会生成它,你可以使用你的IDE(我使用的PhpStorm有一个工具)或XDebug本身直接拉回代码覆盖。很明显,这不会使测试与在单独的CakePHP中使用测试套件一样顺利。

或者,您可以针对呈现的视图进行测试。根据文档,以指定测试操作时的返回类型。因此,一个可能的解决方案是将 view 或 content 与预呈现的文件进行比较。然而,这有一个危险,因为任何不可见的字符('\\\','\r','\t')可能会导致断言失败甚至但在逻辑上,目标和结果标记是相同的。

文档显示了如何使用regex断言值,允许您检查文档的特定区域的有效性。您也可以使用PHP的 DOM类来遍历文档。

祝你好运!

更新21:21 09/05/2013

CakePHP的测试套件是建立在PHP Unit上,依次使用XDebug来生成代码覆盖率。我看看源代码并检查 BaseCoverageReport 类,发现覆盖是为运行测试的任何框架文件生成的,包括被测单元(例如你的控制器) 。这表明代码覆盖没有选择性地打开,并且为包括视图模板的所有文件生成。这说,我没有找到模板作为其生成代码覆盖的文件之一。但是,我注意到,没有为 View 类生成代码覆盖,这意味着没有任何内容正在呈现。正是在这一点上我有点困惑,因为我想象,为了访问视图或内容 / code>控制器测试用例的属性。仔细检查,结果是这些值是空的,所以它似乎可能是我的设置错误。

我建议你做的是获得一个开源IDE它支持调试,如 Eclipse Eclipse的PHP开发工具,并在代码中保持中断点,按照线程通过框架。这将使您更好地了解在测试控制器时如何呈现视图,如果是这样,将有助于跟踪代码覆盖率。我个人认为代码覆盖的模板将是非常有用的,我惊讶的功能不存在。如果你选择修改框架的源代码,那么可能需要在GitHub中克隆CakePHP,然后添加一个pull请求,这样他们可以将你的更改合并到主分支中。

对不起,我不能再有任何帮助,我把最好的拍摄了!

When testing controllers, I can legitimately achieve 100% code coverage, as shown here:

Example of Correct Report of 100% Code Coverage

Controller Code

<?php App::uses('AppController', 'Controller'); class UsersController extends AppController { public function example($option = null) { if ($option == 'foo') { $some_var = 'hello'; } elseif ($option == 'bar') { $some_var = 'goodbye'; } $this->set(compact('option', 'some_var')); } }

Test Code

<?php App::uses('UsersController', 'Controller'); class UsersControllerTest extends ControllerTestCase { public function testExampleFoo() { $this->testAction('/users/example/foo'); $this->assertEquals('hello', $this->vars['some_var']); } public function testExampleBar() { $this->testAction('/users/example/bar'); $this->assertEquals('goodbye', $this->vars['some_var']); } }

However, how can I be sure that I've achieved 100% code coverage in my views? For example:

Example of Incorrect Report of 100% Code Coverage

Controller Code

<?php App::uses('AppController', 'Controller'); class UsersController extends AppController { public function example($option = null) { $this->set('option', $option); } }

View Code

<?php if ($option == 'foo') { $some_var = 'hello'; } elseif ($option == 'bar') { $some_var = 'goodbye'; } if (isset($some_var)) { echo $some_var; }

Test Code

<?php App::uses('UsersController', 'Controller'); class UsersControllerTest extends ControllerTestCase { public function testExampleFoo() { $result = $this->testAction('/users/example/foo', array('return' => 'view')); $this->assertEquals('hello', $result); } }

Notice that the above test code doesn't test the "/users/example/bar" URL, and thus the view's elseif is never tested. So even though 100% of the controller's code was tested, I haven't actually achieved 100% code coverage (since less than 100% of the view's code was tested). What can I do about this?

解决方案

To answer the question in the title, CakePHP can use XDebug to pull out your test's code coverage. If I remember correctly, it embeds the code coverage render into the test suite.

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.

Update

My apologies for not reading @Nick's question properly.

To my knowledge, CakePHP doesn't pull up the code coverage of .ctp 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 tool) 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.

Alternatively, you can test against the rendered view. According to the documentation it is possible to specify the return type when testing actions. Therefore one possible solution would be compare either the view or contents against a pre-rendered file. There is a danger with this however because any invisible characters ('\n', '\r', '\t') may cause the assert to fail even though logically, the target and result markup are identical.

One example in the documentation 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 DOM classes to traverse the document instead.

Good luck!

Update 21:21 09/05/2013

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 BaseCoverageReport 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 View 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 view or contents 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.

What I suggest you do is get hold of an open source IDE which supports debugging like PHP Development Tools for Eclipse 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.

I'm sorry I couldn't be of any more help, I took my best shot at it!

更多推荐

如何使用CakePHP测试检查视图代码覆盖率?

本文发布于:2023-10-19 17:29:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:覆盖率   视图   如何使用   代码   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!