Свежайшие Пирожки от CakePHP по-русски

Полнейшее руководство CakePHP 1.2 на русском языке, горячие новости и полезные статьи

Testing controllers

Say you have a typical articles controller, with it's corresponding model, and it looks like this:

Простой текст
  1. <?php
  2. class ArticlesController extends AppController {
  3. var $name = 'Articles';
  4. var $helpers = array('Ajax', 'Form', 'Html');
  5. function index($short = null) {
  6. if (!empty($this->data)) {
  7. $this->Article->save($this->data);
  8. }
  9. if (!empty($short)) {
  10. $result = $this->Article->findAll(null, array('id',
  11. 'title'));
  12. } else {
  13. $result = $this->Article->findAll();
  14. }
  15. if (isset($this->params['requested'])) {
  16. return $result;
  17. }
  18. $this->set('title', 'Articles');
  19. $this->set('articles', $result);
  20. }
  21. }
  22. ?>

Create a file named articles_controller.test.php in your app/tests/cases/controllers directory and put the following inside:

Простой текст
  1. <?php
  2. class ArticlesControllerTest extends CakeTestCase {
  3. function startCase() {
  4. echo '<h1>Starting Test Case</h1>';
  5. }
  6. function endCase() {
  7. echo '<h1>Ending Test Case</h1>';
  8. }
  9. function startTest($method) {
  10. echo '<h3>Starting method ' . $method . '</h3>';
  11. }
  12. function endTest($method) {
  13. echo '<hr />';
  14. }
  15. function testIndex() {
  16. $result = $this->testAction('/articles/index');
  17. debug($result);
  18. }
  19. function testIndexShort() {
  20. $result = $this->testAction('/articles/index/short');
  21. debug($result);
  22. }
  23. function testIndexShortGetRenderedHtml() {
  24. $result = $this->testAction('/articles/index/short',
  25. array('return' => 'render'));
  26. debug(htmlentities($result));
  27. }
  28. function testIndexShortGetViewVars() {
  29. $result = $this->testAction('/articles/index/short',
  30. array('return' => 'vars'));
  31. debug($result);
  32. }
  33. function testIndexFixturized() {
  34. $result = $this->testAction('/articles/index/short',
  35. array('fixturize' => true));
  36. debug($result);
  37. }
  38. function testIndexPostFixturized() {
  39. $data = array('Article' => array('user_id' => 1, 'published'
  40. => 1, 'slug'=>'new-article', 'title' => 'New Article', 'body' => 'New Body'));
  41. $result = $this->testAction('/articles/index',
  42. array('fixturize' => true, 'data' => $data, 'method' => 'post'));
  43. debug($result);
  44. }
  45. }
  46. ?>