2013-04-26 3 views
1

Кто-нибудь тестирует «Жаль с насмешкой»? Когда я пытаюсь создать макет для клиента жрать я получаю исключение:Mockery and Guzzle

ErrorException: Runtime Notice: Declaration of Mockery_517a66dfe36b0::__call() should be compatible with Guzzle\Service\Client::__call($method, $args = NULL) 

вот код:

$mock = m::mock('\Guzzle\Service\Client'); 

Может кто-нибудь помочь мне с этим?

ответ

5

Если вы хотите создать макет из жрать клиента, почему бы не использовать помощника, предоставленную жрать:

$plugin = new Guzzle\Plugin\Mock\MockPlugin(); 
$plugin->addResponse(new Guzzle\Http\Message\Response(200)); 
$mockedClient = new Guzzle\Http\Client(); 
$mockedClient->addSubscriber($plugin); 

// The following request will get the mock response from the plugin in FIFO order 
$request = $client->get('http://www.test.com/'); 
$request->send(); 

// The MockPlugin maintains a list of requests that were mocked 
$this->assertContainsOnly($request, $plugin->getReceivedRequests()); 

из Unit Testing Guzzle clients

+0

Потому что я не работаю на чистом клиенте. Я передаю его классу mapper с инъекцией зависимости. Я пробую это решение, но это не работает для меня. – Vail

+0

Я считаю, что ссылка, которую вы предоставили, больше не работает. Я нашел эти ссылки для тестирования клиентов Guzzle: http://docs.guzzlephp.org/en/latest/testing/unit-testing.html?highlight=unit%20testing и https://github.com/guzzle/guzzle -docs/blob/master/testing/unit-testing.rst # unit-testing-remote-apis – Onema

+1

Спасибо @onema ссылка обновлена ​​:) –

0

Я нашел обходной путь:

$mock = m::mock('stdClass'); 
$mock->shouldReceive('addPerson')->once()->with(m::contains(1,1))->andReturn(array('id' => 1)); 

$response = new \Guzzle\Http\Message\Response(409); 
$exception = new \Guzzle\Http\Exception\BadResponseException('error message', 409, null); 
$exception->setResponse($response); 
$mock->shouldReceive('addPerson')->once()->with(m::contains(1,2))->andThrow($exception); 

$response = new \Guzzle\Http\Message\Response(404); 
$exception = new \Guzzle\Http\Exception\BadResponseException('error message', 404, null); 
$exception->setResponse($response); 
$mock->shouldReceive('addPerson')->once()->with(m::contains(1,3))->andThrow($exception); 

$mapper = $this->container->get('ftb.clubs.mapper'); 
$mapper->setClient($mock); 

$id = $mapper->addPerson(1, 1); 
$this->assertEquals(
    1, 
    $id 
); 

Do Вы думаете, что это имеет смысл?