Skip to content

Commit

Permalink
Fixing command exception throwing, fixing validating additionalParame…
Browse files Browse the repository at this point in the history
…ters, adding validation tests. Closes guzzle#43
  • Loading branch information
mtdowling committed Aug 8, 2014
1 parent d6d04e2 commit 873a6da
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.idea
.DS_STORE
coverage
phpunit.xml
composer.lock
vendor/
artifacts/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ before_script:
- composer self-update
- composer install --no-interaction --prefer-source --dev

script: vendor/bin/phpunit
script: make test
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
all: clean test

test:
vendor/bin/phpunit

coverage:
vendor/bin/phpunit --coverage-html=artifacts/coverage

view-coverage:
open artifacts/coverage/index.html

clean:
rm -rf artifacts/*

.PHONY: coverage
5 changes: 2 additions & 3 deletions src/Subscriber/ValidateInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function onPrepare(PrepareEvent $event)
}

if ($params = $operation->getAdditionalParameters()) {
foreach ($command as $name => $value) {
foreach ($command->toArray() as $name => $value) {
// It's only additional if it isn't defined in the schema
if (!$operation->hasParam($name)) {
// Always set the name so that error messages are useful
Expand All @@ -68,8 +68,7 @@ public function onPrepare(PrepareEvent $event)
if ($errors) {
throw new CommandException(
'Validation errors: ' . implode("\n", $errors),
$event->getClient(),
$command
$event->getTransaction()
);
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/DescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace GuzzleHttp\Tests\Command\Guzzle;

use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\SchemaFormatter;

/**
* @covers GuzzleHttp\Command\Guzzle\Description
Expand Down
9 changes: 4 additions & 5 deletions tests/GuzzleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GuzzleHttp\Tests\Command\Guzzle;

use GuzzleHttp\Client;
use GuzzleHttp\Command\CommandTransaction;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Command\Event\PrepareEvent;
use GuzzleHttp\Command\Exception\CommandException;
Expand Down Expand Up @@ -139,17 +140,15 @@ public function testPassesCommandExceptionsThrough()
$command->getEmitter()->on('prepare', function(PrepareEvent $event) {
throw new CommandException(
'foo',
$event->getClient(),
$event->getCommand(),
$event->getRequest()
$event->getTransaction()
);
}, 1);
$guzzle->execute($command);
}

/**
* @expectedException \GuzzleHttp\Command\Exception\CommandException
* @expectedExceptionMessage Error executing command: msg
* @expectedException \Exception
* @expectedExceptionMessage msg
*/
public function testWrapsExceptionsInCommandExceptions()
{
Expand Down
114 changes: 114 additions & 0 deletions tests/Subscriber/ValidateInputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace GuzzleHttp\Tests\Command\Guzzle;

use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\CommandTransaction;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\Subscriber\ValidateInput;
use GuzzleHttp\Command\Event\PrepareEvent;

/**
* @covers GuzzleHttp\Command\Guzzle\Subscriber\ValidateInput
*/
class ValidateInputTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \GuzzleHttp\Command\Exception\CommandException
* @expectedExceptionMessage Validation errors: [bar] is a required string
*/
public function testValidates()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j',
'parameters' => [
'bar' => [
'type' => 'string',
'required' => true
]
]
]
]
]);

$client = new GuzzleClient(new Client(), $description);
$val = new ValidateInput();
$event = new PrepareEvent(new CommandTransaction(
$client,
$client->getCommand('foo')
));
$val->onPrepare($event);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage is not a GuzzleHttp\Command\Guzzle\GuzzleCommandInterface
*/
public function testEnsuresCorrectCommandType()
{
$val = new ValidateInput();
$client = $this->getMockBuilder('GuzzleHttp\Command\ServiceClientInterface')
->getMockForAbstractClass();
$command = $this->getMockBuilder('GuzzleHttp\Command\CommandInterface')
->getMockForAbstractClass();
$val->onPrepare(new PrepareEvent(new CommandTransaction(
$client,
$command
)));
}

public function testSuccessfulValidationDoesNotThrow()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j',
'parameters' => []
]
]
]);

$client = new GuzzleClient(new Client(), $description);
$val = new ValidateInput();
$event = new PrepareEvent(new CommandTransaction(
$client,
$client->getCommand('foo')
));
$val->onPrepare($event);
}

/**
* @expectedException \GuzzleHttp\Command\Exception\CommandException
* @expectedExceptionMessage Validation errors: [bar] must be of type string
*/
public function testValidatesAdditionalParameters()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j',
'additionalParameters' => [
'type' => 'string'
]
]
]
]);

$client = new GuzzleClient(new Client(), $description);
$val = new ValidateInput();
$event = new PrepareEvent(new CommandTransaction(
$client,
$client->getCommand('foo', ['bar' => new \stdClass()])
));
$val->onPrepare($event);
}
}

0 comments on commit 873a6da

Please sign in to comment.