/Tests/Controller/Admin/CategoriesControllerTest.php

https://github.com/servergrove/ServerGroveKbBundle · PHP · 147 lines · 90 code · 31 blank · 26 comment · 4 complexity · b19abf574d96d450ab575e152adac5bf MD5 · raw file

  1. <?php
  2. namespace ServerGrove\KbBundle\Tests\Controller\Admin;
  3. /**
  4. * Class CategoriesControllerTest
  5. *
  6. * @author Ismael Ambrosi<ismael@servergrove.com>
  7. */
  8. class CategoriesControllerTest extends ControllerTestCase
  9. {
  10. public function getPaths()
  11. {
  12. return array(
  13. array(null),
  14. array('test'),
  15. );
  16. }
  17. /**
  18. * @param string $path
  19. *
  20. * @dataProvider getPaths
  21. */
  22. public function testIndexAction($path)
  23. {
  24. $client = $this->getClient();
  25. $crawler = $client->request('GET', $this->generateUrl(is_null($path) ? 'sgkb_admin_categories_index' : 'sgkb_admin_categories_show', array('path' => $path)));
  26. $this->assertEquals(200, $client->getResponse()->getStatusCode(), $this->getErrorMessage($client));
  27. $this->assertGreaterThan(0, $crawler->filter('table.table tbody tr')->count());
  28. if (is_null($path)) {
  29. $this->assertEquals(0, $crawler->filter('h1:contains("Category")')->count());
  30. $this->assertEquals('Categories list', $crawler->filter('#doccontent h2')->first()->text());
  31. } else {
  32. $this->assertEquals('Subcategories list', $crawler->filter('#doccontent h2')->first()->text());
  33. }
  34. }
  35. /**
  36. * @param string $path
  37. *
  38. * @dataProvider getPaths
  39. */
  40. public function testNewAction($path)
  41. {
  42. $client = $this->getClient();
  43. $crawler = $client->request('GET', $this->generateUrl(is_null($path) ? 'sgkb_admin_categories_new' : 'sgkb_admin_categories_new_subcategory', array('path' => $path)));
  44. $this->assertEquals(200, $client->getResponse()->getStatusCode(), $this->getErrorMessage($client));
  45. $this->assertEquals('Category creation', $crawler->filter('form legend')->first()->text());
  46. $this->assertGreaterThan(0, $crawler->filter('form')->count());
  47. $form = $crawler->selectButton('Create')->form();
  48. $form['category[name]'] = $name = 'Test category';
  49. $form['category[description]'] = 'This is the description of the Test category';
  50. $client->submit($form);
  51. $this->assertEquals(302, $client->getResponse()->getStatusCode(), $this->getErrorMessage($client));
  52. $category = $this->getDocumentManager()->getRepository('ServerGroveKbBundle:Category')->findOneBy(array('path' => ltrim($path.'/test-category', '/')));
  53. $this->assertInstanceOf('ServerGrove\KbBundle\Document\Category', $category);
  54. $this->assertEquals($name, $category->getName());
  55. if (!is_null($path)) {
  56. $this->assertInstanceOf('ServerGrove\KbBundle\Document\Category', $category->getParent());
  57. $this->assertEquals($path, $category->getParent()->getPath());
  58. }
  59. }
  60. /**
  61. * @depends testNewAction
  62. */
  63. public function testCreateAction()
  64. {
  65. $client = $this->getClient();
  66. $client->request('POST', $this->generateUrl('sgkb_admin_categories_create'));
  67. $this->assertEquals(400, $client->getResponse()->getStatusCode(), $this->getErrorMessage($client));
  68. }
  69. /**
  70. * @param string $path
  71. *
  72. * @dataProvider getPaths
  73. */
  74. public function testEditAction($path)
  75. {
  76. if (is_null($path)) {
  77. return;
  78. }
  79. $client = $this->getClient();
  80. $crawler = $client->request('GET', $this->generateUrl('sgkb_admin_categories_edit', array('path' => $path)));
  81. $this->assertRegExp('/^Category translation for locale \"[a-z_]{2,7}\"$/', $crawler->filter('form legend')->eq(1)->text());
  82. $this->assertGreaterThan(0, $crawler->filter('form')->count());
  83. $form = $crawler->selectButton('Save')->form();
  84. $name = 'New name '.md5(microtime(true));
  85. $form['category_translation_en[name]'] = $name;
  86. $client->submit($form);
  87. $this->assertEquals(302, $client->getResponse()->getStatusCode(), $this->getErrorMessage($client));
  88. $category = $this->getDocumentManager()->getRepository('ServerGroveKbBundle:Category')->findOneBy(array('path' => $path));
  89. $this->assertInstanceOf('ServerGrove\KbBundle\Document\Category', $category);
  90. $this->getDocumentManager()->refresh($category);
  91. $this->assertEquals($name, $category->getName());
  92. }
  93. /**
  94. * @depends testEditAction
  95. */
  96. public function testUpdateAction()
  97. {
  98. $client = $this->getClient();
  99. $client->request('POST', $this->generateUrl('sgkb_admin_categories_update', array('path' => 'test',
  100. 'locale' => 'en')));
  101. $this->assertEquals(400, $client->getResponse()->getStatusCode(), $this->getErrorMessage($client));
  102. }
  103. public function testDeleteAction()
  104. {
  105. $path = 'test';
  106. $client = $this->getClient();
  107. $crawler = $client->request('GET', $this->generateUrl('sgkb_admin_categories_edit', array('path' => $path)));
  108. $this->markTestIncomplete();
  109. $this->assertGreaterThan(0, $crawler->filter('button#btn_sgkb_admin_categories_delete')->count());
  110. $form = $crawler->selectButton('btn_sgkb_admin_categories_delete')->form();
  111. $client->submit($form);
  112. $this->assertEquals(302, $client->getResponse()->getStatusCode(), $this->getErrorMessage($client));
  113. $category = $this->getDocumentManager()->getRepository('ServerGroveKbBundle:Category')->findOneBy(array('path' => $path));
  114. $this->assertNotInstanceOf('ServerGrove\KbBundle\Document\Category', $category);
  115. }
  116. }