I am attempting to use PHPunit to mock out some dependencies, but my mock objects don't seem to be working.
Logger.php
class Logger
{
function __construct($log_path)
{
// make sure file exists, open file handle
}
function write($msg)
{
// write message to log
}
}
MyCurl.php
class MyCurl
{
function __construct($url)
{
// set my default curl options
}
function exec()
{
// execute curl request and capture output
}
}
function_x.php
require_once("Logger.php");
require_once("MyCurl.php");
function function_x($params)
{
// set some stuff up;
$LoggerObj = new Logger($filepath);
$CurlObj = new MyCurl($url);
// more stuff
$LoggerObj->write($CurlObj->exec());
// do stuff
return $result;
}
function_x_Test.php
require_once('function_x.php');
class functionXTest extends PHPUnit_Framework_TestCase
{
public function testCleanRun()
{
$MockLogger = $this->getMockBuilder('Logger')->disableOriginalConstructor()->setMethods(array('write', '__destruct'))->getMock();
$MockLogger->expects($this->any())->method('write')->will($this->returnValue(true));
$MockCurl = $this->getMockBuilder('MyCurl')->disableOriginalConstructor()->setMethods(array('exec', '__destruct'))->getMock();
$MockCurl->expects($this->any())->method('exec')->will($this->returnValue('exec returnz'));
$result = function_x($params);
// start assertions with function_x results
}
}
When I run my test, it shows that the original constructor is being called for my Logger class. It does not seem to be using the mocked class. I assumed that if I declared the mock in the test that all calls to the original class would be mocked, thus eliminating those dependencies. Clearly, I am doing something wrong. Can anyone either lend me a helping hand or point me in the right direction? Thanks!
Mocking is replacing an object (see documentation), not a class.
So, to get your example working with mocks, you should inject the objects (dependency injection):
function function_x($params, $logger = null, $curl = null)
{
//Here, you can set logger and curl if they are null.
// only do this to make sure legacy code works.
if(!$logger) {
$logger = new Logger();
}
if(!$curl) {
$curl = new MYCurl();
}
//rest of your code
}
and in your test, you call
require_once('function_x.php');
class functionXTest extends PHPUnit_Framework_TestCase
{
public function testCleanRun()
{
$MockLogger = $this->getMockBuilder('Logger')->disableOriginalConstructor()->setMethods(array('write', '__destruct'))->getMock();
$MockLogger->expects($this->any())->method('write')->will($this->returnValue(true));
$MockCurl = $this->getMockBuilder('MyCurl')->disableOriginalConstructor()->setMethods(array('exec', '__destruct'))->getMock();
$MockCurl->expects($this->any())->method('exec')->will($this->returnValue('exec returnz'));
$result = function_x($params, $MockLogger, $MockCurl);
// start assertions with function_x results
}
}
Related
I am testing a class, let's call it ClassUnderTest using another class, let's call it OtherClass. In my Test I do:
$OtherClassStub = $this->createStub(OtherClass::class);
$OtherClassStub->method(...)
->willReturn(...);
$ClassUnderTest->otherClass = $OtherClassStub;
That works. But when the $ClassUnderTest calls new OtherClass(), the original OtherClass class is created instead of the stub.
How can I achieve that every possible instance of OtherClass in the context of the test is replaced by the stub?
From your description I infer that in principle you have something like this:
class OtherClass {
protected function someMethod(): bool
{
// determine $x ...
return $x;
}
}
class ClassUnderTest {
public OtherClass $otherClass;
public function methodToBeTested(): bool
{
$otherClass = new OtherClass();
return $otherClass->someMethod();
}
}
class ClassUnderTestTest extends TestCase {
public function testMethodToBeTested(): void
{
$otherClassStub = $this->createStub(OtherClass::class);
$otherClassStub->method('someMethod')
->willReturn(true);
$classUnderTest = new ClassUnderTest();
$classUnderTest->otherClass = $otherClassStub;
$result = $classUnderTest->methodToBeTested();
$this->assertTrue($result);
}
}
Now the assertion in your test may hold or it may fail. Why? Because you are not calling the method you stubbed on the $otherClassStub. Instead you instantiate a new $otherClass object in the method you're testing (or somewhere down the line).
Either your ClassUnderTest should always use the OtherClass object from the ClassUndertTest::otherClass attribute (assuming that's why you put it there in the first place).
Or you could use some other form of dependency injection, e.g. by using a framework like Symfony or Laravel. (In the case of Symfony you can even use only the DependencyInjection Component, no idea if that's possible with Laravel, too.)
The simple answer to your actual question is: you cannot change the behaviour of the new keyword. Calling new on a class will always instantiate a new object based on exactly that class, unless the constructor of that class defines something else.
(You might want to get the concept of classes and objects straight, your code example as well as your question seem to indicate that you're not quite clear on that. Maybe reading up on that as well as on the concept of dependency injection will help you.)
Perhaps a solution to your problem is presented here:
How to Build a PHP Plugin Module System
This is one way to load classes as plugins and they can be called from each other. With modifying this system a bit, you can create as many "new OtherClass()" as you like from your code and still access everything from other classes. If you want multiple instances of a class, perhaps modify it into this direction:
function load ($module,$instance) {
if (isset($this->$module->$instance)) { return true; }
From above link:
<?php
class Core {
// (A) PROPERTIES
public $error = ""; // LAST ERROR MESSAGE
public $pdo = null; // DATABASE CONNECTION
public $stmt = null; // SQL STATEMENT
public $lastID = null; // LAST INSERT/UPDATE ID
// (B) LOAD SPECIFIED MODULE
// $module : module to load
function load ($module) {
// (B1) CHECK IF MODULE IS ALREADY LOADED
if (isset($this->$module)) { return true; }
// (B2) EXTEND MODULE ON CORE OBJECT
$file = PATH_LIB . "LIB-$module.php";
if (file_exists($file)) {
require $file;
$this->$module = new $module();
// EVIL POINTER - ALLOW OBJECTS TO ACCESS EACH OTHER
$this->$module->core =& $this;
$this->$module->error =& $this->error;
$this->$module->pdo =& $this->pdo;
$this->$module->stmt =& $this->stmt;
return true;
} else {
$this->error = "$file not found!";
return false;
}
}
}
ps. thank you for the mod, who made me work a bit more to keep this answer online. the answer is so much better now.
I am writing a small script in PHP and am using a Profiler for the development zone to see which functions/actions are slow etc.
$this->profiler = new \Fabfuel\Prophiler\Profiler();
Then I am using it in different methods all over the place (passing it via DI, using League\Container\Container class):
$profilerSeg = $profiler->profilerStartSegment('Page Load','CMS');
...
$profiler->profilerStopSegment($profilerSeg);
Problem is, I only want to use this in development. Thought about an IF statement:
if($this->environment === 'DEV')
$profilerSeg = $profiler->profilerStartSegment('Page Load','CMS');
...
if($this->environment === 'DEV')
$profiler->profilerStopSegment($profilerSeg);
but it looks ugly to have it everywhere. Then I thought about making a "fake profiler" that gets passed if the script is in production so all the calls would return NULL.
What is the best way to handle such a case: a class needed only in development but that should not be loaded in production?
Typical oop uses inheritance
Most approaches define a root class that all (or most) other classes extend
Class main {
private $profiler;
public __construct(){
$this->profiler = new \Fabfuel\Prophiler\Profiler();
// you can comment and uncomment the above line.
}
public profStart(){
if (!empty($this->profiler)) {
$this->profiler->profilerStartSegment($a,$b);
}
}
}
Class someThing Extends main {
// $profiler is already set as part of the constructor
$this->profStart('someThing','strange');
}
Class otherThing Extends someThing {
// but if you want a constructor, you have to daisy-chain the class constructors
public __construct() {
parent::__construct();
}
}
Alternately, swap out an empty object
...but this is generally considered bad practice
Class deadendObject {
public __get($var){}
public __set($var,$val){}
public __call($var,$args){}
// see: http://php.net/manual/en/language.oop5.magic.php
}
if ($profileMe) {
$this->profiler = new \Fabfuel\Prophiler\Profiler();
} else {
$this->profiler = new deadendObject();
}
Set whether to do stuff in the Profiler object itself
Class Profiler {
public $enabled = true;
public function doSomething(){
if($this->enabled) {
// do stuff
}
}
}
$profiler = new \Fabfuel\Prophiler\Profiler();
$profiler->enabled = false;
Best is a combination of inheritance, and having the profiler handle behavior changes.
Class Profiler {
public $enabled = true;
public function doSomething($var = "unknown"){
if($this->enabled) {
// do stuff
}
}
}
Class main {
public $profiler;
public __construct(){
$this->profiler = new Profiler();
$this->profiler->enabled = true;
// better still would be to define this behavior according to a settings file, or environment variables with getenv()
}
}
Class someThing Extends main {
// $profiler is already set as part of the constructor
$this->profiler->doSomething('neighborhood');
}
I ain't afraid 'o no ghosts!
I am trying to write unit test for my application. which as logging the information functionality.
To start with i have service called LogInfo, this how my class look like
use Zend\Log\Logger;
class LogInfo {
$logger = new Logger;
return $logger;
}
I have another class which will process data. which is below.
class Processor
{
public $log;
public function processData($file)
{
$this->log = $this->getLoggerObj('data');
$this->log->info("Received File");
}
public function getLoggerObj($logType)
{
return $this->getServiceLocator()->get('Processor\Service\LogInfo')->logger($logType);
}
}
here i am calling service Loginfo and using it and writing information in a file.
now i need to write phpunit for class Processor
below is my unit test cases
class ProcessorTest{
public function setUp() {
$mockLog = $this->getMockBuilder('FileProcessor\Service\LogInfo', array('logger'))->disableOriginalConstructor()->getMock();
$mockLogger = $this->getMockBuilder('Zend\Log\Logger', array('info'))->disableOriginalConstructor()->getMock();
$serviceManager = new ServiceManager();
$serviceManager->setService('FileProcessor\Service\LogInfo', $mockLog);
$serviceManager->setService('Zend\Log\Logger', $mockLogger);
$this->fileProcessor = new Processor();
$this->fileProcessor->setServiceLocator($serviceManager);
}
public function testProcess() {
$data = 'I have data here';
$this->fileProcessor->processData($data);
}
}
I try to run it, i am getting an error "......PHP Fatal error: Call to a member function info() on a non-object in"
i am not sure , how can i mock Zend logger and pass it to class.
Lets check out some of your code first, starting with the actual test class ProcessorTest. This class constructs a new ServiceManager(). This means you are going to have to do this in every test class, which is not efficient (DRY). I would suggest constructing the ServiceMananger like the Zend Framework 2 documentation describes in the headline Bootstrapping your tests. The following code is the method we are interested in.
public static function getServiceManager()
{
return static::$serviceManager;
}
Using this approach makes it possible to obtain the instance of ServiceManager through Bootstrap::getServiceManager(). Lets refactor the test class using this method.
class ProcessorTest
{
protected $serviceManager;
protected $fileProcessor;
public function setUp()
{
$this->serviceManager = Bootstrap::getServiceManager();
$this->serviceManager->setAllowOverride(true);
$fileProcessor = new Processor();
$fileProcessor->setServiceLocator($this->serviceManager);
$this->fileProcessor = $fileProcessor;
}
public function testProcess()
{
$mockLog = $this->getMockBuilder('FileProcessor\Service\LogInfo', array('logger'))
->disableOriginalConstructor()
->getMock();
$mockLogger = $this->getMockBuilder('Zend\Log\Logger', array('info'))
->disableOriginalConstructor()
->getMock();
$serviceManager->setService('FileProcessor\Service\LogInfo', $mockLog);
$serviceManager->setService('Zend\Log\Logger', $mockLogger);
$data = 'I have data here';
$this->fileProcessor->processData($data);
}
}
This method also makes it possible to change expectations on the mock objects per test function. The Processor instance is constructed in ProcessorTest::setUp() which should be possible in this case.
Any way this does not solve your problem yet. I can see Processor::getLoggerObj() asks the ServiceManager for the service 'Processor\Service\LogInfo' but your test class does not set this instance anywhere. Make sure you set this service in your test class like the following example.
$this->serviceManager->setService('Processor\Service\LogInfo', $processor);
Hello
folks,
I wrote a low level implementation for a XmlRPC-Api and I've trouble to test the communication.
Here is my code.
abstract class BaseClient
{
protected function call($method, array $params = array())
{
$request = xmlrpc_encode_request($method, $parameters);
$file = file_get_contents($this->getDSN(), false, $context);
$response = xmlrpc_decode($file);
if ($response && xmlrpc_is_fault(array($response))) {
trigger_error("xmlrpc: {$response[faultString]} ({$response[faultCode]})");
}
return $response;
}
}
Client extends BaseClient
{
public function testCall($msg)
{
return $this->call('testMethid', array($msg));
}
}
And here is my Test.
ClientTest extends PHPUnit_FrameWork_TestCase
{
public function testTestCall()
{
$c = new Client();
$resp = $c->testCall('Hello World');
$this->assertEquals('Hello World', $resp);
}
}
This test will crash every time, because its not possible to access the api inside a testing environment.
I can't see a solution to mock and inject the call function. What can I do? Maybe my object structure is bad and not able to test
and how can I improve this structure (if this happen)?
Cheers.
Since you're trying to test an external API, I would begin by wrapping your file_get_contents() call in another class and injecting that into your BaseClient. In the simplest form, it might look something like this:
class RemoteFileRetriever
{
public function retrieveFileContents($url)
{
// Do some work to create $context
...
// Now grab the file contents
$contents = file_get_contents($url, false, $context);
return $contents;
}
}
abstract class BaseClient
{
private $fileRetriever;
public function __construct(RemoteFileRetriever $fileRetriever)
{
$this->fileRetriever = $fileRetriever;
}
protected function call($method, array $params = array())
{
...
$file = $this->fileRetriever->retrieveFileContents($this->getDSN());
...
}
}
Now in your test, you can use a mock object to inject as the file retriever. E.g.:
class ClientTest extends PHPUnit_FrameWork_TestCase
{
public function testTestCall()
{
$mockRetriever = new MockRemoteFileRetriever();
$c = new Client($mockRetriever);
$resp = $c->testCall('Hello World');
$this->assertEquals('Hello World', $resp);
}
}
PHPUnit atually has some built-in helpers for mocking. See PHPUnit's Mock Objects.
You don't want to mock the call function.
If you can't setup a fake service then you want to mock the php functions which you can do using PHP Namespacing (Have to have PHP 5.3). You can then create mocks for internal php functions that you are calling in your call method.
http://www.schmengler-se.de/-php-mocking-built-in-functions-like-time-in-unit-tests
If you are not able to do this, testing can be pretty difficult. Can you create a fake api that you can hit for testing? Remember that you aren't actually testing the methods of the api, rather you are trying to make sure that you code makes the request to the api and handles the response in the manner you intend.
As a rule, assume that third party code has been tested and works properly.
I have the following code:
<?php
class X
{
public function do($url)
{
$httpRequest = new \HttpRequest\Curl($url, $this->getOptions());
$httpRequest->fire();
// etc.
}
// ...
}
In order to be able to unit test this class, I'd like to inject a mocked HttpRequest class. One way to do this would be as follows:
<?php
class X
{
private $httpRequestClass;
public function __construct($httpRequestClass = '\HttpRequest\Curl')
{
$this->httpRequestClass = $httpRequestClass;
}
public function do($url)
{
$httpRequest = new $this->httpRequestClass($url, $this->getOptions());
$httpRequest->fire();
// etc.
}
// ...
}
But this doesn't seem right. Any other ideas?
public function __construct($url, $httpRequestClass = null)
{
$this->url = $url;
if ($httpRequestClass == null) //> Default
$this->httpRequestClass = new HttpRequest\Curl($this->url);
else
$this->httpRequestClass = $httpRequestClass;
}
so when you are using this class normally just call it with one param
yourClass('your url');
Otherwise pass the istance in the second argument
yourClass('url', new MockedObj);
Of course you should always Inject your dependencies without providing a default object
The class needs to generate objects of type HttpRequest, but we don't necessarily want it to initialize an object: we may want it to use the prototype pattern, for example. Therefore, the class calls for the factory pattern. I chose a factory callback, as opposed to a factory class, for brevity.
<?php
class X
{
private $factoryCallback;
public function __construct($factoryCallback = null)
{
$this->factoryCallback = $factoryCallback;
}
public function do($url)
{
$httpRequest = $this->createHttpRequest($url);
$httpRequest->fire();
// etc.
}
private function createHttpRequest($url)
{
$callback = $this->factoryCallback;
if (is_callable($callback)) {
return $callback($url, $this->getOptions());
}
return new \HttpRequest\Curl($url, $this->getOptions());
}
// ...
}
The helper method, createHttpRequest(), is a bit redundant in this example, but would be used for error handling in production code.