Skip to content

Commit

Permalink
RootForm::httpValue() now return the current button value
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed Feb 24, 2021
1 parent b0c9884 commit 0a4514b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Aggregate/RootForm.php
Expand Up @@ -135,7 +135,19 @@ public function value()
*/
public function httpValue()
{
return $this->form->httpValue();
$httpValue = $this->form->httpValue();

if (empty($this->buttons)) {
return $httpValue;
}

$httpValue = (array) $httpValue;

foreach ($this->buttons as $btn) {
$httpValue += $btn->toHttp();
}

return $httpValue;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Button/ButtonInterface.php
Expand Up @@ -36,6 +36,16 @@ public function clicked(): bool;
*/
public function submit($data): bool;

/**
* Get the http value of the button
*
* If the button is not clicked, should return an empty array
* Else, should return an associative array with the button name as key associated to the button value
*
* @return array
*/
public function toHttp(): array;

/**
* Get the constraint groups related the the button
*
Expand Down
12 changes: 12 additions & 0 deletions src/Button/SubmitButton.php
Expand Up @@ -79,6 +79,18 @@ public function submit($data): bool
return $this->clicked = isset($data[$this->name]) && (string) $data[$this->name] === $this->value;
}

/**
* {@inheritdoc}
*/
public function toHttp(): array
{
if (!$this->clicked) {
return [];
}

return [$this->name => $this->value];
}

/**
* {@inheritdoc}
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/Aggregate/RootFormTest.php
Expand Up @@ -52,6 +52,12 @@ public function test_submit_with_button()

$this->assertSame($btn1, $root->submitButton());
$this->assertEquals(['btn1'], $root->constraintGroups());
$this->assertEquals([
'firstName' => 'John',
'lastName' => 'Smith',
'id' => '4',
'btn1' => 'ok',
], $root->httpValue());

$root->submit([
'firstName' => 'John',
Expand All @@ -62,6 +68,12 @@ public function test_submit_with_button()

$this->assertSame($btn2, $root->submitButton());
$this->assertEquals(['btn2'], $root->constraintGroups());
$this->assertEquals([
'firstName' => 'John',
'lastName' => 'Smith',
'id' => '4',
'btn2' => 'ok',
], $root->httpValue());

$root->submit([
'firstName' => 'John',
Expand All @@ -72,6 +84,11 @@ public function test_submit_with_button()

$this->assertNull($root->submitButton());
$this->assertEquals([Constraint::DEFAULT_GROUP], $root->constraintGroups());
$this->assertEquals([
'firstName' => 'John',
'lastName' => 'Smith',
'id' => '4',
], $root->httpValue());

$root->submit([
'firstName' => 'John',
Expand All @@ -81,6 +98,12 @@ public function test_submit_with_button()
]);

$this->assertEquals([Constraint::DEFAULT_GROUP], $root->constraintGroups());
$this->assertEquals([
'firstName' => 'John',
'lastName' => 'Smith',
'id' => '4',
'btn3' => 'ok',
], $root->httpValue());
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/Button/SubmitButtonBuilderTest.php
Expand Up @@ -85,6 +85,10 @@ public function constraintGroups(): array
{
}

public function toHttp(): array
{
}

public function view(?HttpFieldPath $parent = null): ButtonViewInterface
{
// TODO: Implement view() method.
Expand Down
22 changes: 22 additions & 0 deletions tests/Button/SubmitButtonTest.php
Expand Up @@ -50,6 +50,28 @@ public function test_submit_with_integer()
$this->assertTrue($btn->submit(['btn' => 1]));
}

/**
*
*/
public function test_toHttp()
{
$btn = new SubmitButton('btn', 'aaa');

$this->assertSame([], $btn->toHttp());

$btn->submit(null);
$this->assertSame([], $btn->toHttp());

$btn->submit([]);
$this->assertSame([], $btn->toHttp());

$btn->submit(['btn' => 'bbb']);
$this->assertSame([], $btn->toHttp());

$btn->submit(['btn' => 'aaa']);
$this->assertSame(['btn' => 'aaa'], $btn->toHttp());
}

/**
*
*/
Expand Down

0 comments on commit 0a4514b

Please sign in to comment.