コード例 #1
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);
 }
コード例 #2
0
    public function test_end_to_end_components_integration()
    {
        $yamlMapping = <<<EOT
resource:
    alias: 'impiegati'
    table: 'employees'
    primary-key: 'id'

    fields:
        -
            name: 'name'
            alias: 'nome'

        -
            name: 'surname'
            alias: 'cognome'

        -
            name: 'code'
            alias: 'codice'

        -
            name: 'birthday'
            alias: 'dataNascita'
            type: 'date'

        -
            name: 'created'
            alias: 'creatoIl'
            type: 'datetime'

        -
            name: 'edited'
            alias: 'modificatoIl'
            type: 'datetime'

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

            resource:
                alias: 'utenteCreazione'
                table: 'users'
                primary-key: 'id'

                fields:
                    -
                        name: 'username'
                        alias: 'Username'

                    -
                        name: 'firstname'
                        alias: 'Nome'

                    -
                        name: 'lastname'
                        alias: 'Cognome'


        -
            type: 'many-to-one'
            join-column: 'id_user_last_edit'

            resource:
                alias: 'utenteUltimaModifica'
                table: 'users'
                primary-key: 'id'

                fields:
                    -
                        name: 'username'
                        alias: 'Username'

                    -
                        name: 'firstname'
                        alias: 'Nome'

                    -
                        name: 'lastname'
                        alias: 'Cognome'

        -
            type: 'many-to-one'
            join-column: 'id_contract'

            resource:
                alias: 'contratto'
                table: 'contracts'
                primary-key: 'id'

                fields:
                    -
                        name: 'name'
                        alias: 'nome'

                    -
                        name: 'created'
                        alias: 'creatoIl'
                        type: 'datetime'

                    -
                        name: 'edited'
                        alias: 'modificatoIl'
                        type: 'datetime'

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

                        resource:
                            alias: 'utenteCreazione'
                            table: 'users'
                            primary-key: 'id'

                            fields:
                                -
                                    name: 'username'
                                    alias: 'Username'

                                -
                                    name: 'firstname'
                                    alias: 'Nome'

                                -
                                    name: 'lastname'
                                    alias: 'Cognome'


                    -
                        type: 'many-to-one'
                        join-column: 'id_user_last_edit'

                        resource:
                            alias: 'utenteUltimaModifica'
                            table: 'users'
                            primary-key: 'id'

                            fields:
                                -
                                    name: 'username'
                                    alias: 'Username'

                                -
                                    name: 'firstname'
                                    alias: 'Nome'

                                -
                                    name: 'lastname'
                                    alias: 'Cognome'

        -
            type: 'one-to-many'
            join-column: 'id_employee'

            resource:
                alias: 'pagamenti'
                table: 'payments'
                primary-key: 'id'

                fields:
                    -
                        name: 'value'
                        alias: 'Valore'
                        type: 'number'

                relationships:

                    -
                        type: 'many-to-one'
                        join-column: 'id_bank'

                        resource:
                            alias: 'banca'
                            table: 'banks'
                            primary-key: 'id'

                            fields:
                                -
                                    name: 'name'
                                    alias: 'Nome'

EOT;
        $parser = new YamlMappingParser();
        $resource = $parser->parse($yamlMapping);
        $dbalQueryBuilder = new DBALQueryBuilder();
        $databaseConf = new DatabaseConfiguration();
        $databaseConf->setDatabaseName($GLOBALS['DB_DBNAME']);
        $databaseConf->setDriver('pdo_mysql');
        $databaseConf->setHost($GLOBALS['DB_HOST']);
        $databaseConf->setPassword($GLOBALS['DB_PASSWD']);
        $databaseConf->setPort($GLOBALS['DB_PORT']);
        $databaseConf->setUsername($GLOBALS['DB_USER']);
        $queryExecutor = new DBALQueryExecutor($databaseConf);
        $dataExtractor = new DataExtractor($dbalQueryBuilder, $queryExecutor);
        $plainData = $dataExtractor->extractData($resource);
        $dataHydrator = new ArrayHydrator(new FieldHydrationConcreteFactory());
        $hydratedData = $dataHydrator->hydrate($plainData, $resource);
        $expectedData = array('impiegati' => array(0 => array('nome' => 'Mario', 'cognome' => 'Rossi', 'codice' => 'ABC002', 'dataNascita' => new \DateTime('1987-04-07'), 'creatoIl' => new \DateTime('2014-02-10 14:41:23'), 'modificatoIl' => new \DateTime('2014-02-10 14:44:59'), 'utenteCreazione' => array('Username' => 'g.white', 'Nome' => 'Gabriel', 'Cognome' => 'White'), 'utenteUltimaModifica' => array('Username' => 'admin', 'Nome' => 'Mark', 'Cognome' => 'Green'), 'contratto' => array('nome' => 'full-time', 'creatoIl' => new \DateTime('2014-02-01 10:29:50'), 'modificatoIl' => new \DateTime('2014-02-01 10:29:59'), 'utenteCreazione' => array('Username' => 'admin', 'Nome' => 'Mark', 'Cognome' => 'Green'), 'utenteUltimaModifica' => array('Username' => 'g.white', 'Nome' => 'Gabriel', 'Cognome' => 'White')), 'pagamenti' => array(array('Valore' => 1000.0, 'banca' => array('Nome' => 'global bank')), array('Valore' => 2000.0, 'banca' => array('Nome' => 'global bank')), array('Valore' => 3000.0, 'banca' => array('Nome' => 'global bank')))), 1 => array('nome' => 'Giovanni', 'cognome' => 'Verdi', 'codice' => null, 'dataNascita' => null, 'creatoIl' => new \DateTime('2014-02-10 14:50:23'), 'modificatoIl' => null, 'utenteCreazione' => array('Username' => 'admin', 'Nome' => 'Mark', 'Cognome' => 'Green'), 'utenteUltimaModifica' => null, 'contratto' => array('nome' => 'intern', 'creatoIl' => new \DateTime('2014-02-01 10:29:12'), 'modificatoIl' => null, 'utenteCreazione' => array('Username' => 'admin', 'Nome' => 'Mark', 'Cognome' => 'Green'), 'utenteUltimaModifica' => null), 'pagamenti' => array(array('Valore' => 1500, 'banca' => array('Nome' => 'money back bank'))))));
        $this->assertEquals($expectedData, $hydratedData);
    }