123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- class PersistenceTest extends PHPUnit_Framework_TestCase
- {
- private $testObj;
- private $mysqli;
- private $memcache;
- public function setUp()
- {
- $this->mysqli = $this->getMockBuilder('mysqli')
- ->disableOriginalConstructor()
- ->getMock();
- $this->memcache = $this->getMockBuilder('Memcache')
- ->disableOriginalConstructor()
- ->getMock();
-
- $this->testObj = new Persistence($this->mysqli, $this->memcache);
- }
- public function tearDown()
- {
- }
- public function testGetOfferPathPageReturnsPageInformation()
- {
- $offerId = TestUtils::getRandomInteger();
- $page = TestUtils::getRandomInteger();
-
- $sql = sprintf(Persistence::OFFER_PATH_PAGE_SQL, $offerId, $page);
-
- $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");
-
- $this->memcache->method("get")
- ->with($this->getOfferPageCacheKey($offerId, $page))
- ->willReturn(false);
-
- $mockMysqlResult = $this->getMockBuilder("mysqli_result")
- ->disableOriginalConstructor()
- ->getMock();
-
- $mockMysqlResult->expects($this->any())
- ->method("fetch_assoc")
- ->willReturnOnConsecutiveCalls($expected, null);
-
- $this->mysqli->method('query')
- ->with($sql)
- ->willReturn($mockMysqlResult);
-
- $actual = $this->testObj->getOfferPathPage($offerId, $page);
-
- $this->assertEquals($expected, $actual);
- }
- public function testGetOfferPathPageReturnsFromCache()
- {
- $offerId = TestUtils::getRandomInteger();
- $page = TestUtils::getRandomInteger();
- $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");
-
- $this->memcache->method("get")
- ->with($this->getOfferPageCacheKey($offerId, $page))
- ->willReturn($expected);
-
- $this->memcache->expects($this->never())
- ->method("add");
-
- $actual = $this->testObj->getOfferPathPage($offerId, $page);
- $this->assertEquals($expected, $actual);
- }
- public function testGetOfferPathPageSavesValueToCache()
- {
- $offerId = TestUtils::getRandomInteger();
- $page = TestUtils::getRandomInteger();
-
- $sql = sprintf(Persistence::OFFER_PATH_PAGE_SQL, $offerId, $page);
-
- $value = TestUtils::getRandomArray();
-
- $this->memcache->method("get")
- ->with($this->getOfferPageCacheKey($offerId, $page))
- ->willReturn(false);
-
- $mockMysqlResult = $this->getMockBuilder("mysqli_result")
- ->disableOriginalConstructor()
- ->getMock();
-
- $mockMysqlResult->expects($this->any())
- ->method("fetch_assoc")
- ->willReturnOnConsecutiveCalls($value, null);
-
- $this->mysqli->method('query')
- ->with($sql)
- ->willReturn($mockMysqlResult);
-
- $this->memcache->expects($this->once())
- ->method("add")
- ->with($this->getOfferPageCacheKey($offerId, $page), $value);
-
- $this->testObj->getOfferPathPage($offerId, $page);
- }
- public function testGetOfferPathReturnsFalseIfEmptyResults()
- {
- $offerId = TestUtils::getRandomInteger();
- $page = TestUtils::getRandomInteger();
- $value = array();
-
- $sql = sprintf(Persistence::OFFER_PATH_PAGE_SQL, $offerId, $page);
-
- $this->memcache->method("get")
- ->with($this->getOfferPageCacheKey($offerId, $page))
- ->willReturn(false);
-
- $mockMysqlResult = $this->getMockBuilder("mysqli_result")
- ->disableOriginalConstructor()
- ->getMock();
-
- $mockMysqlResult->expects($this->any())
- ->method("fetch_assoc")
- ->willReturnOnConsecutiveCalls($value, null);
-
- $this->mysqli->method('query')
- ->with($sql)
- ->willReturn($mockMysqlResult);
-
- $actual = $this->testObj->getOfferPathPage($offerId, $page);
- $this->assertFalse($actual);
- }
- public function testGetOfferPathReturnsFalseIfMysqlFails()
- {
- $offerId = TestUtils::getRandomInteger();
- $page = TestUtils::getRandomInteger();
-
- $sql = sprintf(Persistence::OFFER_PATH_PAGE_SQL, $offerId, $page);
-
- $this->memcache->method("get")
- ->with($this->getOfferPageCacheKey($offerId, $page))
- ->willReturn(false);
-
- $this->mysqli->method('query')
- ->with($sql)
- ->willReturn(false);
-
- $actual = $this->testObj->getOfferPathPage($offerId, $page);
- $this->assertFalse($actual);
- }
-
- public function testSqlValidation()
- {
- $validSql = "SELECT * FROM offers
- LEFT JOIN offer_paths ON offers.offer_id = offer_paths.offer_id
- LEFT JOIN path_templates ON path_templates.path_id = offer_paths.path_id
- LEFT JOIN templates ON templates.template_id = path_templates.template_id
- WHERE offers.offer_id = %s AND sequence = %s";
- $sql = Persistence::OFFER_PATH_PAGE_SQL;
- $this->assertEquals($validSql, $sql);
- }
-
- private function getOfferPageCacheKey($offerId, $page)
- {
- return "page-" . $offerId . "-" . $page;
- }
- }
|