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; } }