Mock or Stub a method in a php parent class - php

I am testing a class in phpunit, but I am not mocking it, the class is like that:
class MyClass extends ParentClass
{
public function doSomething($param)
{
//do some stuff
$someValue = $this->anotherMethod(); //this method is defined in the parent class
//do some other stuff with $someValue
return $finalValue;
}
}
in the test class I am doing like this
public function testDoSomething($param)
{
$myclass = new MyClass();
//here I need to control the value of $someValue, as it affects the final value returned
$res = $myClass->doSomething();
$this->assertEqual('sonething', res);
}
so my question is How can I control the value returned from anotherMethod method? I'd prefer to mock it so it does not call other methods in it

You could partially mock your class and instrument the methods that you do not want to test, as the following example:
public function testDoSomething()
{
/** #var \App\Models\MyClass $classUnderTest */
$classUnderTest = $this->getMockBuilder(\App\Models\MyClass::class)
->onlyMethods(['anotherMethod'])
->getMock();
$classUnderTest->expects($this->once())
->method('anotherMethod')
->willReturn('mocked-value');
$this->assertEquals("from-test mocked-value", $classUnderTest->doSomething("from-test"));
}
with the following sources:
ParentClass
class ParentClass
{
public function anotherMethod() {
return "parent-value";
}
}
and MyClass
class MyClass extends ParentClass
{
public function doSomething($param)
{
//do some stuff
$someValue = $this->anotherMethod(); //this method is defined in the parent class
//do some other stuff with $someValue
$finalValue = $param . ' '. $someValue;
return $finalValue;
}
}

Related

Can I create a child class out of a parent class?

Lets say I have a class that is responsible for composing another class:
class ClassBuilder
{
protected $baseClass;
public function __construct()
{
$this->baseClass = new BaseClass();
}
public function set()
{
$this->baseClass->foo = 'bar';
}
// other methods to further modify BaseClass
}
class BaseClass
{
public $foo;
}
class ChildClass extends BaseClass {}
I want to create a method in the ClassBuilder that would allow me to update its baseClass property to an instance of ChildClass with the same property values as the current BaseClass object. How can I do that?
public function update()
{
// $this->baseClass = new ChildClass() with the current property values in BaseClass
}
I'm not sure that your overall approach is correct, but about the only way is to loop and set:
public function update()
{
$new = new ChildClass();
foreach($this->baseClass as $name => $value) {
$new->$name = $value;
}
$this->baseClass = $new;
//or
$this->baseClass = clone $new;
}

Execute method once for each child class

I am trying to execute initialize() method for each class that extends from a baseClass, by using late static bindings:
class BaseClass
{
protected static $initialized = false;
public static function find()
{
static::initialize();
//TODO search entries and return as instances...
}
protected static function initialize()
{
if (static::$initialized)
return;
echo 'Initializing ', static::class, '<br/>';
//do important stuff
static::$initialized = true;
}
}
class Child1 extends BaseClass {}
class Child2 extends BaseClass {}
$initialized property is being shared across the extending classes.
Child1::find(); //Outputs 'Initializing Child1', as expected
Child2::find(); //Doesn't execute 'initialize()' because
//$initialized property was set to true by Child1
//Expected: 'Initializing Child2'
Is there a simple way to achieve it?
You can change $initialized to an array (as suggested in comment) using class names as keys:
class BaseClass
{
protected static $initialized = [];
public static function find()
{
return static::initialize();
}
protected static function initialize()
{
$class_name = static::class;
if (!empty(static::$initialized[$class_name])) {
return static::$initialized[$class_name];
}
echo 'Initializing ', $class_name, '<br/>';
//do important stuff
static::$initialized[$class_name] = new static();
// do another important stuff and return instance
return static::$initialized[$class_name];
}
}
class Child1 extends BaseClass {}
class Child2 extends BaseClass {}

Unit test returning injected object

