Пример #1
0
 private function hydrateOneToManySubResource(Resource $referencedResource, array $row)
 {
     $relationshipRows = $row[$referencedResource->getResourceUniqueIdentifier()];
     if ($relationshipRows === null) {
         return array($referencedResource->getAlias() => array());
     }
     return $this->hydrate($relationshipRows, $referencedResource);
 }
Пример #2
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage cannot find relationship for referenced resource alias 'myUnknownResource'
  */
 public function test_that_getting_a_relationship_by_unknown_referenced_resource_alias_throws_exception()
 {
     $rootResource = new Resource('my__0', 'myCoolResource', 'my_cool_table', 'my_cool_id');
     $referencedResource = new Resource('my__1', 'myOtherResource', 'my_other_table', 'my_cool_id');
     $relationship = new Relationship($rootResource, $referencedResource, 'many-to-one', 'my_other_table.cool_id = my_cool_table.id');
     $rootResource->addRelationship($relationship);
     $this->assertEquals($relationship, $rootResource->getRelationshipByReferencedResourceAlias('myUnknownResource'));
 }
Пример #3
0
 private function buildFromJoinsClause(Resource $resource)
 {
     $joinsClause = '';
     if ($resource->hasRelationships()) {
         $relationships = $resource->getRelationships();
         /** @var \FlyRRM\Mapping\Relationship $rel */
         foreach ($relationships as $rel) {
             if ($rel->getType() === Relationship::TYPE_MANY_TO_ONE) {
                 $referencedResource = $rel->getReferencedResource();
                 $mainResource = $rel->getMainResource();
                 $joinCondition = $mainResource->getResourceUniqueIdentifier() . '.' . $rel->getJoinColumn() . ' = ' . $referencedResource->getResourceUniqueIdentifier() . '.' . $referencedResource->getPrimaryKey();
                 $joinsClause .= ' left outer join ' . $referencedResource->getTable() . ' ' . $referencedResource->getResourceUniqueIdentifier() . ' on ' . $joinCondition . $this->buildFromJoinsClause($referencedResource);
             }
         }
     }
     return $joinsClause;
 }
