TemplatePageTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. class TemplatePageTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $testObj;
  5. private $twig;
  6. private $offerRetriever;
  7. public function setUp()
  8. {
  9. $this->twig = $this->getMockBuilder('TwigWrapper')
  10. ->disableOriginalConstructor()
  11. ->getMock();
  12. $this->offerRetriever = $this->getMockBuilder('OfferRetriever')
  13. ->disableOriginalConstructor()
  14. ->getMock();
  15. $this->testObj = new TemplatePage($this->twig, $this->offerRetriever);
  16. }
  17. public function tearDown()
  18. {
  19. unset($_GET['id']);
  20. unset($_GET['page']);
  21. }
  22. public function testDisplayOutputsDefaultPage()
  23. {
  24. $pageId = 0;
  25. $offerId = TestUtils::getRandomInteger();
  26. $_GET['id'] = $offerId;
  27. $templateFilename = TestUtils::getRandomString(10);
  28. $pageData = array('filename' => $templateFilename);
  29. $expected = TestUtils::getRandomString(20);
  30. $this->offerRetriever->method('getPageData')
  31. ->with($offerId, $pageId)
  32. ->willReturn($pageData);
  33. $this->twig->expects($this->once())
  34. ->method('render')
  35. ->with($templateFilename, $pageData)
  36. ->willReturn($expected);
  37. $actual = $this->testObj->display();
  38. $this->assertEquals($expected, $actual);
  39. }
  40. public function testDisplayOutputsAnErrorIfTemplateNotFound()
  41. {
  42. $templateRequested = "templateDoesNotExist.html";
  43. $expected = TestUtils::getRandomString(20);
  44. $_GET['id'] = null;
  45. $this->twig->expects($this->once())
  46. ->method('render')
  47. ->with($templateRequested, array())
  48. ->willReturn($expected);
  49. $actual = $this->testObj->display();
  50. $this->assertEquals($expected, $actual);
  51. }
  52. public function testDisplayOutputsCorrectTemplate()
  53. {
  54. $offerId = TestUtils::getRandomInteger();
  55. $pageId = TestUtils::getRandomInteger();
  56. $_GET['id'] = $offerId;
  57. $_GET['page'] = $pageId;
  58. $offerName = TestUtils::getRandomString(10);
  59. $displayTitle = TestUtils::getRandomString(10);
  60. $disclaimer = TestUtils::getRandomString(10);
  61. $templateFilename = TestUtils::getRandomString(10);
  62. $expected = TestUtils::getRandomString(20);
  63. $template = array('name' => $offerName, 'display_title' => $displayTitle, 'disclaimer' => $disclaimer, 'filename' => $templateFilename, 'offer_id' => $offerId);
  64. $this->offerRetriever->method('getPageData')
  65. ->with($offerId, $pageId)
  66. ->willReturn($template);
  67. $this->twig->expects($this->once())
  68. ->method('render')
  69. ->with($templateFilename, $template)
  70. ->willReturn($expected);
  71. $actual = $this->testObj->display();
  72. $this->assertEquals($expected, $actual);
  73. }
  74. }