/**
  * @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;
 }
Example #2
0
 /**
  * @param array $expectedTablesCounts
  */
 private function thenItShouldHaveGeneratedRecordsWithTablesCounts(array $expectedTablesCounts)
 {
     foreach ($expectedTablesCounts as $tableName => $expectedTableCounts) {
         $actual = $this->rm->getTable($tableName)->createQuery()->countByPk();
         $this->assertEquals($expectedTableCounts, $actual);
     }
 }
Example #3
0
 public function testGetDelegatedFieldsOnNotInitializedParents()
 {
     $manager = $this->rm->getTable('manager')->createRecord();
     $this->assertFalse($manager->firstname);
     $this->assertFalse($manager->lastname);
     $this->assertFalse($manager->salary);
 }
 /**
  * @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);
 }
Example #5
0
 /**
  * @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);
 }
Example #6
0
 /**
  * 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);
 }
Example #7
0
 /**
  * @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();
 }
Example #8
0
 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));
 }
Example #9
0
 /**
  * @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);
 }
 /**
  * @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;
 }
Example #11
0
 /**
  * @expectedException \Dive\Collection\CollectionException
  */
 public function testAddWrongRecordTypeWillThrowException()
 {
     $table = $this->rm->getTable('author');
     $author = $table->createRecord();
     $this->userColl->add($author);
 }
 private function givenIHaveAUniqueConstraintTestTable()
 {
     $this->table = $this->rm->getTable('unique_constraint_test');
 }
Example #13
0
 /**
  * @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;
 }
Example #14
0
 /**
  * @param string $tableName
  */
 private function givenIHaveARecordOfTable($tableName)
 {
     $this->record = $this->rm->getTable($tableName)->createRecord();
 }
Example #15
0
 /**
  * @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);
 }
Example #16
0
 /**
  * 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);
 }
Example #17
0
 /**
  * @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;
 }
Example #18
0
 /**
  * @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);
 }
 /**
  * @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);
 }