I have a basic class which I inject into another class
AClass
{
protected $thing;
public function setThing($thing)
{
$this->thing = $thing;
}
public function getThing()
{
return $this->thing;
}
}
This class is the SUT.
AnotherClass
{
protected $aClass;
protected $someOtherClass;
__construct(AClass $aClass, SomeOtherClass $someOtherClass)
{
$this->aClass = $aClass;
$this->someOtherClass = $someOtherClass;
}
public function thatImTesting()
{
...
$thing = "logic from {$this->someOtherClass} and some other stuff";
return $this->aClass->setThing($thing);
}
}
So I want to test AnotherClass so I mock SomeOtherClass and inject it into the SUT. However, I create a new AClass and inject it in because I don't want to mock the functions (as that would make no sense).
$someOtherClassMock = m::mock(SomeOtherClass::class, [
// mocking the functions here
]);
$aClass = new AClass();
$anotherClass = new AnotherClass($aClass, $someOtherClassMock);
$this->assertEquals('Something', $anotherClass->getThing());
As $anotherClass object is returned and I need to call a function to check the data in the test, is this still a unit test?

How to make the inherited class run a method from same class in PHP

Whats wrong with me OOP here.
I want to inherit from Class A
The return_output method will do something common so I don't want to write that in the inherited classes.
However when I do B->return_output() I want it to run the do_something method in Class B, but I see that it always runs the method from Class A.
Should I replace $this with something else?
class A {
private function do_something() {
// do something
}
public function return_output() {
$op = $this->do_something();
// add some wrappers to $op
return $op;
}
}
class B extends A {
private function do_something() {
// do something different
}
}
var newClass = new B;
echo B->return_output();
use protected and not private since you are running it inside of scope a and scope b can't access private scope a:
class A {
protected function do_something() {
echo('ado_something');
}
public function return_output() {
$op = $this->do_something();
// add some wrappers to $op
return $op;
}
}
class B extends A {
protected function do_something() {
echo('bdo_something');
}
}
$newClass = new B;
echo $newClass->return_output();

Testing a private method in an abstract class extends the other one

I'm trying to test a private method in an abstract class.
I've got three abstract classes:
abstract class AbstractClass1 extends AbstractClass2
{
private function _privateFunction()
{
//method's body
}
}
abstract class AbstractClass2 extends AbstractClass3
{
public function __construct($param)
{
parent::__construct($param)
}
}
abstract class AbstractClass3
{
public function __construct($param = array())
{
//something
}
}
The test class:
class AbstractClass1Test extends PHPUnit_Framework_TestCase
{
public function test_privateFunction()
{
$stub = $this->getMockForAbstractClass("AbstractClass1");
$class = new ReflectionClass($stub);
$method = $class->getMethod("_privateFunction");
$method->setAccessible(true);
//some assertings with $method->invoke($stub)
}
}
The test failed, because of the error:
Missing argument 1 for AbstractClass2::__construct(), called in /usr/share/php/PHPUnit/Framework/MockObject/Generator.php on line 190 and defined
AbstractClass2.php
public function __construct($param)
AbstractClass1.php
$classMock = $this->getMockForAbstractClass("AbstractClass1");
Generator.php:190
if ($callOriginalConstructor &&
!interface_exists($originalClassName, $callAutoload)) {
if (count($arguments) == 0) {
<strong>$mockObject = new $mock['mockClassName'];</strong>
} else {
$mockClass = new ReflectionClass($mock['mockClassName']);
$mockObject = $mockClass->newInstanceArgs($arguments);
}
} else ...
What do I wrong? Or how can I test my private function in this situation?
You need to pass an argument to AbstractClass1's constructor. Pass constructor arguments in an array as the second argument to getMockForAbstractClass().
$stub = $this->getMockForAbstractClass("AbstractClass1", array('param'));
Seeing as you overrode the original constructor,
public function __construct($param = array()) //Allow null $param as it would default to array();
With a new one:
public function __construct($param) //Does not allow null $param.
You will require to define the $param when you initialize the object. That's probably your problem.
Objects in PHP are not like JavaScript, they cannot be called like associative arrays. Your object initialization should look like:
$mockObject = new ClassExtendingAbstractClass1Or2('parameter');
The new keyword cannot be used in front of a variable.

Categories