コード例 #1
0
 /**
  * @param  string $lastname
  * @return Author
  */
 protected function createAuthor($lastname)
 {
     $table = $this->rm->getTable('author');
     $authorData = array('firstname' => $lastname, 'lastname' => $lastname, 'email' => 'mail@' . $lastname . '.local');
     $author = $table->createRecord($authorData);
     return $author;
 }
コード例 #2
0
ファイル: DelegateBehaviorTest.php プロジェクト: sigma-z/dive
 public function testGetDelegatedFieldsOnNotInitializedParents()
 {
     $manager = $this->rm->getTable('manager')->createRecord();
     $this->assertFalse($manager->firstname);
     $this->assertFalse($manager->lastname);
     $this->assertFalse($manager->salary);
 }
コード例 #3
0
ファイル: RecordGeneratorTest.php プロジェクト: sigma-z/dive
 /**
  * @return array
  */
 private function thenThereShouldBeAnEditorLinkedWith($lastname)
 {
     $editor = $this->rm->createQuery('author', 'a')->select("e.id, e.firstname, e.lastname, e.email")->leftJoin('a.Editor e')->where('a.lastname = ?', $lastname)->andWhere("e.id IS NOT NULL")->fetchArray();
     $this->assertTrue((bool) $editor);
     $this->assertCount(1, $editor);
     return $editor[0];
 }
コード例 #4
0
 /**
  * @param array $recordData
  * @return \Dive\Record
  */
 private function createRecordWithRandomData(array $recordData)
 {
     $table = $this->rm->getTable('unique_constraint_test');
     $fieldValueGenerator = new FieldValuesGenerator();
     $recordData = $fieldValueGenerator->getRandomRecordData($table->getFields(), $recordData, FieldValuesGenerator::MAXIMAL_WITHOUT_AUTOINCREMENT);
     return $table->createRecord($recordData);
 }
コード例 #5
0
ファイル: RelationLoadingTest.php プロジェクト: sigma-z/dive
 /**
  * @dataProvider provideLoadReferences
  * @param string $tableName
  * @param string $recordKey
  * @param array  $references
  * @param array  $expectedReferencesLoaded
  */
 public function testLoadReferences($tableName, $recordKey, array $references, array $expectedReferencesLoaded)
 {
     $table = self::$rm->getTable($tableName);
     $record = $this->getGeneratedRecord(self::$recordGenerator, $table, $recordKey);
     $record->loadReferences($references);
     $actualReferencesLoaded = $this->getLoadedReferences($record);
     $this->assertEquals($expectedReferencesLoaded, $actualReferencesLoaded);
 }
コード例 #6
0
ファイル: Table.php プロジェクト: sigma-z/dive
 /**
  * Creates query with this table in from clause
  *
  * @param  string $alias
  * @return Query
  */
 public function createQuery($alias = 'a')
 {
     $from = $this->getTableName() . ' ' . $alias;
     $queryClass = $this->rm->getQueryClass();
     /** @var Query $query */
     $query = new $queryClass($this->rm);
     $query->from($from);
     return $query;
 }
コード例 #7
0
ファイル: UnitOfWork.php プロジェクト: sigma-z/dive
 /**
  * @param Record $record
  * @return bool
  */
 private function validateRecord(Record $record)
 {
     $validator = $this->recordManager->getRecordValidationContainer();
     $isValid = $validator->validate($record);
     //        if (!$isValid) {
     //            var_dump($record->toArray(false));
     //        }
     return $isValid;
 }
コード例 #8
0
ファイル: Query.php プロジェクト: sigma-z/dive
 /**
  * Gets root table
  *
  * @return \Dive\Table
  * @throws QueryException
  */
 public function getRootTable()
 {
     $alias = $this->rootAlias;
     if ($alias === null || !isset($this->queryComponents[$alias])) {
         throw new QueryException('Root table is not defined, yet.');
     }
     $tableName = $this->queryComponents[$alias]['table'];
     return $this->rm->getTable($tableName);
 }
コード例 #9
0
ファイル: RecordGenerator.php プロジェクト: sigma-z/dive
 /**
  * @param $refTableName
  * @param $refField
  * @param $id
  * @param $relatedKey
  * @throws RecordGeneratorException
  * @throws \Dive\Table\TableException
  */
 private function updateRelatedRecord($refTableName, $refField, $id, $relatedKey)
 {
     $record = $this->rm->getTable($refTableName)->getFromRepository($this->getRecordIdFromMap($refTableName, $relatedKey));
     if (!$record) {
         throw new RecordGeneratorException("record not found");
     }
     $record->set($refField, $id);
     $this->rm->scheduleSave($record);
     $this->rm->commit();
 }
