Вход Регистрация
Файл: symfony-2.7/src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderTest.php
Строк: 392
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyComponentValidatorTestsMappingLoader;

use 
DoctrineCommonAnnotationsAnnotationReader;
use 
SymfonyComponentValidatorConstraintsAll;
use 
SymfonyComponentValidatorConstraintsCallback;
use 
SymfonyComponentValidatorConstraintsChoice;
use 
SymfonyComponentValidatorConstraintsCollection;
use 
SymfonyComponentValidatorConstraintsNotNull;
use 
SymfonyComponentValidatorConstraintsRange;
use 
SymfonyComponentValidatorConstraintsTrue;
use 
SymfonyComponentValidatorMappingClassMetadata;
use 
SymfonyComponentValidatorMappingLoaderAnnotationLoader;
use 
SymfonyComponentValidatorTestsFixturesConstraintA;

class 
AnnotationLoaderTest extends PHPUnit_Framework_TestCase
{
    public function 
testLoadClassMetadataReturnsTrueIfSuccessful()
    {
        
$reader = new AnnotationReader();
        
$loader = new AnnotationLoader($reader);
        
$metadata = new ClassMetadata('SymfonyComponentValidatorTestsFixturesEntity');

        
$this->assertTrue($loader->loadClassMetadata($metadata));
    }

    public function 
testLoadClassMetadataReturnsFalseIfNotSuccessful()
    {
        
$loader = new AnnotationLoader(new AnnotationReader());
        
$metadata = new ClassMetadata('stdClass');

        
$this->assertFalse($loader->loadClassMetadata($metadata));
    }

    public function 
testLoadClassMetadata()
    {
        
$loader = new AnnotationLoader(new AnnotationReader());
        
$metadata = new ClassMetadata('SymfonyComponentValidatorTestsFixturesEntity');

        
$loader->loadClassMetadata($metadata);

        
$expected = new ClassMetadata('SymfonyComponentValidatorTestsFixturesEntity');
        
$expected->setGroupSequence(array('Foo''Entity'));
        
$expected->addConstraint(new ConstraintA());
        
$expected->addConstraint(new Callback(array('SymfonyComponentValidatorTestsFixturesCallbackClass''callback')));
        
$expected->addConstraint(new Callback('validateMe'));
        
$expected->addConstraint(new Callback('validateMeStatic'));
        
$expected->addPropertyConstraint('firstName', new NotNull());
        
$expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
        
$expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
        
$expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
        
$expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
            
'foo' => array(new NotNull(), new Range(array('min' => 3))),
            
'bar' => new Range(array('min' => 5)),
        ))));
        
$expected->addPropertyConstraint('firstName', new Choice(array(
            
'message' => 'Must be one of %choices%',
            
'choices' => array('A''B'),
        )));
        
$expected->addGetterConstraint('lastName', new NotNull());
        
$expected->addGetterConstraint('valid', new True());
        
$expected->addGetterConstraint('permissions', new True());

        
// load reflection class so that the comparison passes
        
$expected->getReflectionClass();

        
$this->assertEquals($expected$metadata);
    }

    
/**
     * Test MetaData merge with parent annotation.
     */
    
public function testLoadParentClassMetadata()
    {
        
$loader = new AnnotationLoader(new AnnotationReader());

        
// Load Parent MetaData
        
$parent_metadata = new ClassMetadata('SymfonyComponentValidatorTestsFixturesEntityParent');
        
$loader->loadClassMetadata($parent_metadata);

        
$expected_parent = new ClassMetadata('SymfonyComponentValidatorTestsFixturesEntityParent');
        
$expected_parent->addPropertyConstraint('other', new NotNull());
        
$expected_parent->getReflectionClass();

        
$this->assertEquals($expected_parent$parent_metadata);
    }
    
/**
     * Test MetaData merge with parent annotation.
     */
    
public function testLoadClassMetadataAndMerge()
    {
        
$loader = new AnnotationLoader(new AnnotationReader());

        
// Load Parent MetaData
        
$parent_metadata = new ClassMetadata('SymfonyComponentValidatorTestsFixturesEntityParent');
        
$loader->loadClassMetadata($parent_metadata);

        
$metadata = new ClassMetadata('SymfonyComponentValidatorTestsFixturesEntity');

        
// Merge parent metaData.
        
$metadata->mergeConstraints($parent_metadata);

        
$loader->loadClassMetadata($metadata);

        
$expected_parent = new ClassMetadata('SymfonyComponentValidatorTestsFixturesEntityParent');
        
$expected_parent->addPropertyConstraint('other', new NotNull());
        
$expected_parent->getReflectionClass();

        
$expected = new ClassMetadata('SymfonyComponentValidatorTestsFixturesEntity');
        
$expected->mergeConstraints($expected_parent);

        
$expected->setGroupSequence(array('Foo''Entity'));
        
$expected->addConstraint(new ConstraintA());
        
$expected->addConstraint(new Callback(array('SymfonyComponentValidatorTestsFixturesCallbackClass''callback')));
        
$expected->addConstraint(new Callback('validateMe'));
        
$expected->addConstraint(new Callback('validateMeStatic'));
        
$expected->addPropertyConstraint('firstName', new NotNull());
        
$expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
        
$expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
        
$expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
        
$expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
            
'foo' => array(new NotNull(), new Range(array('min' => 3))),
            
'bar' => new Range(array('min' => 5)),
        ))));
        
$expected->addPropertyConstraint('firstName', new Choice(array(
            
'message' => 'Must be one of %choices%',
            
'choices' => array('A''B'),
        )));
        
$expected->addGetterConstraint('lastName', new NotNull());
        
$expected->addGetterConstraint('valid', new True());
        
$expected->addGetterConstraint('permissions', new True());

        
// load reflection class so that the comparison passes
        
$expected->getReflectionClass();

        
$this->assertEquals($expected$metadata);
    }

    public function 
testLoadGroupSequenceProviderAnnotation()
    {
        
$loader = new AnnotationLoader(new AnnotationReader());

        
$metadata = new ClassMetadata('SymfonyComponentValidatorTestsFixturesGroupSequenceProviderEntity');
        
$loader->loadClassMetadata($metadata);

        
$expected = new ClassMetadata('SymfonyComponentValidatorTestsFixturesGroupSequenceProviderEntity');
        
$expected->setGroupSequenceProvider(true);
        
$expected->getReflectionClass();

        
$this->assertEquals($expected$metadata);
    }
}
Онлайн: 0
Реклама