/** * Write an entry to the ORM log. * * Pass a method or class name as the second argument. * * @param string $sMessage * @param string $sClass * @throws \Exception */ public function log($sMessage, $sClass) { if ($this->oConfig instanceof Configuration) { $aConfig = $this->oConfig->get(); if (isset($aConfig[Configuration::ORM_KEY]['logfile']) && \is_file($aConfig[Configuration::ORM_KEY]['logfile']) && \is_writable($aConfig[Configuration::ORM_KEY]['logfile'])) { $sLog = \is_string($sClass) ? $sClass . ' ' . $sMessage : $sMessage; //Write to the log file if it exists. Append entries and apply a lock to the file during write operation. \file_put_contents($aConfig[Configuration::ORM_KEY]['logfile'], $sLog, FILE_APPEND | LOCK_EX); } else { throw new \Exception('Log file cannot be found or is not writable'); } } }
/** * Returns an instance of the PHP data object using the mysql PDO extension. * * @param Configuration $oConfig * @throws \MiniOrm\DbalException */ public function getDataAccessObject(Configuration $oConfig) { $aConfig = $oConfig->get(); $sConnect = $this->getConnection($oConfig); if (isset($aConfig['connection']['username']) && isset($aConfig['connection']['password'])) { try { return new \PDO($sConnect, $aConfig['connection']['username'], $aConfig['connection']['password']); } catch (\PDOException $oExp) { throw DbalException::pdoException($oExp->getMessage(), $oExp); } } else { throw DbalException::credentialsNotFound(); } }
/** * */ public function testGetPDOMySQLConnection() { $oConfig = Configuration::getInstance(); $oConfig->read(__DIR__ . self::MYSQL_TEST_CONFIG); $oFactory = new DriverFactory(); $this->object = new DbalManager($oFactory, $oConfig); $oDao = $this->object->getDataObject(); $this->assertInstanceOf('\\PDO', $oDao); }
/** * */ public function testPrepareInsert() { $oEntity = new ApplicationUser(); $oConfig = Configuration::getInstance(); $oConfig->read(__DIR__ . self::MYSQL_TEST_CONFIG); $oDbal = new DbalManager(new DriverFactory(), $oConfig); $oMapper = new AnnotationMapping(); $oMapper->mapEntity(\get_class($oEntity)); $oPrepare = new PrepareStatementImpl(); $insert = $oPrepare->insert($oEntity, $oMapper, $oDbal->getDataObject()); }
/** * Set the error mode for PDO. * * If no error mode is set in configurations then PDO will be set to error mode exception. * * @param \PDO $oPDO */ private function setErrorMode(\PDO $oPDO) { $aConfig = $this->oConfig->get(); if (isset($aConfig[Configuration::PDO_KEY]) && isset($aConfig[Configuration::PDO_KEY]['errormode'])) { switch ($aConfig[Configuration::PDO_KEY]['errormode']) { case self::ERRMODE_SILENT: $oPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT); break; case self::ERRMODE_WARNING: $oPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); break; case self::ERRMODE_EXCEPTION: $oPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); break; } } else { //Default setting if error mode is not set in configurations. $oPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } }
/** * Returns the connection string that is to be passed when building the PDO object. * * Method returns a PDO connection string for SQLite3. * * @param \Configuration $oConfig * @return string * @throws \MiniOrm\DbalException */ public function getConnection(Configuration $oConfig) { $aConfig = $oConfig->get(); $sConnect = ''; //Check that the driver is present and is the correct one. if (isset($aConfig['connection']) && isset($aConfig['connection']['driver']) && self::DRIVER_NAME == $aConfig['connection']['driver']) { //Sqlite DSN has a variety of prefixes. //If one is not set in confuigurations then assign the default held in the PREFIX constant. $sPrefix = null; if (isset($aConfig['connection']['prefix']) && !empty($aConfig['connection']['prefix'])) { $sPrefix = $aConfig['connection']['prefix']; } else { $sPrefix = self::PREFIX; } $sConnect .= $sPrefix; $sConnect .= isset($aConfig['connection']['database']) ? $aConfig['connection']['database'] : ''; } else { throw DbalException::configurationInvalid('Cannot find the connection configurations or driver is invalid for the Sqlite driver.'); } return $sConnect; }
/** * !!Problem with date times. Adjust */ public function testInstantiateAndPersist() { $oConfig = Configuration::getInstance(); $oConfig->read(__DIR__ . self::MYSQL_TEST_CONFIG); $sName = uniqid('user_'); //Create some entites to persist in the data layer $oEntity = new ApplicationUser(); $oEntity->setEmail($sName . '@foobar.com'); $oEntity->setDisplayName($sName . ' display'); $oEntity->setUsername('domain\\' . $sName); $oEntity2 = new ApplicationUser(); $oEntity2->setEmail($sName . '*****@*****.**'); $oEntity2->setDisplayName($sName . ' 2display'); $oEntity2->setUsername('domain2\\' . $sName); $oPrepare = new PrepareStatementImpl(); $oUnitOfWork = new UnitOfWork(); $oDbal = new DbalManager(new DriverFactory(), $oConfig); $oMapper = new AnnotationMapping(); $oEntityManager = new EntityManager($oDbal, $oMapper, $oUnitOfWork, $oPrepare); //Mark the entity as one to be persisted or inserted into the database. $oEntityManager->persist($oEntity); $oEntityManager->persist($oEntity2); $this->assertEquals(2, $oUnitOfWork->count()); //Synchronize the objects in the unit of work with the database. //All entites that were passed to the persist method will also be updated with the relevant id. $oEntityManager->flush(); //Flush should empty the unit of work $this->assertEquals(0, $oUnitOfWork->count()); $this->assertNotNull($oEntity->getId()); $this->assertNotNull($oEntity2->getId()); $oPersistEntity = $oEntityManager->find('MiniOrmTest\\Entity\\ApplicationUser', $oEntity2->getId()); $this->assertNotNull($oPersistEntity); $this->assertEquals($oEntity2->getDisplayName(), $oPersistEntity->getDisplayName()); print_r($oPersistEntity, 'Entity is persisted'); $aCollection = $oEntityManager->findAll('MiniOrmTest\\Entity\\ApplicationUser'); print_r($aCollection); $this->assertNotEmpty($aCollection); $this->assertGreaterThan(0, $aCollection); // $oEntityManager->delete($oEntity); $oEntityManager->delete($oEntity2); // $oEntityManager->flush(); $oRemovedEntity1 = $oEntityManager->find('MiniOrmTest\\Entity\\ApplicationUser', $oEntity2->getId()); $this->assertNull($oRemovedEntity1); }
/** * @expectedException \MiniOrm\DbalException */ public function testGetInvalidConnection() { $oConfig = Configuration::getInstance(); $oConfig->read(__DIR__ . self::MYSQL_TEST_EMPTY_CONFIG); $this->object->getConnection($oConfig); }
/** * */ public function testEmptyConfiguration() { $oConfig = Configuration::getInstance(); $oConfig->read(__DIR__ . '/../configuration/orm-empty-config.php'); }