コード例 #10
0
ファイル: RecordManagerTest.php プロジェクト: sigma-z/dive
 public function testGetTableWithBehavior()
 {
     $tableName = 'article';
     // initializes article table and instances TimestampableBehavior as shared instance
     $this->rm->getTable($tableName);
     $tableBehaviors = self::readAttribute($this->rm, 'tableBehaviors');
     $this->assertCount(1, $tableBehaviors);
     /** @var TimestampableBehavior $timestampableBehavior */
     $timestampableBehavior = current($tableBehaviors);
     $this->assertInstanceOf('\\Dive\\Table\\Behavior\\TimestampableBehavior', $timestampableBehavior);
     $eventDispatcher = $this->rm->getEventDispatcher();
     $this->assertCount(1, $eventDispatcher->getListeners(Record::EVENT_PRE_SAVE));
     $this->assertCount(1, $eventDispatcher->getListeners(Record::EVENT_PRE_UPDATE));
     $this->assertCount(1, $eventDispatcher->getListeners(Record::EVENT_PRE_INSERT));
 }
コード例 #11
0
ファイル: RelationTest.php プロジェクト: sigma-z/dive
 /**
  * @dataProvider provideIsOwningSideFlag
  */
 public function testReferenceMapOneToManySet($isOwningSide)
 {
     $authorTable = $this->rm->getTable('author');
     /** @var Author $author */
     $author = $authorTable->createRecord();
     /** @var Author $editor */
     $editor = $authorTable->createRecord();
     $relation = $authorTable->getRelation('Author');
     if ($isOwningSide) {
         $author->Editor = $editor;
         $this->assertTrue($relation->hasReferenceLoadedFor($author, 'Editor'), 'Reference to author->Editor is not set!');
     } else {
         $this->assertInstanceOf('\\Dive\\Collection\\RecordCollection', $editor->Author);
         $editor->Author[] = $author;
         $this->assertTrue($relation->hasReferenceLoadedFor($editor, 'Author'), 'Reference to editor->Author is not set!');
     }
     $this->assertRelationReferences($editor, 'Author', $author);
 }
コード例 #12
0
 /**
  * @param string $username
  * @param bool   $withArticles
  *
  * @return User
  */
 private function createUserWithAuthor($username, $withArticles)
 {
     $tablesRows = $this->tableRows[$username];
     if (!$withArticles) {
         unset($tablesRows['articles']);
     }
     $rm = self::createDefaultRecordManager();
     $recordGenerator = self::saveTableRows($rm, $tablesRows);
     $userId = $recordGenerator->getRecordIdFromMap('user', $username);
     $this->assertNotEmpty($userId);
     // fetching user from database
     $userTable = $this->rm->getTable('user');
     /** @var User $user */
     $user = $userTable->findByPk($userId);
     $this->assertInstanceOf('\\Dive\\Record', $user);
     $this->assertInstanceOf('\\Dive\\Record', $user->Author);
     if ($withArticles && isset($tablesRows['articles'])) {
         $this->assertInstanceOf('\\Dive\\Collection\\RecordCollection', $user->Author->Article);
         $this->assertEquals(count($tablesRows['articles']), $user->Author->Article->count());
     }
     return $user;
 }
コード例 #13
0
 /**
  * @param array $recordData
  */
 private function givenIHaveARecordStoredWithData(array $recordData)
 {
     $this->storedRecord = self::getRecordWithRandomData($this->table, $recordData);
     $this->rm->scheduleSave($this->storedRecord)->commit();
 }
コード例 #14
0
ファイル: QueryTest.php プロジェクト: sigma-z/dive
 /**
  * @param  RecordManager $rm
  * @return array
  */
 private static function saveUserRecords(RecordManager $rm)
 {
     $table = $rm->getTable('user');
     $userIds = array();
     foreach (self::$usersData as &$userData) {
         $userIds[] = self::insertDataset($table, $userData);
     }
     return $userIds;
 }
コード例 #15
0
ファイル: FieldValidatorTest.php プロジェクト: sigma-z/dive
 /**
  * @param string $tableName
  */
 private function givenIHaveARecordOfTable($tableName)
 {
     $this->record = $this->rm->getTable($tableName)->createRecord();
 }
 /**
  * @return Article
  */
 private function createArticle()
 {
     $table = $this->rm->getTable('article');
     $recordData = array('id' => '1', 'title' => 'Release announcement', 'teaser' => 'Dive release with some brand new features', 'text' => 'Dive into Dive');
     return self::getRecordWithRandomData($table, $recordData);
 }
