merged branch willdurand/propel-type-guesser (PR #3953)

Commits
-------

b7c2d3d [Propel1] Added tests for guessType() method
897a389 [Propel1] Added tests for the PropelTypeGuesser
cffcdc9 Improve the TypeGuesser to match the latest Sf2.1

Discussion
----------

[Form] Propel type guesser

Thanks to @vicb, the PropelTypeGuesser has been updated. I've added unit tests to prove his improvement, and everything is ok from my point of view.

---------------------------------------------------------------------------

by willdurand at 2012-04-15T16:38:09Z

Well, I made the changes, but I really don't care about fixing these comments.

To write `assertNull` doesn't improve readabilty, as I expect to read the returned value in the test. And, it's better to read `null` than to read the assertion method. Moreover, that makes the test suite inconsistent, as you are not able to read each tests the same way :)

---------------------------------------------------------------------------

by vicb at 2012-04-15T17:20:01Z

Great ! thanks @willdurand
This commit is contained in:
Fabien Potencier 2012-04-18 09:22:19 +02:00
commit a721f1b61d
5 changed files with 244 additions and 3 deletions

View File

@ -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)

View File

@ -0,0 +1,59 @@
<?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 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);
}
}

View File

@ -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();

View File

@ -13,6 +13,15 @@ namespace Symfony\Bridge\Propel1\Tests\Fixtures;
class ItemQuery
{
private $map = array(
'id' => \PropelColumnTypes::INTEGER,
'value' => \PropelColumnTypes::VARCHAR,
'price' => \PropelColumnTypes::FLOAT,
'is_active' => \PropelColumnTypes::BOOLEAN,
'enabled' => \PropelColumnTypes::BOOLEAN_EMU,
'updated_at' => \PropelColumnTypes::TIMESTAMP,
);
public function getTableMap()
{
// Allows to define methods in this class
@ -24,4 +33,32 @@ 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;
}
/**
* Method from the TableMap API
*/
public function getRelations()
{
return array();
}
}

View File

@ -0,0 +1,119 @@
<?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 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()
{
$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());
}
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),
);
}
}