1

I am having trouble understanding how I should go about unit testing a method which utilizes \Guzzle\Common\Event and has no return. I have a function

public function setRequest(\Guzzle\Common\Event $e) {
    $e['request']->getQuery()->set('key', $this->getKey());
}

I cannot get the methods described at Guzzles mock object documentation to produce a successful test. What all needs to be mocked for this to work? getQuery() is part of the \Guzzle\Http\Message\Request I guess? What about the set()?

Edit: What I did so far is this, but I don't know if this is the correct approach. It succeeds, but it does not assert any test. I don't know that I can if the method is not returning anything.

public function testSetRequest()
{
    $collection = $this->getMock('Guzzle\\Common\\Collection');
    $collection->expects($this->once())
        ->method('set')
        ->will($this->returnValue(array('key' => 321)));

    $request = $this->getMockBuilder('Guzzle\\Http\\Message\\Request')
        ->disableOriginalConstructor()
        ->getMock();
    $request->expects($this->once())
        ->method('getQuery')
        ->will($this->returnValue($collection));

    $event = $this->getMock('Guzzle\\Common\\Event');
    $event->expects($this->once())
        ->method('offsetGet')
        ->with('request')
        ->will($this->returnValue($request));

    $result = $this->place->setRequest($event);
}

I tracked set() down to the guzzle common collection. btw, $this->place simply refers to the instance of the object being tested, set in the setUp() function.

  • You probably should add some code as well how so far you tried to write that test (even as you said it failed). Even if it fails it often helps to understand the concrete problem you're facing. And yes, the magic with ArrayAccess in Guzzle\Common\Event makes it hard to easily understand it. You find a list which arguments exist for each event here: http://guzzlephp.org/guide/http/creating_plugins.html – hakre Oct 03 '13 at 17:26
  • Also you might be interested to read about: [*Mocking/Stubbing an Object of a class that implements arrayaccess in PHPUnit*](http://stackoverflow.com/q/10607173/367456) – hakre Oct 03 '13 at 17:28

0 Answers0