コード例 #17
0
 /**
  * @param array $userData
  */
 private function givenIHaveAUserRecordWithData(array $userData)
 {
     $this->record = $this->rm->getOrCreateRecord('user', $userData);
 }
コード例 #18
0
ファイル: ConnectionTest.php プロジェクト: sigma-z/dive
 /**
  * @param RecordManager $rm
  * @param Relation      $relation
  * @return string
  */
 protected function insertRequiredLocalRelationGraph(RecordManager $rm, Relation $relation)
 {
     $randomGenerator = new FieldValuesGenerator();
     $conn = $rm->getConnection();
     $refTableName = $relation->getReferencedTable();
     $refTable = $rm->getTable($refTableName);
     $refFields = $refTable->getFields();
     $data = array();
     // recursion: walk through all local relations that are required and handle this by calling this method with
     //  next relation
     $owningRelations = $refTable->getReferencedRelationsIndexedByOwningField();
     foreach ($owningRelations as $owningField => $owningRelation) {
         // check if field is required (default of matchType) and insert required related data
         if ($randomGenerator->matchType($refFields[$owningField])) {
             $data[$owningField] = $this->insertRequiredLocalRelationGraph($rm, $owningRelation);
         }
     }
     // insert a record and return its id
     $data = $randomGenerator->getRandomRecordData($refFields, $data);
     $conn->insert($refTable, $data);
     return $conn->getLastInsertId($refTableName);
 }
コード例 #19
0
ファイル: RecordTest.php プロジェクト: sigma-z/dive
 /**
  * @param  RecordManager $rm
  * @return User
  */
 private function createUserRecord(RecordManager $rm)
 {
     $table = $rm->getTable('user');
     $data = array('username' => 'Joe', 'password' => 'secret password');
     return $table->createRecord($data);
 }
コード例 #20
0
ファイル: RecordCollectionTest.php プロジェクト: sigma-z/dive
 /**
  * @expectedException \Dive\Collection\CollectionException
  */
 public function testAddWrongRecordTypeWillThrowException()
 {
     $table = $this->rm->getTable('author');
     $author = $table->createRecord();
     $this->userColl->add($author);
 }
コード例 #21
0
ファイル: Relation.php プロジェクト: sigma-z/dive
 /**
  * Gets join table
  *
  * @param  \Dive\RecordManager  $rm
  * @param  string               $relationName
  * @return \Dive\Table
  */
 public function getJoinTable(RecordManager $rm, $relationName)
 {
     $joinTableName = $this->getJoinTableName($relationName);
     return $rm->getTable($joinTableName);
 }
コード例 #22
0
ファイル: RecordDeleteTest.php プロジェクト: sigma-z/dive
 /**
  * @param  RecordManager $rm
  * @param  array         $recordKeys keys: record keys; values: table names
  * @return Record[]      recordKey as keys
  */
 private function getRecordsForRecordKeys(RecordManager $rm, array $recordKeys)
 {
     $records = array();
     foreach ($recordKeys as $recordKey => $tableName) {
         $table = $rm->getTable($tableName);
         $record = $this->getGeneratedRecord($this->recordGenerator, $table, $recordKey);
         $records[$recordKey] = $record;
     }
     return $records;
 }
コード例 #23
0
ファイル: Hydrator.php プロジェクト: sigma-z/dive
 /**
  * hydrates record
  *
  * @param   Table $table
  * @param   array $row
  * @return  Record
  */
 protected function hydrateRecord(Table $table, array $row)
 {
     $record = $this->recordManager->getOrCreateRecord($table->getTableName(), $row, true);
     return $record;
 }
コード例 #24
0
 /**
  * @param RecordManager $rm
  */
 private function addPreFieldValueChangeEventListener(RecordManager $rm)
 {
     $fieldValueChangeListener = function (FieldValueChangeEvent $event) {
         /** @var Record $record */
         $record = $event->getRecord();
         $record->markFieldAsModified($event->getProperty());
     };
     // clean event dispatcher with no listeners
     $rm->getConnection()->setEventDispatcher(new EventDispatcher());
     $eventDispatcher = $rm->getEventDispatcher();
     $eventDispatcher->addListener(Record::EVENT_PRE_FIELD_VALUE_CHANGE, $fieldValueChangeListener);
 }