예제 #1
0
 public function testCanIterateOverResultsUsingRespository()
 {
     $pdoConfig = BookDao::getConfig('pdosqlite');
     $strategyA = new \Octopus\Strategy\PdoSqlite($pdoConfig);
     $strategyB = new \Octopus\Strategy\PdoSqlite($pdoConfig);
     $this->createBookTestTableIWithPdoHandle($strategyA->getPdoHandle(), $pdoConfig);
     $this->createBookTestTableIWithPdoHandle($strategyB->getPdoHandle(), $pdoConfig);
     $w = array($strategyA, $strategyB);
     // Note how I'm only reading from strategyB.
     $r = array($strategyB);
     $bookDao = new BookDao($w, $r);
     // Save 7 models.
     for ($i = 0; $i < 7; $i++) {
         $bookDao->create(new Book());
     }
     // Load a repository.
     $bookRepository = new BookRepository($bookDao);
     // Run a query which returns all of the books.
     $result = $bookRepository->getAllBooks($strategyA->getPdoHandle());
     $this->assertTrue($result instanceof \Traversable);
     $this->assertTrue($result instanceof \Countable);
     $this->assertEquals(7, count($result));
     foreach ($result as $thisBook) {
         $this->assertEquals(36, strlen($thisBook->getId()));
     }
 }
예제 #2
0
 public function testBackupStrategyNotTriggeredUnderNormalCircumstances()
 {
     $pdoConfig = BookDao::getConfig('pdosqlite');
     // This one triggers an exception.
     $pdoSqliteStrategyMock = $this->getMock('\\Octopus\\Strategy\\PdoSqlite', array('create'), array($pdoConfig));
     $pdoSqliteStrategyMock->expects($this->once())->method('create');
     // This one should be used because there was an exception thrown.
     $pdoSqliteStrategyMockBackup = $this->getMock('\\Octopus\\Strategy\\PdoSqlite', array('create'), array($pdoConfig));
     $pdoSqliteStrategyMockBackup->expects($this->never())->method('create');
     $this->createBookTestTableIWithPdoHandle($pdoSqliteStrategyMock->getPdoHandle(), $pdoConfig);
     $w = array($pdoSqliteStrategyMock);
     $r = array($pdoSqliteStrategyMock);
     $bookDao = new BookDao($w, $r);
     $bookDao->setPrimaryWriteBackupStrategy($pdoSqliteStrategyMockBackup);
     $bookDao->create(new Book());
 }
예제 #3
0
 /**
  * Only the create function should work. Other methods should trigger 
  * exceptions indicating those RUD methods are not supported by this
  * strategy.
  * 
  * @param array $write_strategies
  * @param array $read_strategies
  */
 protected function _testGoldenCPath(array $write_strategies, array $read_strategies)
 {
     // Create a DAO.
     $bookDao = new BookDao($write_strategies, $read_strategies);
     // Make a book and set its properties.
     $book = new Book();
     $title = uniqid();
     $author = uniqid();
     $book->setTitle($title);
     $book->setAuthor($author);
     // Run the "C" create function.
     $bookDao->create($book);
     // Ensure a UUID was defined as the id.
     $this->assertTrue(strlen($book->getId()) == 36);
     $correctReadExceptionDetected = false;
     $correctUpdateExceptionDetected = false;
     $correctDeleteExceptionDetected = false;
     // Do a "R"ead and ensure everything comes back.
     try {
         $returnedBook = $bookDao->read($book->getId());
     } catch (\Octopus\Strategy\Exception\InvalidStrategyMethod $e) {
         $correctReadExceptionDetected = true;
     }
     // "U"pdate the record.
     try {
         $book->setTitle('foo123');
         $bookDao->update($book);
     } catch (\Octopus\Strategy\Exception\InvalidStrategyMethod $e) {
         $correctUpdateExceptionDetected = true;
     }
     // "R"emove it
     try {
         $bookDao->delete($book);
     } catch (\Octopus\Strategy\Exception\InvalidStrategyMethod $ex) {
         $correctDeleteExceptionDetected = true;
     }
     $this->assertTrue($correctReadExceptionDetected);
     $this->assertTrue($correctUpdateExceptionDetected);
     $this->assertTrue($correctDeleteExceptionDetected);
 }
예제 #4
0
 public function setUp()
 {
     $config = BookDao::getConfig('pdosqlite');
     $this->_strategy = new PdoSqlite($config);
     $fieldDefinitions = '';
     foreach ($config['columns'] as $field) {
         $fieldDefinitions .= "{$field} varchar(255), ";
     }
     $fieldDefinitions = rtrim($fieldDefinitions);
     $fieldDefinitions = substr($fieldDefinitions, 0, strlen($fieldDefinitions) - 1);
     $sql = "CREATE TABLE " . $config['table'] . " ({$fieldDefinitions});";
     $handle = $this->_strategy->getPdoHandle()->exec($sql);
 }
예제 #5
0
 public function setUp()
 {
     $config = BookDao::getConfig('memcache');
     $this->_strategy = new Memcache($config);
 }