From cffcdc933fe50c7fd776f587a47b71c93f7f1f02 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Wed, 21 Mar 2012 21:23:07 +0100 Subject: [PATCH 1/3] Improve the TypeGuesser to match the latest Sf2.1 --- .../Bridge/Propel1/Form/PropelTypeGuesser.php | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Propel1/Form/PropelTypeGuesser.php b/src/Symfony/Bridge/Propel1/Form/PropelTypeGuesser.php index 91560c5ba9..840f140050 100644 --- a/src/Symfony/Bridge/Propel1/Form/PropelTypeGuesser.php +++ b/src/Symfony/Bridge/Propel1/Form/PropelTypeGuesser.php @@ -107,8 +107,17 @@ class PropelTypeGuesser implements FormTypeGuesserInterface */ public function guessMaxLength($class, $property) { - if (($column = $this->getColumn($class, $property)) && $column->isText()) { - return new ValueGuess($column->getSize(), Guess::HIGH_CONFIDENCE); + if ($column = $this->getColumn($class, $property)) { + if ($column->isText()) { + return new ValueGuess($column->getSize(), Guess::HIGH_CONFIDENCE); + } + switch ($column->getType()) { + case \PropelColumnTypes::FLOAT: + case \PropelColumnTypes::REAL: + case \PropelColumnTypes::DOUBLE: + case \PropelColumnTypes::DECIMAL: + return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE); + } } } @@ -117,6 +126,15 @@ class PropelTypeGuesser implements FormTypeGuesserInterface */ public function guessMinLength($class, $property) { + if ($column = $this->getColumn($class, $property)) { + switch ($column->getType()) { + case \PropelColumnTypes::FLOAT: + case \PropelColumnTypes::REAL: + case \PropelColumnTypes::DOUBLE: + case \PropelColumnTypes::DECIMAL: + return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE); + } + } } protected function getTable($class) From 897a389ffeaaffc5089bd33ce9337c7f69b1d600 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Sun, 15 Apr 2012 18:26:58 +0200 Subject: [PATCH 2/3] =?UTF-8?q?[Propel1]=C2=A0Added=20tests=20for=20the=20?= =?UTF-8?q?PropelTypeGuesser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Bridge/Propel1/Tests/Fixtures/Column.php | 59 +++++++++++++++ .../Bridge/Propel1/Tests/Fixtures/Item.php | 10 ++- .../Propel1/Tests/Fixtures/ItemQuery.php | 26 +++++++ .../Tests/Form/PropelTypeGuesserTest.php | 74 +++++++++++++++++++ 4 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Bridge/Propel1/Tests/Fixtures/Column.php create mode 100644 src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php diff --git a/src/Symfony/Bridge/Propel1/Tests/Fixtures/Column.php b/src/Symfony/Bridge/Propel1/Tests/Fixtures/Column.php new file mode 100644 index 0000000000..6b03977e20 --- /dev/null +++ b/src/Symfony/Bridge/Propel1/Tests/Fixtures/Column.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Propel1\Tests\Fixtures; + +class Column +{ + private $name; + + private $type; + + public function __construct($name, $type) + { + $this->name = $name; + $this->type = $type; + } + + public function getType() + { + return $this->type; + } + + public function isText() + { + if (!$this->type) { + return false; + } + + switch ($this->type) { + case \PropelColumnTypes::CHAR: + case \PropelColumnTypes::VARCHAR: + case \PropelColumnTypes::LONGVARCHAR: + case \PropelColumnTypes::BLOB: + case \PropelColumnTypes::CLOB: + case \PropelColumnTypes::CLOB_EMU: + return true; + } + + return false; + } + + public function getSize() + { + return $this->isText() ? 255 : 0; + } + + public function isNotNull() + { + return ('id' === $this->name); + } +} diff --git a/src/Symfony/Bridge/Propel1/Tests/Fixtures/Item.php b/src/Symfony/Bridge/Propel1/Tests/Fixtures/Item.php index e10e111bf2..70705b6c86 100644 --- a/src/Symfony/Bridge/Propel1/Tests/Fixtures/Item.php +++ b/src/Symfony/Bridge/Propel1/Tests/Fixtures/Item.php @@ -21,11 +21,14 @@ class Item implements \Persistent private $groupName; - public function __construct($id = null, $value = null, $groupName = null) + private $price; + + public function __construct($id = null, $value = null, $groupName = null, $price = null) { $this->id = $id; $this->value = $value; $this->groupName = $groupName; + $this->price = $price; } public function getId() @@ -48,6 +51,11 @@ class Item implements \Persistent return $this->groupName; } + public function getPrice() + { + return $this->price; + } + public function getPrimaryKey() { return $this->getId(); diff --git a/src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php b/src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php index f06c850583..adc155546c 100644 --- a/src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php +++ b/src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php @@ -13,6 +13,12 @@ namespace Symfony\Bridge\Propel1\Tests\Fixtures; class ItemQuery { + private $map = array( + 'id' => \PropelColumnTypes::INTEGER, + 'value' => \PropelColumnTypes::VARCHAR, + 'price' => \PropelColumnTypes::FLOAT, + ); + public function getTableMap() { // Allows to define methods in this class @@ -24,4 +30,24 @@ class ItemQuery { return array('id'); } + + /** + * Method from the TableMap API + */ + public function hasColumn($column) + { + return in_array($column, array_keys($this->map)); + } + + /** + * Method from the TableMap API + */ + public function getColumn($column) + { + if ($this->hasColumn($column)) { + return new Column($column, $this->map[$column]); + } + + return null; + } } diff --git a/src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php b/src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php new file mode 100644 index 0000000000..9b8eb72578 --- /dev/null +++ b/src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php @@ -0,0 +1,74 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Propel1\Tests\Form; + +use Symfony\Bridge\Propel1\Form\PropelTypeGuesser; +use Symfony\Bridge\Propel1\Tests\Propel1TestCase; + +class PropelTypeGuesserTest extends Propel1TestCase +{ + const CLASS_NAME = 'Symfony\Bridge\Propel1\Tests\Fixtures\Item'; + + private $guesser; + + public function setUp() + { + $this->guesser = new PropelTypeGuesser(); + } + + public function testGuessMaxLengthWithText() + { + $value = $this->guesser->guessMaxLength(self::CLASS_NAME, 'value'); + + $this->assertNotNull($value); + $this->assertEquals(255, $value->getValue()); + } + + public function testGuessMaxLengthWithFloat() + { + $value = $this->guesser->guessMaxLength(self::CLASS_NAME, 'price'); + + $this->assertNotNull($value); + $this->assertNull($value->getValue()); + } + + public function testGuessMinLengthWithText() + { + $value = $this->guesser->guessMinLength(self::CLASS_NAME, 'value'); + + $this->assertNull($value); + } + + public function testGuessMinLengthWithFloat() + { + $value = $this->guesser->guessMinLength(self::CLASS_NAME, 'price'); + + $this->assertNotNull($value); + $this->assertNull($value->getValue()); + } + + public function testGuessRequired() + { + $value = $this->guesser->guessRequired(self::CLASS_NAME, 'id'); + + $this->assertNotNull($value); + $this->assertTrue($value->getValue()); + } + + public function testGuessRequiredWithNullableColumn() + { + $value = $this->guesser->guessRequired(self::CLASS_NAME, 'value'); + + $this->assertNotNull($value); + $this->assertFalse($value->getValue()); + } +} From b7c2d3da89c53ee366335d2751e3da8f2a0194e4 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Sun, 15 Apr 2012 18:54:29 +0200 Subject: [PATCH 3/3] =?UTF-8?q?[Propel1]=C2=A0Added=20tests=20for=20guessT?= =?UTF-8?q?ype()=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Propel1/Tests/Fixtures/ItemQuery.php | 17 +++++-- .../Tests/Form/PropelTypeGuesserTest.php | 45 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php b/src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php index adc155546c..75d2a4f731 100644 --- a/src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php +++ b/src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php @@ -14,9 +14,12 @@ namespace Symfony\Bridge\Propel1\Tests\Fixtures; class ItemQuery { private $map = array( - 'id' => \PropelColumnTypes::INTEGER, - 'value' => \PropelColumnTypes::VARCHAR, - 'price' => \PropelColumnTypes::FLOAT, + 'id' => \PropelColumnTypes::INTEGER, + 'value' => \PropelColumnTypes::VARCHAR, + 'price' => \PropelColumnTypes::FLOAT, + 'is_active' => \PropelColumnTypes::BOOLEAN, + 'enabled' => \PropelColumnTypes::BOOLEAN_EMU, + 'updated_at' => \PropelColumnTypes::TIMESTAMP, ); public function getTableMap() @@ -50,4 +53,12 @@ class ItemQuery return null; } + + /** + * Method from the TableMap API + */ + public function getRelations() + { + return array(); + } } diff --git a/src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php b/src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php index 9b8eb72578..7fcbd4f711 100644 --- a/src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php +++ b/src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php @@ -13,11 +13,14 @@ namespace Symfony\Bridge\Propel1\Tests\Form; use Symfony\Bridge\Propel1\Form\PropelTypeGuesser; use Symfony\Bridge\Propel1\Tests\Propel1TestCase; +use Symfony\Component\Form\Guess\Guess; class PropelTypeGuesserTest extends Propel1TestCase { const CLASS_NAME = 'Symfony\Bridge\Propel1\Tests\Fixtures\Item'; + const UNKNOWN_CLASS_NAME = 'Symfony\Bridge\Propel1\Tests\Fixtures\UnknownItem'; + private $guesser; public function setUp() @@ -71,4 +74,46 @@ class PropelTypeGuesserTest extends Propel1TestCase $this->assertNotNull($value); $this->assertFalse($value->getValue()); } + + public function testGuessTypeWithoutTable() + { + $value = $this->guesser->guessType(self::UNKNOWN_CLASS_NAME, 'property'); + + $this->assertNotNull($value); + $this->assertEquals('text', $value->getType()); + $this->assertEquals(Guess::LOW_CONFIDENCE, $value->getConfidence()); + } + + public function testGuessTypeWithoutColumn() + { + $value = $this->guesser->guessType(self::CLASS_NAME, 'property'); + + $this->assertNotNull($value); + $this->assertEquals('text', $value->getType()); + $this->assertEquals(Guess::LOW_CONFIDENCE, $value->getConfidence()); + } + + /** + * @dataProvider dataProviderForGuessType + */ + public function testGuessType($property, $type, $confidence) + { + $value = $this->guesser->guessType(self::CLASS_NAME, $property); + + $this->assertNotNull($value); + $this->assertEquals($type, $value->getType()); + $this->assertEquals($confidence, $value->getConfidence()); + } + + static public function dataProviderForGuessType() + { + return array( + array('is_active', 'checkbox', Guess::HIGH_CONFIDENCE), + array('enabled', 'checkbox', Guess::HIGH_CONFIDENCE), + array('id', 'integer', Guess::MEDIUM_CONFIDENCE), + array('value', 'text', Guess::MEDIUM_CONFIDENCE), + array('price', 'number', Guess::MEDIUM_CONFIDENCE), + array('updated_at', 'datetime', Guess::HIGH_CONFIDENCE), + ); + } }