0

I have a method in a class that parses some xml. If it finds the tag < status >failure< /status >, it returns an exception.

I want to build a unittest that checks this method does return an exception when the status=failure.

For the moment, I fail to get it done using phpunit and MOCKING?

Example:

<?php
$mock = $this->getMock('Service_Order_Http', array('getResponse'));
        $mock->expects($this->any())
            ->method('getResponse')
            ->will($this->throwException(new Exception()));

        $e = null;
        try {
            $mock->getResponse();
        } catch (Exception $e) {

        }
        $this->assertTrue($e instanceof Exception, "Method getResponse should have thrown an exception");

//phpunit sends back: PHPUnit_Framework_ExpectationFailedException : Failed asserting that exception of type "Exception" is thrown.
?>

Thanks for your help

Marlene75
  • 29
  • 1
  • 4

1 Answers1

4

I think you're misunderstanding the purpose of mocks in unit testing.

A mock is used to substitute a dependency of the class you're actually trying to test.

This is probably worth a read: What is Object Mocking and when do I need it?

I think you're actually looking for something more along these lines with your test:

<?php

    // This is a class that Service_Order_Http depends on.
    // Since we don't want to test the implementation of this class
    // we create a mock of it.
    $dependencyMock = $this->getMock('Dependency_Class');

    // Create an instance of the Service_Order_Http class,
    // passing in the dependency to the constructor (dependency injection).
    $serviceOrderHttp = new Service_Order_Http($dependencyMock);

    // Create or load in some sample XML to test with 
    // that contains the tag you're concerned with
    $sampleXml = "<xml><status>failure</status></xml>";

    // Give the sample XML to $serviceOrderHttp, however that's done
    $serviceOrderHttp->setSource($sampleXml);

    // Set the expectation of the exception
    $this->setExpectedException('Exception');

    // Run the getResponse method.
    // Your test will fail if it doesn't throw
    // the exception.
    $serviceOrderHttp->getResponse();

?>
Community
  • 1
  • 1
Dan
  • 2,212
  • 20
  • 29
  • Thanks Dan, that works, but what about my xml parsing? getResponse() should only return an exception if the xml tag < status >failure < /status > In your case, it always returns an excpetion. – Marlene75 Mar 20 '13 at 10:56
  • thx again, this is almost it, as I dont have a setSource() :) I need to pass $sampleXml without using setSource(), this is why I thought about mocking. – Marlene75 Mar 20 '13 at 11:33
  • How does Service_Order_Http get the xml during normal operation? – Dan Mar 20 '13 at 11:46
  • A protected method using Zend_Http_Client. The protected method sets the response xml into a protected var, which will be prased by the getResponse() – Marlene75 Mar 20 '13 at 14:09
  • The one test should test the failure in status, and return the exception. Another test case is then done, with another possible mock object, to handle success. – Steven Scott Mar 20 '13 at 14:44