/** * @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; }
public function testGetDelegatedFieldsOnNotInitializedParents() { $manager = $this->rm->getTable('manager')->createRecord(); $this->assertFalse($manager->firstname); $this->assertFalse($manager->lastname); $this->assertFalse($manager->salary); }
/** * @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]; }
/** * @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); }
/** * @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); }
/** * 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; }
/** * @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; }
/** * 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); }
/** * @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(); }
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)); }
/** * @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; }
/** * @param array $recordData */ private function givenIHaveARecordStoredWithData(array $recordData) { $this->storedRecord = self::getRecordWithRandomData($this->table, $recordData); $this->rm->scheduleSave($this->storedRecord)->commit(); }
/** * @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; }
/** * @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); }
/** * @param array $userData */ private function givenIHaveAUserRecordWithData(array $userData) { $this->record = $this->rm->getOrCreateRecord('user', $userData); }
/** * @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); }
/** * @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); }
/** * @expectedException \Dive\Collection\CollectionException */ public function testAddWrongRecordTypeWillThrowException() { $table = $this->rm->getTable('author'); $author = $table->createRecord(); $this->userColl->add($author); }
/** * 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); }
/** * @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; }
/** * 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; }
/** * @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); }