DbMultiControllerTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. class DbMultiControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  3. {
  4. protected function setUp()
  5. {
  6. $this->bootstrap = new Zend_Application(
  7. APPLICATION_ENV,
  8. APPLICATION_PATH . '/configs/application.ini'
  9. );
  10. parent::setUp();
  11. }
  12. public function testType3MultipleDatabaseQueries()
  13. {
  14. $this->dispatch('/db-multi?queries=2');
  15. $this->assertResponseResultsEquals(2, $this->getResponse()->getBody());
  16. }
  17. public function testType3MultipleDatabaseQueryLessThan1DefaultsTo1()
  18. {
  19. $this->dispatch('/db-multi?queries=-1');
  20. $this->assertResponseResultsEquals(1, $this->getResponse()->getBody());
  21. }
  22. public function testType3MultipleDatabaseQueryMoreThan500DefaultsTo500()
  23. {
  24. $this->dispatch('/db-multi?queries=501');
  25. $this->assertResponseResultsEquals(500, $this->getResponse()->getBody());
  26. }
  27. public function testType3MultipleDatabaseQueryNotAnIntDefaultsTo1()
  28. {
  29. $this->dispatch('/db-multi?queries=foobar');
  30. $this->assertResponseResultsEquals(1, $this->getResponse()->getBody());
  31. }
  32. public function testType3MultipleDatabaseQueryNoQueriesParamDefaultsTo1()
  33. {
  34. $this->dispatch('/db-multi');
  35. $this->assertResponseResultsEquals(1, $this->getResponse()->getBody());
  36. }
  37. /**
  38. * Helper assertion
  39. *
  40. * @param int $expectedCount
  41. * @param string $content
  42. */
  43. protected function assertResponseResultsEquals($expectedCount, $content)
  44. {
  45. $this->assertModule('default');
  46. $this->assertController('db-multi');
  47. $this->assertAction('index');
  48. $this->assertResponseCode(200);
  49. $this->assertHeaderRegex('Content-Type', '#^application/json$#');
  50. $results = json_decode($content, true);
  51. $this->assertTrue(json_last_error() === JSON_ERROR_NONE, 'Json decode failure');
  52. $this->assertCount($expectedCount, $results);
  53. $count = count($results);
  54. for ($i = 0; $i < $count; $i++) {
  55. $this->assertArrayHasKey('id', $results[$i]);
  56. $this->assertGreaterThan(0, $results[$i]['id']);
  57. $this->assertLessThan(10000, $results[$i]['id']);
  58. $this->assertArrayHasKey('randomNumber', $results[$i]);
  59. $this->assertGreaterThan(0, $results[$i]['randomNumber']);
  60. $this->assertLessThan(10000, $results[$i]['randomNumber']);
  61. }
  62. }
  63. }