PersistenceTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. class PersistenceTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $testObj;
  5. private $mysqli;
  6. private $memcache;
  7. public function setUp()
  8. {
  9. $this->mysqli = $this->getMockBuilder('mysqli')
  10. ->disableOriginalConstructor()
  11. ->getMock();
  12. $this->memcache = $this->getMockBuilder('Memcache')
  13. ->disableOriginalConstructor()
  14. ->getMock();
  15. $this->testObj = new Persistence($this->mysqli, $this->memcache);
  16. }
  17. public function tearDown()
  18. {
  19. }
  20. public function testGetOfferPathPageReturnsPageInformation()
  21. {
  22. $offerId = TestUtils::getRandomInteger();
  23. $page = TestUtils::getRandomInteger();
  24. $sql = sprintf(Persistence::OFFER_PATH_PAGE_SQL, $offerId, $page);
  25. $expected = array('offer_id' => 2, 'name' => "Offer 2", 'display_title' => "Offer Two", 'disclaimer' => "Disclaimer 2", 'path_id' => 2, 'template_id' => 4, 'sequence' => 1, 'filename' => "o2-secondpage.html");
  26. $this->memcache->method("get")
  27. ->with($this->getOfferPageCacheKey($offerId, $page))
  28. ->willReturn(false);
  29. $mockMysqlResult = $this->getMockBuilder("mysqli_result")
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $mockMysqlResult->expects($this->any())
  33. ->method("fetch_assoc")
  34. ->willReturnOnConsecutiveCalls($expected, null);
  35. $this->mysqli->method('query')
  36. ->with($sql)
  37. ->willReturn($mockMysqlResult);
  38. $actual = $this->testObj->getOfferPathPage($offerId, $page);
  39. $this->assertEquals($expected, $actual);
  40. }
  41. public function testGetOfferPathPageReturnsFromCache()
  42. {
  43. $offerId = TestUtils::getRandomInteger();
  44. $page = TestUtils::getRandomInteger();
  45. $expected = array('offer_id' => $offerId, 'name' => "Offer 2", 'display_title' => "Offer Two", 'disclaimer' => "Disclaimer 2", 'path_id' => 2, 'template_id' => 4, 'sequence' => $page, 'filename' => "o2-secondpage.html");
  46. $this->memcache->method("get")
  47. ->with($this->getOfferPageCacheKey($offerId, $page))
  48. ->willReturn($expected);
  49. $this->memcache->expects($this->never())
  50. ->method("add");
  51. $actual = $this->testObj->getOfferPathPage($offerId, $page);
  52. $this->assertEquals($expected, $actual);
  53. }
  54. public function testGetOfferPathPageSavesValueToCache()
  55. {
  56. $offerId = TestUtils::getRandomInteger();
  57. $page = TestUtils::getRandomInteger();
  58. $sql = sprintf(Persistence::OFFER_PATH_PAGE_SQL, $offerId, $page);
  59. $value = TestUtils::getRandomArray();
  60. $this->memcache->method("get")
  61. ->with($this->getOfferPageCacheKey($offerId, $page))
  62. ->willReturn(false);
  63. $mockMysqlResult = $this->getMockBuilder("mysqli_result")
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $mockMysqlResult->expects($this->any())
  67. ->method("fetch_assoc")
  68. ->willReturnOnConsecutiveCalls($value, null);
  69. $this->mysqli->method('query')
  70. ->with($sql)
  71. ->willReturn($mockMysqlResult);
  72. $this->memcache->expects($this->once())
  73. ->method("add")
  74. ->with($this->getOfferPageCacheKey($offerId, $page), $value);
  75. $this->testObj->getOfferPathPage($offerId, $page);
  76. }
  77. public function testGetOfferPathReturnsFalseIfEmptyResults()
  78. {
  79. $offerId = TestUtils::getRandomInteger();
  80. $page = TestUtils::getRandomInteger();
  81. $value = array();
  82. $sql = sprintf(Persistence::OFFER_PATH_PAGE_SQL, $offerId, $page);
  83. $this->memcache->method("get")
  84. ->with($this->getOfferPageCacheKey($offerId, $page))
  85. ->willReturn(false);
  86. $mockMysqlResult = $this->getMockBuilder("mysqli_result")
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $mockMysqlResult->expects($this->any())
  90. ->method("fetch_assoc")
  91. ->willReturnOnConsecutiveCalls($value, null);
  92. $this->mysqli->method('query')
  93. ->with($sql)
  94. ->willReturn($mockMysqlResult);
  95. $actual = $this->testObj->getOfferPathPage($offerId, $page);
  96. $this->assertFalse($actual);
  97. }
  98. public function testGetOfferPathReturnsFalseIfMysqlFails()
  99. {
  100. $offerId = TestUtils::getRandomInteger();
  101. $page = TestUtils::getRandomInteger();
  102. $sql = sprintf(Persistence::OFFER_PATH_PAGE_SQL, $offerId, $page);
  103. $this->memcache->method("get")
  104. ->with($this->getOfferPageCacheKey($offerId, $page))
  105. ->willReturn(false);
  106. $this->mysqli->method('query')
  107. ->with($sql)
  108. ->willReturn(false);
  109. $actual = $this->testObj->getOfferPathPage($offerId, $page);
  110. $this->assertFalse($actual);
  111. }
  112. public function testSqlValidation()
  113. {
  114. $validSql = "SELECT * FROM offers
  115. LEFT JOIN offer_paths ON offers.offer_id = offer_paths.offer_id
  116. LEFT JOIN path_templates ON path_templates.path_id = offer_paths.path_id
  117. LEFT JOIN templates ON templates.template_id = path_templates.template_id
  118. WHERE offers.offer_id = %s AND sequence = %s";
  119. $sql = Persistence::OFFER_PATH_PAGE_SQL;
  120. $this->assertEquals($validSql, $sql);
  121. }
  122. private function getOfferPageCacheKey($offerId, $page)
  123. {
  124. return "page-" . $offerId . "-" . $page;
  125. }
  126. }