Пример #1
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);
 }
Пример #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);
    }