2011-02-27 22:32:13 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Guzzle\Tests\Common;
|
|
|
|
|
|
|
|
use Guzzle\Common\Collection;
|
2012-04-21 00:23:07 -07:00
|
|
|
use Guzzle\Service\Inspector;
|
|
|
|
use Guzzle\Service\Exception\ValidationException;
|
2011-02-27 22:32:13 -06:00
|
|
|
|
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector
|
2011-02-27 22:32:13 -06:00
|
|
|
*
|
2012-01-14 13:57:05 -06:00
|
|
|
* @guzzle test type="type:object"
|
2011-02-27 22:32:13 -06:00
|
|
|
* @guzzle bool_1 default="true" type="boolean"
|
|
|
|
* @guzzle bool_2 default="false"
|
|
|
|
* @guzzle float type="float"
|
|
|
|
* @guzzle int type="integer"
|
|
|
|
* @guzzle date type="date"
|
2012-01-14 13:57:05 -06:00
|
|
|
* @guzzle timestamp type="time"
|
2011-02-27 22:32:13 -06:00
|
|
|
* @guzzle string type="string"
|
2012-01-14 13:57:05 -06:00
|
|
|
* @guzzle username required="true" filters="strtolower"
|
2011-02-28 22:30:22 -06:00
|
|
|
* @guzzle dynamic default="{{username}}_{{ string }}_{{ does_not_exist }}"
|
2012-01-14 13:57:05 -06:00
|
|
|
* @guzzle test_function type="string" filters="Guzzle\Tests\Common\InspectorTest::strtoupper"
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
2012-01-14 13:57:05 -06:00
|
|
|
class InspectorTest extends \Guzzle\Tests\GuzzleTestCase
|
2011-02-27 22:32:13 -06:00
|
|
|
{
|
2012-01-14 13:57:05 -06:00
|
|
|
public static function strtoupper($string)
|
2011-02-27 22:32:13 -06:00
|
|
|
{
|
2012-01-14 13:57:05 -06:00
|
|
|
return strtoupper($string);
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector::validateClass
|
2012-04-21 00:23:07 -07:00
|
|
|
* @expectedException Guzzle\Service\Exception\ValidationException
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
|
|
|
public function testValidatesRequiredArgs()
|
|
|
|
{
|
2011-03-27 17:46:59 -05:00
|
|
|
Inspector::getInstance()->validateClass(__CLASS__, new Collection());
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
2011-03-20 17:51:54 -05:00
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @cover Guzzle\Service\Inspector::__constructor
|
|
|
|
*/
|
|
|
|
public function testRegistersDefaultFilters()
|
|
|
|
{
|
|
|
|
$inspector = new Inspector();
|
|
|
|
$this->assertNotEmpty($inspector->getRegisteredConstraints());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers Guzzle\Service\Inspector::prepareConfig
|
2011-03-20 17:51:54 -05:00
|
|
|
*/
|
|
|
|
public function testPreparesConfig()
|
|
|
|
{
|
|
|
|
$c = Inspector::prepareConfig(array(
|
|
|
|
'a' => '123',
|
|
|
|
'base_url' => 'http://www.test.com/'
|
|
|
|
), array(
|
|
|
|
'a' => 'xyz',
|
|
|
|
'b' => 'lol'
|
|
|
|
), array('a'));
|
|
|
|
|
2011-04-13 12:44:53 -05:00
|
|
|
$this->assertInstanceOf('Guzzle\Common\Collection', $c);
|
2011-03-20 17:51:54 -05:00
|
|
|
$this->assertEquals(array(
|
|
|
|
'a' => '123',
|
|
|
|
'b' => 'lol',
|
|
|
|
'base_url' => 'http://www.test.com/'
|
|
|
|
), $c->getAll());
|
2011-03-23 14:19:14 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
$c = Inspector::prepareConfig(null, null, array('a'));
|
|
|
|
$this->fail('Exception not throw when missing config');
|
2012-04-21 00:23:07 -07:00
|
|
|
} catch (ValidationException $e) {
|
2011-03-23 14:19:14 -05:00
|
|
|
}
|
2011-03-20 17:51:54 -05:00
|
|
|
}
|
|
|
|
|
2011-02-27 22:32:13 -06:00
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
|
|
|
public function testAddsDefaultAndInjectsConfigs()
|
|
|
|
{
|
|
|
|
$col = new Collection(array(
|
|
|
|
'username' => 'user',
|
2011-02-28 22:30:22 -06:00
|
|
|
'string' => 'test',
|
|
|
|
'float' => 1.23
|
2011-02-27 22:32:13 -06:00
|
|
|
));
|
|
|
|
|
2011-02-28 22:30:22 -06:00
|
|
|
Inspector::getInstance()->validateClass(__CLASS__, $col);
|
|
|
|
$this->assertEquals(false, $col->get('bool_2'));
|
|
|
|
$this->assertEquals('user_test_', $col->get('dynamic'));
|
|
|
|
$this->assertEquals(1.23, $col->get('float'));
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector::validateClass
|
2012-04-21 00:23:07 -07:00
|
|
|
* @expectedException Guzzle\Service\Exception\ValidationException
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
|
|
|
public function testValidatesTypeHints()
|
|
|
|
{
|
|
|
|
Inspector::getInstance()->validateClass(__CLASS__, new Collection(array(
|
2011-02-28 22:30:22 -06:00
|
|
|
'test' => 'uh oh',
|
|
|
|
'username' => 'test'
|
2011-02-27 22:32:13 -06:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector::validateClass
|
|
|
|
* @covers Guzzle\Service\Inspector::validateConfig
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
|
|
|
public function testConvertsBooleanDefaults()
|
|
|
|
{
|
|
|
|
$c = new Collection(array(
|
2011-02-28 22:30:22 -06:00
|
|
|
'test' => $this,
|
|
|
|
'username' => 'test'
|
2011-02-27 22:32:13 -06:00
|
|
|
));
|
|
|
|
|
|
|
|
Inspector::getInstance()->validateClass(__CLASS__, $c);
|
|
|
|
|
|
|
|
$this->assertTrue($c->get('bool_1'));
|
|
|
|
$this->assertFalse($c->get('bool_2'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
|
|
|
public function testInspectsClassArgs()
|
|
|
|
{
|
|
|
|
$doc = <<<EOT
|
|
|
|
/**
|
|
|
|
* Client for interacting with the Unfuddle webservice
|
|
|
|
*
|
|
|
|
* @guzzle username required="true" doc="API username" type="string"
|
|
|
|
* @guzzle password required="true" doc="API password" type="string"
|
|
|
|
* @guzzle subdomain required="true" doc="Unfuddle project subdomain" type="string"
|
2012-01-14 13:57:05 -06:00
|
|
|
* @guzzle api_version required="true" default="v1" doc="API version" type="choice:'v1','v2',v3"
|
2011-02-27 22:32:13 -06:00
|
|
|
* @guzzle protocol required="true" default="https" doc="HTTP protocol (http or https)" type="string"
|
|
|
|
* @guzzle base_url required="true" default="{{ protocol }}://{{ subdomain }}.unfuddle.com/api/{{ api_version }}/" doc="Unfuddle API base URL" type="string"
|
2012-01-14 13:57:05 -06:00
|
|
|
* @guzzle class type="type:object"
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
$params = Inspector::getInstance()->parseDocBlock($doc);
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'required' => 'true',
|
|
|
|
'doc' => 'API username',
|
|
|
|
'type' => 'string'
|
|
|
|
), $params['username']);
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'required' => 'true',
|
|
|
|
'default' => 'v1',
|
|
|
|
'doc' => 'API version',
|
2012-01-14 13:57:05 -06:00
|
|
|
'type' => "choice:'v1','v2',v3"
|
2011-02-27 22:32:13 -06:00
|
|
|
), $params['api_version']);
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'required' => 'true',
|
|
|
|
'default' => 'https',
|
|
|
|
'doc' => 'HTTP protocol (http or https)',
|
|
|
|
'type' => 'string'
|
|
|
|
), $params['protocol']);
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'required' => 'true',
|
|
|
|
'default' => '{{ protocol }}://{{ subdomain }}.unfuddle.com/api/{{ api_version }}/',
|
|
|
|
'doc' => 'Unfuddle API base URL',
|
|
|
|
'type' => 'string'
|
|
|
|
), $params['base_url']);
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
2012-01-14 13:57:05 -06:00
|
|
|
'type' => "type:object"
|
2011-02-27 22:32:13 -06:00
|
|
|
), $params['class']);
|
|
|
|
|
2011-02-28 22:30:22 -06:00
|
|
|
$config = new Collection(array(
|
|
|
|
'username' => 'test',
|
|
|
|
'password' => 'pass',
|
2012-01-14 13:57:05 -06:00
|
|
|
'subdomain' => 'sub',
|
|
|
|
'api_version' => 'v2'
|
2011-02-28 22:30:22 -06:00
|
|
|
));
|
|
|
|
|
|
|
|
Inspector::getInstance()->validateConfig($params, $config);
|
|
|
|
|
|
|
|
// make sure the configs were injected
|
2012-01-14 13:57:05 -06:00
|
|
|
$this->assertEquals('https://sub.unfuddle.com/api/v2/', $config->get('base_url'));
|
2011-02-28 22:30:22 -06:00
|
|
|
|
2011-02-27 22:32:13 -06:00
|
|
|
try {
|
|
|
|
Inspector::getInstance()->validateConfig($params, new Collection(array(
|
|
|
|
'base_url' => '',
|
|
|
|
'username' => '',
|
|
|
|
'password' => '',
|
2012-01-14 13:57:05 -06:00
|
|
|
'class' => '123',
|
|
|
|
'api_version' => 'v10'
|
2011-02-27 22:32:13 -06:00
|
|
|
)));
|
|
|
|
$this->fail('Expected exception not thrown when params are invalid');
|
2012-04-21 00:23:07 -07:00
|
|
|
} catch (ValidationException $e) {
|
2012-04-20 17:30:39 -07:00
|
|
|
|
|
|
|
$concat = $e->getMessage();
|
|
|
|
$this->assertContains("Validation errors: Requires that the username argument be supplied. (API username)", $concat);
|
|
|
|
$this->assertContains("Requires that the password argument be supplied. (API password)", $concat);
|
|
|
|
$this->assertContains("Requires that the subdomain argument be supplied. (Unfuddle project subdomain)", $concat);
|
2012-05-06 19:08:27 -07:00
|
|
|
$this->assertContains("Value must be one of: v1, v2, v3", $concat);
|
|
|
|
$this->assertContains("Value must be of type object", $concat);
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector::registerConstraint
|
|
|
|
* @covers Guzzle\Service\Inspector::getConstraint
|
|
|
|
* @covers Guzzle\Service\Inspector::getRegisteredConstraints
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
2012-01-14 13:57:05 -06:00
|
|
|
public function testRegistersCustomConstraints()
|
2011-02-27 22:32:13 -06:00
|
|
|
{
|
2012-05-06 19:08:27 -07:00
|
|
|
$constraintClass = 'Guzzle\\Common\\Validation\\Ip';
|
2011-02-27 22:32:13 -06:00
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
Inspector::getInstance()->registerConstraint('mock', $constraintClass);
|
|
|
|
Inspector::getInstance()->registerConstraint('mock_2', $constraintClass, array(
|
|
|
|
'version' => '4'
|
|
|
|
));
|
2011-02-27 22:32:13 -06:00
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
$this->assertArrayHasKey('mock', Inspector::getInstance()->getRegisteredConstraints());
|
|
|
|
$this->assertArrayHasKey('mock_2', Inspector::getInstance()->getRegisteredConstraints());
|
|
|
|
|
|
|
|
$this->assertInstanceOf($constraintClass, Inspector::getInstance()->getConstraint('mock'));
|
|
|
|
$this->assertInstanceOf($constraintClass, Inspector::getInstance()->getConstraint('mock_2'));
|
2011-02-27 22:32:13 -06:00
|
|
|
|
|
|
|
$validating = new Collection(array(
|
2012-01-14 13:57:05 -06:00
|
|
|
'data' => '192.168.16.121',
|
|
|
|
'test' => '10.1.1.0'
|
2011-02-27 22:32:13 -06:00
|
|
|
));
|
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
$this->assertTrue(Inspector::getInstance()->validateConfig(array(
|
2011-02-27 22:32:13 -06:00
|
|
|
'data' => array(
|
|
|
|
'type' => 'mock'
|
|
|
|
),
|
|
|
|
'test' => array(
|
|
|
|
'type' => 'mock_2'
|
|
|
|
)
|
2012-01-14 13:57:05 -06:00
|
|
|
), $validating, false));
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector
|
2011-02-27 22:32:13 -06:00
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testChecksFilterValidity()
|
|
|
|
{
|
|
|
|
Inspector::getInstance()->validateConfig(array(
|
|
|
|
'data' => array(
|
|
|
|
'type' => 'invalid'
|
|
|
|
)
|
|
|
|
), new Collection(array(
|
|
|
|
'data' => 'false'
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
|
|
|
public function testValidatesArgs()
|
|
|
|
{
|
2011-02-28 22:30:22 -06:00
|
|
|
$config = new Collection(array(
|
|
|
|
'data' => 123,
|
|
|
|
'min' => 'a',
|
|
|
|
'max' => 'aaa'
|
|
|
|
));
|
|
|
|
|
2011-02-27 22:32:13 -06:00
|
|
|
$result = Inspector::getInstance()->validateConfig(array(
|
|
|
|
'data' => array(
|
|
|
|
'type' => 'string'
|
|
|
|
),
|
|
|
|
'min' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
'min_length' => 2
|
|
|
|
),
|
|
|
|
'max' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
'max_length' => 2
|
|
|
|
)
|
2011-02-28 22:30:22 -06:00
|
|
|
), $config, false);
|
2011-02-27 22:32:13 -06:00
|
|
|
|
2012-04-20 17:30:39 -07:00
|
|
|
$concat = implode("\n", $result);
|
2012-05-06 19:08:27 -07:00
|
|
|
$this->assertContains("Value must be of type string", $concat);
|
2012-04-20 17:30:39 -07:00
|
|
|
$this->assertContains("Requires that the min argument be >= 2 characters", $concat);
|
|
|
|
$this->assertContains("Requires that the max argument be <= 2 characters", $concat);
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
2011-11-13 16:46:37 -06:00
|
|
|
|
|
|
|
/**
|
2012-01-14 13:57:05 -06:00
|
|
|
* @covers Guzzle\Service\Inspector::parseDocBlock
|
2011-11-13 16:46:37 -06:00
|
|
|
*/
|
|
|
|
public function testVerifiesGuzzleAnnotations()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
array(),
|
|
|
|
Inspector::getInstance()->parseDocBlock('testing')
|
|
|
|
);
|
|
|
|
}
|
2012-01-14 13:57:05 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers Guzzle\Service\Inspector::validateConfig
|
|
|
|
*/
|
|
|
|
public function testRunsValuesThroughFilters()
|
|
|
|
{
|
|
|
|
$data = new Collection(array(
|
|
|
|
'username' => 'TEST',
|
|
|
|
'test_function' => 'foo'
|
|
|
|
));
|
|
|
|
Inspector::getInstance()->validateClass(__CLASS__, $data);
|
|
|
|
$this->assertEquals('test', $data->get('username'));
|
|
|
|
$this->assertEquals('FOO', $data->get('test_function'));
|
|
|
|
}
|
2012-05-06 19:08:27 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers Guzzle\Service\Inspector::setTypeValidation
|
|
|
|
* @covers Guzzle\Service\Inspector::validateConfig
|
|
|
|
*/
|
|
|
|
public function testTypeValidationCanBeDisabled()
|
|
|
|
{
|
|
|
|
$i = Inspector::getInstance();
|
|
|
|
$i->setTypeValidation(false);
|
|
|
|
|
|
|
|
// Ensure that the type is not validated
|
|
|
|
$i->validateConfig(array(
|
|
|
|
'data' => array(
|
|
|
|
'type' => 'string'
|
|
|
|
)
|
|
|
|
), new Collection(array(
|
|
|
|
'data' => new \stdClass()
|
|
|
|
)), true);
|
|
|
|
|
|
|
|
$i->setTypeValidation(true);
|
|
|
|
|
|
|
|
// Ensure that nothing is validated
|
|
|
|
$i->validateConfig(array(
|
|
|
|
'data' => array(
|
|
|
|
'type' => 'string'
|
|
|
|
)
|
|
|
|
), new Collection(array(
|
|
|
|
'data' => new \stdClass()
|
|
|
|
)), true, false);
|
|
|
|
}
|
2012-04-21 00:23:07 -07:00
|
|
|
}
|