Skip to content

Commit

Permalink
Merge pull request #135 from danizord/hotfix/idempotent-validation
Browse files Browse the repository at this point in the history
Do not mutate command at validation.
  • Loading branch information
Konafets authored Feb 20, 2017
2 parents 05cb2c6 + 2b191b1 commit 09f5904
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
6 changes: 0 additions & 6 deletions src/Handler/ValidatedDescriptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ public function __invoke(callable $handler)

if (! $this->validator->validate($schema, $value)) {
$errors = array_merge($errors, $this->validator->getErrors());
} elseif ($value !== $command[$name]) {
// Update the config value if it changed and no validation
// errors were encountered
$command[$name] = $value;
}
}

Expand All @@ -64,8 +60,6 @@ public function __invoke(callable $handler)
$params->setName($name);
if (! $this->validator->validate($params, $value)) {
$errors = array_merge($errors, $this->validator->getErrors());
} elseif ($value !== $command[$name]) {
$command[$name] = $value;
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Handler/ValidatedDescriptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace GuzzleHttp\Tests\Command\Guzzle\Handler;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;

Expand Down Expand Up @@ -109,4 +110,41 @@ public function testFilterBeforeValidate()
$client = new GuzzleClient(new HttpClient(), $description);
$client->foo(['bar' => new \DateTimeImmutable()]); // Should not throw any exception
}

public function testValidationDoesNotMutateCommand()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'parameters' => [
'bar' => [
'location' => 'query',
'type' => 'string',
'filters' => ['json_encode'],
'required' => true,
]
]
]
]
]);

$client = new GuzzleClient(new HttpClient(), $description);
$command = new Command('foo', ['bar' => ['baz' => 'bat']]);

$paramsBeforeValidation = $command->toArray();

$client->getHandlerStack()->after('validate_description', function (callable $next) use ($paramsBeforeValidation) {
return function (Command $command) use ($next, $paramsBeforeValidation) {
$paramsAfterValidation = $command->toArray();

$this->assertSame($paramsBeforeValidation, $paramsAfterValidation);

return $next($command);
};
});

$client->execute($command);
}
}

0 comments on commit 09f5904

Please sign in to comment.