@@ -29,49 +28,95 @@ public function setUp()
$this->markTestSkipped('The "Validator" component is not available');
}
- $metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
+ $this->metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
- $this->typeGuesser = new ValidatorTypeGuesser($metadataFactory);
+ $this->typeGuesser = new ValidatorTypeGuesser($this->metadataFactory);
}
- public function testGuessMaxLengthForConstraintWithMaxValue()
+ public function testGuessOptionsForConstraintWithMaxLength()
{
- $constraint = new Length(array('max' => '2'));
+ $class = new \stdClass();
- $result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
- $this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
- $this->assertEquals(2, $result->getValue());
- $this->assertEquals(Guess::HIGH_CONFIDENCE, $result->getConfidence());
+ $this->setupMetadata($class, 'foo', array(new Length(array('max' => '2'))));
+
+ $result = $this->typeGuesser->guessAttributes($class, 'foo');
+
+ $this->assertArrayHasKey('maxlength', $result);
+ $this->assertEquals(2, $result['maxlength']->getValue());
+ $this->assertFalse(isset($result['min']));
}
- public function testGuessMaxLengthForConstraintWithMinValue()
+ public function testGuessOptionsForConstraintWithMinLength()
{
- $constraint = new Length(array('min' => '2'));
+ $class = new \stdClass();
+
+ $this->setupMetadata($class, 'foo', array(new Length(array('min' => '2'))));
- $result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
- $this->assertNull($result);
+ $result = $this->typeGuesser->guessAttributes($class, 'foo');
+
+ $this->assertFalse(isset($result['maxlength']));
}
- /**
-* @dataProvider dataProviderTestGuessMaxLengthForConstraintWithType
-*/
- public function testGuessMaxLengthForConstraintWithType($type)
+ public function testGuessOptionsForConstraintWithMinValue()
{
- $constraint = new Type($type);
+ $class = new \stdClass();
+
+ $this->setupMetadata($class, 'foo', array(new Range(array('min' => '2'))));
- $result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
- $this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
- $this->assertEquals(null, $result->getValue());
- $this->assertEquals(Guess::MEDIUM_CONFIDENCE, $result->getConfidence());
+ $result = $this->typeGuesser->guessAttributes($class, 'foo');
+
+ $this->assertArrayHasKey('min', $result);
+ $this->assertEquals(2, $result['min']->getValue());
}
- public static function dataProviderTestGuessMaxLengthForConstraintWithType()
+ public function testGuessOptionsForConstraintWithMaxValue()
{
- return array (
- array('double'),
- array('float'),
- array('numeric'),
- array('real')
- );
+ $class = new \stdClass();
+
+ $this->setupMetadata($class, 'foo', array(new Range(array('max' => '2'))));
+
+ $result = $this->typeGuesser->guessAttributes($class, 'foo');
+
+ $this->assertArrayHasKey('max', $result);
+ $this->assertEquals(2, $result['max']->getValue());
+ }
+
+ public function testGuessOptionsForConstraintWithMinAndMaxValue()
+ {
+ $class = new \stdClass();
+
+ $this->setupMetadata($class, 'foo', array(new Range(array('min' => 1, 'max' => '2'))));
+
+ $result = $this->typeGuesser->guessAttributes($class, 'foo');
+
+ $this->assertArrayHasKey('min', $result);
+ $this->assertEquals(1, $result['min']->getValue());
+ $this->assertArrayHasKey('max', $result);
+ $this->assertEquals(2, $result['max']->getValue());
+ }
+
+ private function setupMetadata($class, $property, array $constraints)
+ {
+ $this->elementMetadata = $this->getMock('Symfony\Component\Validator\Mapping\ElementMetadata');
+ $this->elementMetadata->expects($this->any())
+ ->method('getConstraints')
+ ->will($this->returnValue($constraints));
+
+ $this->metadata = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->metadata->expects($this->any())
+ ->method('hasMemberMetadatas')
+ ->with($property)
+ ->will($this->returnValue(true));
+ $this->metadata->expects($this->any())
+ ->method('getMemberMetadatas')
+ ->with($property)
+ ->will($this->returnValue(array($this->elementMetadata)));
+
+ $this->metadataFactory->expects($this->any())
+ ->method('getMetadataFor')
+ ->with($class)
+ ->will($this->returnValue($this->metadata));
}
}
diff --git a/src/Symfony/Component/Form/Tests/FormFactoryTest.php b/src/Symfony/Component/Form/Tests/FormFactoryTest.php
index a06b49876e1ca..744f5e0d1627c 100644
--- a/src/Symfony/Component/Form/Tests/FormFactoryTest.php
+++ b/src/Symfony/Component/Form/Tests/FormFactoryTest.php
@@ -446,13 +446,13 @@ public function testCreateBuilderCreatesTextFormIfNoGuess()
public function testOptionsCanBeOverridden()
{
$this->guesser1->expects($this->once())
- ->method('guessType')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new TypeGuess(
- 'text',
- array('attr' => array('maxlength' => 10)),
- Guess::MEDIUM_CONFIDENCE
- )));
+ ->method('guessType')
+ ->with('Application\Author', 'firstName')
+ ->will($this->returnValue(new TypeGuess(
+ 'text',
+ array('attr' => array('maxlength' => 10)),
+ Guess::MEDIUM_CONFIDENCE
+ )));
$factory = $this->getMockFactory(array('createNamedBuilder'));
@@ -474,20 +474,20 @@ public function testOptionsCanBeOverridden()
public function testCreateBuilderUsesMaxLengthIfFound()
{
$this->guesser1->expects($this->once())
- ->method('guessMaxLength')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- 15,
- Guess::MEDIUM_CONFIDENCE
- )));
+ ->method('guessAttributes')
+ ->with('Application\Author', 'firstName')
+ ->will($this->returnValue(array('maxlength' => new ValueGuess(
+ 15,
+ Guess::MEDIUM_CONFIDENCE
+ ))));
$this->guesser2->expects($this->once())
- ->method('guessMaxLength')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- 20,
- Guess::HIGH_CONFIDENCE
- )));
+ ->method('guessAttributes')
+ ->with('Application\Author', 'firstName')
+ ->will($this->returnValue(array('maxlength' => new ValueGuess(
+ 20,
+ Guess::HIGH_CONFIDENCE
+ ))));
$factory = $this->getMockFactory(array('createNamedBuilder'));
@@ -504,23 +504,126 @@ public function testCreateBuilderUsesMaxLengthIfFound()
$this->assertEquals('builderInstance', $this->builder);
}
+ public function testCreateBuilderUsesMinAndMaxValueIfFound()
+ {
+ $this->guesser1->expects($this->once())
+ ->method('guessType')
+ ->will($this->returnValue(new TypeGuess(
+ 'integer',
+ array(),
+ Guess::HIGH_CONFIDENCE
+ )));
+
+ $this->guesser1->expects($this->once())
+ ->method('guessAttributes')
+ ->with('Application\Temperature', 'degrees')
+ ->will($this->returnValue(array('min' => new ValueGuess(
+ -276,
+ Guess::HIGH_CONFIDENCE
+ ))));
+
+ $this->guesser2->expects($this->once())
+ ->method('guessAttributes')
+ ->with('Application\Temperature', 'degrees')
+ ->will($this->returnValue(array('max' => new ValueGuess(
+ 100,
+ Guess::HIGH_CONFIDENCE
+ ))));
+
+ $factory = $this->getMockFactory(array('createNamedBuilder'));
+
+ $factory->expects($this->once())
+ ->method('createNamedBuilder')
+ ->with('degrees', 'integer', null, array('attr' => array('min' => -276, 'max' => 100)))
+ ->will($this->returnValue('builderInstance'));
+
+ $this->builder = $factory->createBuilderForProperty(
+ 'Application\Temperature',
+ 'degrees'
+ );
+
+ $this->assertEquals('builderInstance', $this->builder);
+ }
+
+ public function testMinAndMaxAttributesCanBeOverridden()
+ {
+ $this->guesser1->expects($this->once())
+ ->method('guessType')
+ ->will($this->returnValue(new TypeGuess(
+ 'integer',
+ array(),
+ Guess::HIGH_CONFIDENCE
+ )));
+
+ $this->guesser1->expects($this->once())
+ ->method('guessAttributes')
+ ->with('Application\Temperature', 'degrees')
+ ->will($this->returnValue(array('min' => new ValueGuess(
+ -276,
+ Guess::HIGH_CONFIDENCE
+ ))));
+
+ $factory = $this->getMockFactory(array('createNamedBuilder'));
+
+ $factory->expects($this->once())
+ ->method('createNamedBuilder')
+ ->with('degrees', 'integer', null, array('attr' => array('min' => 50)))
+ ->will($this->returnValue('builderInstance'));
+
+ $this->builder = $factory->createBuilderForProperty(
+ 'Application\Temperature',
+ 'degrees',
+ null,
+ array('attr' => array('min' => 50))
+ );
+
+ $this->assertEquals('builderInstance', $this->builder);
+ }
+
+ public function testMinAndMaxAttributesAreNotAddedToTextType()
+ {
+ $this->guesser1->expects($this->once())
+ ->method('guessAttributes')
+ ->with('Application\Author', 'firstName')
+ ->will($this->returnValue(array('min' => new ValueGuess(
+ -276,
+ Guess::HIGH_CONFIDENCE
+ ))));
+
+ $factory = $this->getMockFactory(array('createNamedBuilder'));
+
+ $factory->expects($this->once())
+ ->method('createNamedBuilder')
+ ->with('firstName', 'text', null)
+ ->will($this->returnValue('builderInstance'));
+
+ $this->builder = $factory->createBuilderForProperty(
+ 'Application\Author',
+ 'firstName',
+ null,
+ array('attr' => array('min' => 50))
+ );
+
+ $this->assertEquals('builderInstance', $this->builder);
+ }
+
public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
{
$this->guesser1->expects($this->once())
- ->method('guessRequired')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- true,
- Guess::MEDIUM_CONFIDENCE
- )));
+ ->method('guessRequired')
+ ->with('Application\Author', 'firstName')
+ ->will($this->returnValue(new ValueGuess(
+ true,
+ Guess::MEDIUM_CONFIDENCE
+ )));
$this->guesser2->expects($this->once())
- ->method('guessRequired')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- false,
- Guess::HIGH_CONFIDENCE
- )));
+ ->method('guessRequired')
+ ->with('Application\Author', 'firstName')
+ ->will($this->returnValue(new ValueGuess(
+ false,
+ Guess::HIGH_CONFIDENCE
+ )));
$factory = $this->getMockFactory(array('createNamedBuilder'));
@@ -540,20 +643,20 @@ public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
public function testCreateBuilderUsesPatternIfFound()
{
$this->guesser1->expects($this->once())
- ->method('guessPattern')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- '[a-z]',
- Guess::MEDIUM_CONFIDENCE
- )));
+ ->method('guessAttributes')
+ ->with('Application\Author', 'firstName')
+ ->will($this->returnValue(array('pattern' => new ValueGuess(
+ '[a-z]',
+ Guess::MEDIUM_CONFIDENCE
+ ))));
$this->guesser2->expects($this->once())
- ->method('guessPattern')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- '[a-zA-Z]',
- Guess::HIGH_CONFIDENCE
- )));
+ ->method('guessAttributes')
+ ->with('Application\Author', 'firstName')
+ ->will($this->returnValue(array('pattern' => new ValueGuess(
+ '[a-zA-Z]',
+ Guess::HIGH_CONFIDENCE
+ ))));
$factory = $this->getMockFactory(array('createNamedBuilder'));
@@ -570,6 +673,31 @@ public function testCreateBuilderUsesPatternIfFound()
$this->assertEquals('builderInstance', $this->builder);
}
+ public function testCreateBuilderWithNullAttribute()
+ {
+ $this->guesser1->expects($this->once())
+ ->method('guessAttributes')
+ ->with('Application\Author', 'firstName')
+ ->will($this->returnValue(array('maxlength' => new ValueGuess(
+ null,
+ Guess::MEDIUM_CONFIDENCE
+ ))));
+
+ $factory = $this->getMockFactory(array('createNamedBuilder'));
+
+ $factory->expects($this->once())
+ ->method('createNamedBuilder')
+ ->with('firstName', 'text', null, array())
+ ->will($this->returnValue('builderInstance'));
+
+ $this->builder = $factory->createBuilderForProperty(
+ 'Application\Author',
+ 'firstName'
+ );
+
+ $this->assertEquals('builderInstance', $this->builder);
+ }
+
private function getMockFactory(array $methods = array())
{
return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy