Testing for Guzzle model structure.
<?php
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Mock\MockPlugin;
use Guzzle\Service\Client;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Tests\GuzzleTestCase;
class ModelTest extends GuzzleTestCase {
public function testModel(){
// Define a service with a test() method
$service = ServiceDescription::factory( array(
'name' => 'test-service',
'operations' => array (
'test' => array (
'uri' => '/test.json',
'httpMethod' => 'GET',
'responseClass' => 'TestModel',
),
),
'models' => array (
'TestModel' => array (
'type' => 'object',
'properties' => array (
// property that will exist in response
'foo' => array (
'type' => 'integer',
'location' => 'json',
),
// property that won't exist in response
'bar' => array (
'type' => 'integer',
'location' => 'json',
'required' => true, // <- makes no difference
),
),
),
),
) );
$client = new Client;
$client->setDescription( $service );
// fake a response with valid "foo" and invalid "baz" properties
$plugin = new MockPlugin();
$plugin->addResponse( new Response( 200, array(), '{"foo":1,"baz":"nan"}' ) );
$client->addSubscriber( $plugin );
$response = $client->test();
// test value of "foo" key, which will exist
$this->assertEquals( 1, $response->get('foo') );
// test value of "bar" key which isn't in response
// Why doesn't the model complain this is missing in response?
$this->assertEquals( null, $response->get('bar') ); -
// test value of "baz" key, which isn't in model schema
// Why doesn't this fail if it's not valid?
$this->assertEquals( "nan", $response->get('baz') );
// test model structure is present
// Why is this empty?
$structure = $response->getStructure()->toArray();
$this->assertGreaterThan( 0, count($structure), 'Model structure is empty' );
}
}