Пример #4
0
 public function test_data_extractor_with_two_to_many_resource()
 {
     $rootResource = new Resource('my__1', 'myCoolResource', 'my_cool_table', 'id');
     $rootField = new Field($rootResource, 'myCoolField', 'my_cool_field', Field::TYPE_STRING);
     $rootResource->addField($rootField);
     $referencedResource = new Resource('my__2', 'myHotResource', 'my_hot_table', 'id');
     $coolField = new Field($referencedResource, 'myCoolField', 'my_cool_field', Field::TYPE_STRING);
     $referencedResource->addField($coolField);
     $relationship = new Relationship($rootResource, $referencedResource, 'one-to-many', 'cool_id');
     $rootResource->addRelationship($relationship);
     $thirdResource = new Resource('my__3', 'myThirdResource', 'my_third_table', 'id');
     $thirdField = new Field($thirdResource, 'myThirdField', 'my_third_field', Field::TYPE_STRING);
     $thirdResource->addField($thirdField);
     $thirdRelationship = new Relationship($rootResource, $thirdResource, 'one-to-many', 'cool_id');
     $rootResource->addRelationship($thirdRelationship);
     $mainSql = 'select my__1.id as my__1_id, my__1.my_cool_field as my__1_myCoolField from my_cool_table my__1';
     $mainData = array(array('my__1_id' => 1, 'my__1_myCoolField' => 'hello'), array('my__1_id' => 2, 'my__1_myCoolField' => 'world!'));
     $toManyData1 = array(array('my__2_id' => 1, 'my__2_myHotField' => 'clean'), array('my__2_id' => 2, 'my__2_myHotField' => 'code'));
     $toManyData2 = array(array('my__2_id' => 5, 'my__2_myHotField' => 'tdd'), array('my__2_id' => 6, 'my__2_myHotField' => 'rulez'));
     $thirdToManyData = array(array('my__3_id' => 1, 'my__3_myThirdField' => 'ugly'), array('my__3_id' => 2, 'my__3_myThirdField' => 'bugs'));
     $thirdToManyData2 = array(array('my__3_id' => 3, 'my__3_myThirdField' => 'fix'), array('my__3_id' => 4, 'my__3_myThirdField' => 'me'));
     $toManySqls = 'select my__2.id as my__2_id, my__2.my_hot_field as my__2_myHotField from my_hot_table my__2 where my__2.cool_id = :my__1_id';
     $toManySqlsThird = 'select my__3.id as my__3_id, my__3.my_third_field as my__3_myThirdField from my_third_table my__3 where my__3.cool_id = :my__1_id';
     $queryBuilder = $this->getMock('\\FlyRRM\\QueryBuilding\\QueryBuilder');
     $queryBuilder->expects($this->at(0))->method('buildQuery')->with($this->equalTo($rootResource))->will($this->returnValue($mainSql));
     $queryBuilder->expects($this->at(1))->method('buildToManyQueries')->with($this->equalTo($relationship))->will($this->returnValue($toManySqls));
     $queryBuilder->expects($this->at(2))->method('buildToManyQueries')->with($this->equalTo($thirdRelationship))->will($this->returnValue($toManySqlsThird));
     $queryExecutor = $this->getMock('\\FlyRRM\\QueryExecution\\QueryExecutor');
     $queryExecutor->expects($this->at(0))->method('executeQuery')->with($this->equalTo($mainSql))->will($this->returnValue($mainData));
     $queryExecutor->expects($this->at(1))->method('executeQuery')->with($this->equalTo($toManySqls), $this->equalTo(array(':my__1_id' => 1)))->will($this->returnValue($toManyData1));
     $queryExecutor->expects($this->at(2))->method('executeQuery')->with($this->equalTo($toManySqls), $this->equalTo(array(':my__1_id' => 2)))->will($this->returnValue($toManyData2));
     $queryExecutor->expects($this->at(3))->method('executeQuery')->with($this->equalTo($toManySqlsThird), $this->equalTo(array(':my__1_id' => 1)))->will($this->returnValue($thirdToManyData));
     $queryExecutor->expects($this->at(4))->method('executeQuery')->with($this->equalTo($toManySqlsThird), $this->equalTo(array(':my__1_id' => 2)))->will($this->returnValue($thirdToManyData2));
     $dataExtractor = new DataExtractor($queryBuilder, $queryExecutor);
     $extractedData = $dataExtractor->extractData($rootResource);
     $expectedData = $mainData;
     $expectedData[0]['my__2'] = $toManyData1;
     $expectedData[1]['my__2'] = $toManyData2;
     $expectedData[0]['my__3'] = $thirdToManyData;
     $expectedData[1]['my__3'] = $thirdToManyData2;
     $this->assertEquals($expectedData, $extractedData);
 }
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage relationship must be of type one-to-many
  */
 public function test_that_to_many_queries_without_one_to_many_relationship_throws_exception()
 {
     $mainResource = new Resource('my__0', 'myCoolResource', 'my_cool_table', 'id');
     $mainResourceField = new Field($mainResource, 'myCoolField', 'my_cool_field', Field::TYPE_STRING);
     $mainResource->addField($mainResourceField);
     $referencedResource = new Resource('my__1', 'myHotResource', 'my_hot_table', 'id');
     $referencedResourceField = new Field($referencedResource, 'myHotField', 'my_hot_field', Field::TYPE_STRING);
     $referencedResource->addField($referencedResourceField);
     $relationship = new Relationship($mainResource, $referencedResource, Relationship::TYPE_MANY_TO_ONE, 'hot_id');
     $mainResource->addRelationship($relationship);
     $generatedToManySql = $this->dbalQueryBuilder->buildToManyQueries($relationship);
 }
Пример #6
0
 public function formatSimpleFieldInResource(Resource $resource, $fieldAlias, $fieldValue)
 {
     $fieldObj = $resource->getFieldByAlias($fieldAlias);
     $fieldFormatter = $this->fieldFormatterFactory->buildFieldFormatterForField($fieldObj);
     return $fieldFormatter->format($fieldValue);
 }
