Skip to content

Commit 28a9fb6

Browse files
committed
feature #31959 [DomCrawler][Feature][DX] Add Form::getName() method (JustBlackBird)
This PR was squashed before being merged into the 4.4 branch (closes #31959). Discussion ---------- [DomCrawler][Feature][DX] Add Form::getName() method | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | None | License | MIT | Doc PR | symfony/symfony-docs#11705 The PR adds `Symfony\Component\DomCrawler\Form::getName` method. The method is actually a syntax sugar but can improve DX when dealing with tests. For example, in the snippet ```php $client = static::createClient(); $crawler = $client->request('GET', '/post/hello-world'); $form = $crawler->selectButton('submit')->form(); $form['my_form[name]'] = 'Fabien'; $form['my_form[subject]'] = 'Symfony rocks!'; ``` the prefix in field name (`my_form`) is form name, which is generated by Symfony automatically. The method, added in the PR helps to get that name in a most obvious way. Commits ------- ff53cb462a [DomCrawler][Feature][DX] Add Form::getName() method
2 parents d8acf7f + a205470 commit 28a9fb6

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* Added `Form::getName()` method.
8+
49
4.3.0
510
-----
611

Form.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ public function getMethod()
250250
return $this->node->getAttribute('method') ? strtoupper($this->node->getAttribute('method')) : 'GET';
251251
}
252252

253+
/**
254+
* Gets the form name.
255+
*
256+
* If no name is defined on the form, an empty string is returned.
257+
*/
258+
public function getName(): string
259+
{
260+
return $this->node->getAttribute('name');
261+
}
262+
253263
/**
254264
* Returns true if the named field exists.
255265
*

Tests/FormTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ public function testGetMethodWithOverride()
327327
$this->assertEquals('POST', $form->getMethod(), '->getMethod() returns the method attribute value of the form');
328328
}
329329

330+
public function testGetName()
331+
{
332+
$form = $this->createForm('<form name="foo"><input type="submit" /></form>');
333+
$this->assertSame('foo', $form->getName());
334+
}
335+
336+
public function testGetNameOnFormWithoutName()
337+
{
338+
$form = $this->createForm('<form><input type="submit" /></form>');
339+
$this->assertSame('', $form->getName());
340+
}
341+
330342
public function testGetSetValue()
331343
{
332344
$form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');

0 commit comments

Comments
 (0)