This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Form/CollectionFieldTest.php
Bernhard Schussek fb1f99137d [Form] Changed semantics of a "bound" form
A form now always has to be bound, independent of whether the request is a POST request or not. The bind() method detects itself whether the request was a post request or not and reads its data accordingly. The "old" bind()/isBound() methods were renamed to submit()/isSubmitted().

	$form = new Form('author');
	$form->bind($request, $author);

	if ($form->isValid()) {
		// isValid() implies isSubmitted(), non-submitted forms can
		// never be valid
		// do something with author now
	}

Alternatively, you can only bind global variables, if you don't have a request object.

	$form->bindGlobals($author);

Note that the $author object is in both cases optional. You can also pass no object at all and read the data using $form->getData(), but then no validation will occur. You can also prefill the form with an object during instantiation.

	$form = new Form('author', array('data' => $author));
	$form->bind($request);

	// etc.
2011-02-01 15:27:12 +01:00

149 lines
5.3 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Form;
require_once __DIR__ . '/Fixtures/TestField.php';
use Symfony\Component\Form\CollectionField;
use Symfony\Component\Form\Form;
use Symfony\Tests\Component\Form\Fixtures\TestField;
class CollectionFieldTest extends \PHPUnit_Framework_TestCase
{
public function testContainsNoFieldsByDefault()
{
$field = new CollectionField(new TestField('emails'));
$this->assertEquals(0, count($field));
}
public function testSetDataAdjustsSize()
{
$field = new CollectionField(new TestField('emails'));
$field->setData(array('foo@foo.com', 'foo@bar.com'));
$this->assertTrue($field[0] instanceof TestField);
$this->assertTrue($field[1] instanceof TestField);
$this->assertEquals(2, count($field));
$this->assertEquals('foo@foo.com', $field[0]->getData());
$this->assertEquals('foo@bar.com', $field[1]->getData());
$field->setData(array('foo@baz.com'));
$this->assertTrue($field[0] instanceof TestField);
$this->assertFalse(isset($field[1]));
$this->assertEquals(1, count($field));
$this->assertEquals('foo@baz.com', $field[0]->getData());
}
public function testSetDataAdjustsSizeIfModifiable()
{
$field = new CollectionField(new TestField('emails'), array(
'modifiable' => true,
));
$field->setData(array('foo@foo.com', 'foo@bar.com'));
$this->assertTrue($field[0] instanceof TestField);
$this->assertTrue($field[1] instanceof TestField);
$this->assertTrue($field['$$key$$'] instanceof TestField);
$this->assertEquals(3, count($field));
$field->setData(array('foo@baz.com'));
$this->assertTrue($field[0] instanceof TestField);
$this->assertFalse(isset($field[1]));
$this->assertTrue($field['$$key$$'] instanceof TestField);
$this->assertEquals(2, count($field));
}
public function testThrowsExceptionIfObjectIsNotTraversable()
{
$field = new CollectionField(new TestField('emails'));
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$field->setData(new \stdClass());
}
public function testModifiableCollectionsContainExtraField()
{
$field = new CollectionField(new TestField('emails'), array(
'modifiable' => true,
));
$field->setData(array('foo@bar.com'));
$this->assertTrue($field['0'] instanceof TestField);
$this->assertTrue($field['$$key$$'] instanceof TestField);
$this->assertEquals(2, count($field));
}
public function testNotResizedIfSubmittedWithMissingData()
{
$field = new CollectionField(new TestField('emails'));
$field->setData(array('foo@foo.com', 'bar@bar.com'));
$field->submit(array('foo@bar.com'));
$this->assertTrue($field->has('0'));
$this->assertTrue($field->has('1'));
$this->assertEquals('foo@bar.com', $field[0]->getData());
$this->assertEquals(null, $field[1]->getData());
}
public function testResizedIfSubmittedWithMissingDataAndModifiable()
{
$field = new CollectionField(new TestField('emails'), array(
'modifiable' => true,
));
$field->setData(array('foo@foo.com', 'bar@bar.com'));
$field->submit(array('foo@bar.com'));
$this->assertTrue($field->has('0'));
$this->assertFalse($field->has('1'));
$this->assertEquals('foo@bar.com', $field[0]->getData());
}
public function testNotResizedIfSubmittedWithExtraData()
{
$field = new CollectionField(new TestField('emails'));
$field->setData(array('foo@bar.com'));
$field->submit(array('foo@foo.com', 'bar@bar.com'));
$this->assertTrue($field->has('0'));
$this->assertFalse($field->has('1'));
$this->assertEquals('foo@foo.com', $field[0]->getData());
}
public function testResizedUpIfSubmittedWithExtraDataAndModifiable()
{
$field = new CollectionField(new TestField('emails'), array(
'modifiable' => true,
));
$field->setData(array('foo@bar.com'));
$field->submit(array('foo@foo.com', 'bar@bar.com'));
$this->assertTrue($field->has('0'));
$this->assertTrue($field->has('1'));
$this->assertEquals('foo@foo.com', $field[0]->getData());
$this->assertEquals('bar@bar.com', $field[1]->getData());
$this->assertEquals(array('foo@foo.com', 'bar@bar.com'), $field->getData());
}
public function testResizedDownIfSubmittedWithLessDataAndModifiable()
{
$field = new CollectionField(new TestField('emails'), array(
'modifiable' => true,
));
$field->setData(array('foo@bar.com', 'bar@bar.com'));
$field->submit(array('foo@foo.com'));
$this->assertTrue($field->has('0'));
$this->assertFalse($field->has('1'));
$this->assertEquals('foo@foo.com', $field[0]->getData());
$this->assertEquals(array('foo@foo.com'), $field->getData());
}
}