FormFieldTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DomCrawler\Tests\Field;
  11. use Symfony\Component\DomCrawler\Field\InputFormField;
  12. class FormFieldTest extends FormFieldTestCase
  13. {
  14. public function testGetName()
  15. {
  16. $node = $this->createNode('input', '', ['type' => 'text', 'name' => 'name', 'value' => 'value']);
  17. $field = new InputFormField($node);
  18. $this->assertEquals('name', $field->getName(), '->getName() returns the name of the field');
  19. }
  20. public function testGetSetHasValue()
  21. {
  22. $node = $this->createNode('input', '', ['type' => 'text', 'name' => 'name', 'value' => 'value']);
  23. $field = new InputFormField($node);
  24. $this->assertEquals('value', $field->getValue(), '->getValue() returns the value of the field');
  25. $field->setValue('foo');
  26. $this->assertEquals('foo', $field->getValue(), '->setValue() sets the value of the field');
  27. $this->assertTrue($field->hasValue(), '->hasValue() always returns true');
  28. }
  29. public function testLabelReturnsNullIfNoneIsDefined()
  30. {
  31. $dom = new \DOMDocument();
  32. $dom->loadHTML('<html><form><input type="text" id="foo" name="foo" value="foo" /><input type="submit" /></form></html>');
  33. $field = new InputFormField($dom->getElementById('foo'));
  34. $this->assertNull($field->getLabel(), '->getLabel() returns null if no label is defined');
  35. }
  36. public function testLabelIsAssignedByForAttribute()
  37. {
  38. $dom = new \DOMDocument();
  39. $dom->loadHTML('<html><form>
  40. <label for="foo">Foo label</label>
  41. <input type="text" id="foo" name="foo" value="foo" />
  42. <input type="submit" />
  43. </form></html>');
  44. $field = new InputFormField($dom->getElementById('foo'));
  45. $this->assertEquals('Foo label', $field->getLabel()->textContent, '->getLabel() returns the associated label');
  46. }
  47. public function testLabelIsAssignedByParentingRelation()
  48. {
  49. $dom = new \DOMDocument();
  50. $dom->loadHTML('<html><form>
  51. <label for="foo">Foo label<input type="text" id="foo" name="foo" value="foo" /></label>
  52. <input type="submit" />
  53. </form></html>');
  54. $field = new InputFormField($dom->getElementById('foo'));
  55. $this->assertEquals('Foo label', $field->getLabel()->textContent, '->getLabel() returns the parent label');
  56. }
  57. }