Пример #7
0
 public function test_that_a_null_one_to_many_relationship_is_hydrated_to_a_structured_array()
 {
     $mainResource = new Resource('my__0', 'myCoolResource', 'my_cool_table', 'my_cool_id');
     $mainResourceField = new Field($mainResource, 'myCoolField', 'my_cool_field', Field::TYPE_STRING);
     $mainResource->addField($mainResourceField);
     $referencedResource = new Resource('my__1', 'myHotResource', 'my_hot_table', 'my_hot_id');
     $referencedResourceField = new Field($referencedResource, 'myHotField', 'my_hot_field', Field::TYPE_STRING);
     $referencedResource->addField($referencedResourceField);
     $referencedResourceField = new Field($referencedResource, 'myHotField2', 'my_hot_field_2', Field::TYPE_STRING);
     $referencedResource->addField($referencedResourceField);
     $relationship = new Relationship($mainResource, $referencedResource, 'one-to-many', 'cool_id');
     $mainResource->addRelationship($relationship);
     $plainArray = array(array('my__0_myCoolField' => 'my cool value!', 'my__1' => null));
     $expectedStructuredArray = array('myCoolResource' => array(array('myCoolField' => 'my cool value!', 'myHotResource' => array())));
     $resultArray = $this->hydrator->hydrate($plainArray, $mainResource);
     $this->assertEquals($expectedStructuredArray, $resultArray);
 }
 public function test_that_a_structured_data_with_nested_one_to_many_relationship_is_formatted()
 {
     $mainResource = new Resource('my__0', 'myCoolResource', 'my_cool_table', 'id');
     $stringField = new Field($mainResource, 'myCoolField', 'my_cool_field', Field::TYPE_STRING);
     $mainResource->addField($stringField);
     $referencedResource = new Resource('my__1', 'myHotResource', 'my_hot_resource', 'id');
     $dateField = new Field($referencedResource, 'myHotField', 'my_hot_field', Field::TYPE_DATE, 'd/m/Y');
     $referencedResource->addField($dateField);
     $relationship = new Relationship($mainResource, $referencedResource, 'one-to-many', 'cool_id');
     $mainResource->addRelationship($relationship);
     $thirdResource = new Resource('my__2', 'myThirdResource', 'my_third_resource', 'id');
     $thirdField = new Field($thirdResource, 'myThirdField', 'my_third_field', Field::TYPE_NUMBER);
     $thirdResource->addField($thirdField);
     $thirdRelationship = new Relationship($referencedResource, $thirdResource, 'one-to-many', 'hot_id');
     $referencedResource->addRelationship($thirdRelationship);
     $structuredData = array('myCoolResource' => array(array('myCoolField' => 'my cool value!', 'myHotResource' => array(array('myHotField' => new \DateTime('2014-02-18'), 'myThirdResource' => array(array('myThirdField' => 10), array('myThirdField' => 20), array('myThirdField' => 30))), array('myHotField' => new \DateTime('2014-02-19'), 'myThirdResource' => array(array('myThirdField' => 10.1))), array('myHotField' => new \DateTime('2014-02-20'), 'myThirdResource' => array())))));
     $expectedData = array('myCoolResource' => array(array('myCoolField' => 'my cool value!', 'myHotResource' => array(array('myHotField' => '18/02/2014', 'myThirdResource' => array(array('myThirdField' => 10), array('myThirdField' => 20), array('myThirdField' => 30))), array('myHotField' => '19/02/2014', 'myThirdResource' => array(array('myThirdField' => 10.1))), array('myHotField' => '20/02/2014', 'myThirdResource' => array())))));
     $outputtedData = $this->arrayFormatter->format($structuredData, $mainResource);
     $this->assertEquals($expectedData, $outputtedData);
 }
    public function test_that_a_resource_with_one_related_many_to_one_is_mapped()
    {
        $input = <<<EOT
resource:
    alias: 'myResource'
    table: 'myTable'
    primary-key: 'my_id'

    fields:
        -
            name: 'myName'
            alias: 'myAlias'

    relationships:
        -
            type: 'many-to-one'
            join-column: 'idMyTable'

            resource:
                alias: 'myOtherResource'
                table: 'myOtherTable'
                primary-key: 'my_other_id'

                fields:
                    -
                        name: 'myOtherField'
                        alias: 'myOtherAlias'
EOT;
        /** @var \FlyRRM\Mapping\Resource $rootResource */
        $rootResource = $this->parseInput($input);
        $relationships = $rootResource->getRelationships();
        $this->assertEquals(1, sizeof($relationships));
        /** @var \FlyRRM\Mapping\Relationship $firstRelationship */
        $firstRelationship = $relationships[0];
        $expectedMainResource = new Resource('myT_0', 'myResource', 'myTable', 'my_id');
        $expectedMainResourceFields = array(new Field($expectedMainResource, 'myAlias', 'myName', Field::TYPE_STRING));
        $expectedMainResource->addFieldsArray($expectedMainResourceFields);
        $expectedReferencedResource = new Resource('myO_1', 'myOtherResource', 'myOtherTable', 'my_other_id');
        $expectedReferencedResourceFields = array(new Field($expectedReferencedResource, 'myOtherAlias', 'myOtherField', Field::TYPE_STRING));
        $expectedReferencedResource->addFieldsArray($expectedReferencedResourceFields);
        $expectedRelationship = new Relationship($expectedMainResource, $expectedReferencedResource, 'many-to-one', 'idMyTable');
        $expectedMainResource->addRelationship($expectedRelationship);
        $this->assertEquals($expectedRelationship, $firstRelationship);
    }