PHP Unit: Create common object to work with - php

I'm writing PHP Unit tests for a class, which make some curl requests. At the moment every test starts with my class instance initiation and login directive and ends with logout directive, e.g.
public function testSomeMethod(){
$a = new myClass();
$a->login();
....
$a->logout();
$this->assertTrue($smth);
I want to create one common object $a = new myClass(), call login method before all test and logout method after all tests. How can I do that?

In accordion with the PHPUnit documentation here you can use the following hook method:
The setUp() and tearDown() template methods are run once for each test
method (and on fresh instances) of the test case class.
Also
In addition, the setUpBeforeClass() and tearDownAfterClass() template
methods are called before the first test of the test case class is run
and after the last test of the test case class is run, respectively.
In your case you can define the login class as class member and instantiate (login) in the setUpBeforeClass() method and do the logout in the tearDownAfterClass()
EDIT: EXAMPLE
Consider the following class:
namespace Acme\DemoBundle\Service;
class MyService {
public function login()
{
echo 'login called'.PHP_EOL;
}
public function logout()
{
echo 'logout called'.PHP_EOL;
}
public function doService($name)
{
echo $name.PHP_EOL;
return $name;
}
}
This test case:
use Acme\DemoBundle\Service\MyService;
class MyServiceTest extends \PHPUnit_Framework_TestCase {
/**
* #var \Acme\DemoBundle\Service\MyService
*/
protected static $client;
public static function setUpBeforeClass()
{
self::$client = new MyService();
self::$client->login();
}
public function testSomeMethod1()
{
$value = self::$client->doService("test1");
$this->assertEquals("test1",$value);
}
public function testSomeMethod2()
{
$value = self::$client->doService("test2");
$this->assertEquals("test2",$value);
}
public static function tearDownAfterClass()
{
self::$client->logout();
}
}
Dump the following output:
login called .test1 .test2 logout called
Time: 49 ms, Memory: 6.00Mb
OK (2 tests, 2 assertions)
hope this help

Creating reusable / common object at class level ( to be use in methods/functions) answer by #Matteo was helpful, Here is my implementation
no need for multiple inheritance , or __construct() constructor, I spent a lot of time on that ( specially coming from java background)
<?php
namespace Tests\Unit;
use Tests\TestCase;
use App\Services\HelperService;
class HelperServiceTest extends TestCase
{
//Object that will be reused in function()'s of this HelperServiceTest class itself
protected static $HelperServiceObj;
//this is the magic function
public static function setUpBeforeClass()
{
self::$HelperServiceObj = new HelperService();
}
public function testOneHelperServiceToBeTrue()
{
$this->assertTrue(self::$HelperServiceObj->getValue());
}
public function testTwoHelperServiceToBeTrue()
{
$this->assertTrue(self::$HelperServiceObj->getValueTwo());
}
}
Refer official docs setUp() , setUpBeforeClass() , tearDown() for magic function like setUpBeforeClass()
Sample implementation of getValue() function in HelperServiceTest class
<?php
namespace App\Services;
class HelperServiceTest
{
function getValue(){
return true;
}
function getValueTwo(){
return true;
}
}

Related

PHP, phpunit, and dbunit - getConnection and getDataSet don't get called if setUp and tearDown are overridden

I'm working with an existing phpunit test suite, and trying to incorporate dbunit. In particular, I want to use the dataSet abstraction to load fixture data and clean up after me.
I've added the PHPUnit_Extensions_Database_TestCase_Trait trait to the test case, and implemented functions getConnection and getDataSet. However, those methods only get called by the trait's default setUp and tearDown methods. Many of my tests have their own setUp and tearDown methods defined.
Is there someplace different I should be putting this existing setUp/tearDown code so that I don't have to override setUp and tearDown from the trait?
reference code:
class FooTest extends \PHPUnit_Framework_TestCase {
use PHPUnit_Extensions_Database_TestCase_Trait;
static private $pdo = null;
private $conn = null;
public function testTrueIsTrue() {
$foo = true;
$this->assertTrue($foo);
}
public function setUp() {
error_log("in setUp");
}
public function tearDown() {
error_log("in tearDown");
}
public function getConnection() {
error_log("in getConnection");
return $this->createDefaultDBConnection();
}
/**
* #return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
public function getDataSet() {
error_log("in getDataSet");
return new PHPUnit_Extensions_Database_DataSet_DefaultDataSet();
}
}
OK this turns out to be a specialized case of this:
How to override trait function and call it from the overridden function?
so, I've modified my use statement:
use PHPUnit_Extensions_Database_TestCase_Trait {
setUp as protected defaultSetUp;
tearDown as protected defaultTearDown;
}
and added the calls to the default methods:
public function setUp() {
$this->defaultSetUp();
error_log("in setUp");
}
public function tearDown() {
error_log("in tearDown");
$this->defaultTearDown();
}

PHP Traits: How to circumvenient constructors or force them to be called?

Have a look at the following trait:
trait PrimaryModelRest {
use RestController;
protected $primaryModel;
public function __construct() {
$mc = $this->getPrimaryModelClass();
try {
$this->primaryModel = new $mc();
if(!($this->primaryModel instanceof Model)) {
throw new ClassNotFoundException("Primary Model fatal exception: The given Class is not an instance of Illuminate\Database\Eloquent\Model");
}
} catch (Exception $e) {
throw new WrongImplementationException("Primary Model Exception: Class not found.");
}
}
/**
* #return string: Classname of the primary model.
*/
public abstract function getPrimaryModelClass();
// various functions here
}
As you can see the trait makes sure that the using class holds a certain model instance and it implements certain methods. This works as long as the implementing class does not override the constructor.
So here is my question: I want to make sure that either the constructor is called or a better solution, such that I can instantiate this model on initialization.
Please make in answer which respects Multiple inheritance as well es Multi-Level inheritance.
I think you are trying to make the trait do a job it is not designed for.
Traits are not a form of multiple inheritance, but rather "horizontal reuse" - they're often described as "compiler-assisted copy-and-paste". As such, the job of a trait is to provide some code, so that you don't have to copy it into the class manually. The only relationship it has is with the class where the use statement occurs, where the code is "pasted". To aid in this role, it can make some basic requirements of that target class, but after that, the trait takes no part in inheritance.
In your example, you are concerned that a sub-class might try to access $primaryModel without running the constructor code which initialises it, and you are trying to use the trait to enforce that; but this is not actually the trait's responsibility.
The following definitions of class Sub are completely equivalent:
trait Test {
public function foo() {
echo 'Hello, World!';
}
}
class ParentWithTrait {
use Test;
}
class Sub inherits ParentWithTrait {
}
vs:
class ParentWithMethodDefinition {
public function foo() {
echo 'Hello, World!';
}
}
class Sub inherits ParentWithMethodDefinition {
}
In either case, class Sub could have its own definition of foo(), and by-pass the logic you'd written in the parent class.
The only contract that can prevent that is the final keyword, which in your case would mean marking your constructor as final. You can then provide an extension point that can be overridden for sub-classes to add their own initialisation:
class Base {
final public function __construct() {
important_things(); // Always run this!
$this->onConstruct(); // Extension point
}
protected function onConstruct() {
// empty default definition
}
}
class Sub {
protected function onConstruct() {
stuff_for_sub(); // Runs after mandatory important_things()
}
}
A trait can also mark its constructor as final, but this is part of the code being pasted, not a requirement on the class using the trait. You could actually use a trait with a constructor, but then write a new constructor as well, and it would mask the trait's version completely:
trait Test {
final public function __construct() {
echo "Trait Constructor";
}
}
class Noisy {
use Test;
}
class Silent {
use Test;
public function __construct() {
// Nothing
}
}
As far as the trait is concerned, this is like buying a bottle of beer and pouring it down the sink: you asked for its code and didn't use it, but that's your problem.
Crucially, though, you can also alias the methods of the trait, creating a new method with the same code but a different name and/or a different visibility. This means you can mix in code from traits which declare constructors, and use that code in a more complex constructor, or somewhere else in the class altogether.
The target class might also use the "final + hook" pattern:
trait TestOne {
final public function __construct() {
echo "Trait TestOne Constructor\n";
}
}
trait TestTwo {
final public function __construct() {
echo "Trait TestTwo Constructor\n";
}
}
class Mixed {
final public function __construct() {
echo "Beginning\n";
$this->testOneConstructor();
echo "Middle\n";
$this->testTwoConstructor();
echo "After Traits\n";
$this->onConstruct();
echo "After Sub-Class Hook\n";
}
use TestOne { __construct as private testOneConstructor; }
use TestTwo { __construct as private testTwoConstructor; }
protected function onConstruct() {
echo "Default hook\n";
}
}
class ChildOfMixed extends Mixed {
protected function onConstruct() {
echo "Child hook\n";
}
}
The trait hasn't forced the Mixed class to implement this pattern, but it has enabled it, in keeping with its purpose of facilitating code reuse.
Interestingly, the below code doesn't work, because the as keyword adds an alias, rather than renaming the normal method, so this ends up trying to override the final constructor from Mixed:
class ChildOfMixed extends Mixed {
use TestTwo { __construct as private testTwoConstructor; }
protected function onConstruct() {
$this->testTwoConstructor();
echo "Child hook\n";
}
}
Use a base class, this will let you handle the trait as a parent.
<?php
trait StorageTrait
{
public function __construct()
{
echo "Storage Trait";
}
}
class StorageAttempt
{
use StorageTrait;
public function __construct()
{
parent::__construct();
echo " - Storage Attempt";
}
}
abstract class StorageBase
{
use StorageTrait;
}
class MyStorage extends StorageBase
{
public function __construct()
{
parent::__construct();
echo ' - My Storage';
}
}
new StorageAttempt(); // won't work - will trigger error
new MyStorage(); // will display "Storage Trait - My Storage"
Also if you are using traits you can also work with properties and getters & setters.
Example: A Storage trait involves that a Storage Engine will be used. You can add the storageEngine property and its getters and setters. (with or without Type Hinting)
interface StorageEngineInterface{}
trait StorageTrait
{
/**
* #var StorageEngineInterface
*/
protected $storageEngine;
/**
* #return StorageEngineInterface
*/
public function getStorageEngine(): StorageEngineInterface
{
return $this->storageEngine;
}
/**
* #param StorageEngineInterface $storageEngine
*/
public function setStorageEngine(StorageEngineInterface $storageEngine)
{
$this->storageEngine = $storageEngine;
return $this;
}
}
Note: this is just an explanation so you can better understand how Traits work
UPDATE
To avoid conflict you can use aliases for trait methods. This way you can use both constructors (from trait and from extended class) you can do the following
class DifferentStorage
{
public function __construct()
{
echo ' diff ';
}
}
class MyDifferentStorage extends DifferentStorage
{
use StorageTrait {
StorageTrait::__construct as otherConstructor;
}
public function __construct()
{
parent::__construct();
self::otherConstructor();
}
}
You could use the interface injection pattern: implement an interface iPrimaryModelRest into the same class that uses the trait PrimaryModelRest:
interface iPrimaryModelRest {
public function init();
public abstract function getPrimaryModelClass();
}
The class that uses the trait woud look like this:
class cMyClass implements iPrimaryModelRest {
use PrimaryModelRest;
}
Then, whenever the class is instantiated (not only autoloaded) you could call a special factory-like initialisation function like this:
class cMyApp {
public function start() {
/** #var cMyClass $oClass */ // enlighten IDE
$oClass = $this->init(new cMyClass);
}
public function init($oClass) {
if ($oClass instanceof iPrimaryModelRest) {$oClass->init();}
if ($oClass instanceof whateverinterface) {
// pass optional stuff, like database connection
}
}
}
The interface is used to determine the capabilities of the class, and sets data/runs corresponding functions. If I'm not mistaken then this pattern is called a Service Locator.
I needed a trait for database connection. To avoid using the __construct in a trait, I've used a magic getter instead:
trait WithDatabaseConnection
{
public function __get(string $name)
{
if ($name === 'pdo') {
return App::make(\PDO::class);
}
trigger_error("Property $name does not exist.");
return null;
}
}
class Foo {
use WithDatabaseConnection;
public function save() {
$this->pdo->query('...');
}
}

Calling a method of a Test Class from another Test class

I am using PHPUnit to unit test my application (using Zend Framework 2). I am stuck in a situation where I need to call a method that is in one test class
from another test class. Let me explain myself with a small example:
<?php
// TestUser.php
namespace Test\User;
class UserTest extends \PHPUnit_Framework_TestCase
{
public static function GetUserCount(){
// some code here
}
}
?>
<?php
// TestAdmin.php
namespace Test\Admin;
use Test\User;
class AdminTest extends \PHPUnit_Framework_TestCase
{
public static function AdminAction(){
Test\User::GetUserCount();
}
}
?>
When I call the Test\User::GetUserCount(); or User::GetUserCount(); I get the following error:
PHP Fatal error: Class 'Test\User' not found in path/to/TestAdmin.php
on line 11
Any idea if the method is callable from one test class to another test class? If yes, how?
Thanks
Normally, you would Mock the other class call, to ensure the returned values are what your class is expecting. You may also link some test together with Test Dependencies.
I have added a short sample. Note, I assume you added the AdminAction and GetUserCount() as samples since these are not really test methods that you would have with PHPUnit tests.
TestUser.php
<?php
namespace Test\User;
class UserTest extends \PHPUnit_Framework_TestCase
{
protected $UserObject;
public function setUp()
{
$this->UserObject = new Test\User(); // Normal Object
}
public static function testGetUserCount()
{
$this->assertEquals(1, $this->UserObject->GetUserCount(), 'Testing the basic object will return 1 if initialized'); // Do your tests here.
}
}
TestAdmin.php
<?php
namespace Test\Admin;
class AdminTest extends \PHPUnit_Framework_TestCase
{
protected $AdminObject;
public function setUp()
{
$this->AdminObject = new Test\Admin();
}
public static function testAdminAction()
{
// Create a stub for the User class.
$stub = $this->getMock('User');
// Configure the stub.
$stub->expects($this->any())
->method('GetUserCount')
->will($this->returnValue(2));
// Calling $stub->GetUserCount() will now return 2. You can then ensure the Admin class works correctly, by changing what the mocks return.
$this->assertEquals(2, $stub->GetUserCount());
}
}

PHPUnit Stubbing Class methods declared as "final"

I'm writing a unit test for a class method that calls another class's method using a mock, only the method that needs to be called is declared as final, so PHPUnit is unable to mock it. Is there a different approach I can take?
example:
class to be mocked
class Class_To_Mock
{
final public function needsToBeCalled($options)
{
...
}
}
my test case
class MyTest extends PHPUnit_Framework_TestCase
{
public function testDoSomething()
{
$mock = $this->getMock('Class_To_Mock', array('needsToBeCalled'));
$mock->expects($this->once())
->method('needsToBeCalled')
->with($this->equalTo(array('option'));
}
}
Edit: If using the solution provided by Mike B and you have a setter/getter for the object you're mocking that does type checking (to ensure the correct object was passed into the setter), you'll need to mock the getter on the class you're testing and have it return the other mock.
example:
class to be mocked
class Class_To_Mock
{
final public function needsToBeCalled($options)
{
...
}
}
mock
class Class_To_MockMock
{
public function needsToBeCalled($options)
{
...
}
}
class to be tested
class Class_To_Be_Tested
{
public function setClassToMock(Class_To_Mock $classToMock)
{
...
}
public function getClassToMock()
{
...
}
public function doSomething()
{
$this->getClassToMock()
->needsToBeCalled(array('option'));
}
}
my test case
class MyTest extends PHPUnit_Framework_TestCase
{
public function testDoSomething()
{
$classToTest = $this->getMock('Class_To_Be_Tested', array('getClassToMock'));
$mock = $this->getMock('Class_To_MockMock', array('needsToBeCalled'));
$classToTest->expects($this->any())
->method('getClassToMock')
->will($this->returnValue($mock));
$mock->expects($this->once())
->method('needsToBeCalled')
->with($this->equalTo(array('option'));
$classToTest->doSomething();
}
}
I don't think PHPUnit supports stubbing/mocking of final methods. You may have to create your own stub for this situation and do some extension trickery:
class myTestClassMock {
public function needsToBeCalled() {
$foo = new Class_To_Mock();
$result = $foo->needsToBeCalled();
return array('option');
}
}
Found this in the PHPUnit Manual under Chapter 11. Test Doubles
Limitations
Please note that final, private and static methods cannot be stubbed or mocked. They are ignored by PHPUnit's test double functionality and retain their original behavior.
I just stumbled upon this issue today. Another alternative is to mock the interface that the class implements, given that it implements an interface and you use the interface as type hinting.
For example, given the problem in question, you can create an interface and use it as follows:
interface Interface_To_Mock
{
function needsToBeCalled($options);
}
class Class_To_Mock implements Interface_To_Mock
{
final public function needsToBeCalled($options)
{
...
}
}
class Class_To_Be_Tested
{
public function setClassToMock(Interface_To_Mock $classToMock)
{
...
}
...
}
class MyTest extends PHPUnit_Framework_TestCase
{
public function testDoSomething()
{
$mock = $this->getMock('Interface_To_Mock', array('needsToBeCalled'));
...
}
}

PHPUnit Mock Objects and Static Methods

I am looking for the best way to go about testing the following static method (specifically using a Doctrine Model):
class Model_User extends Doctrine_Record
{
public static function create($userData)
{
$newUser = new self();
$newUser->fromArray($userData);
$newUser->save();
}
}
Ideally, I would use a mock object to ensure that fromArray (with the supplied user data) and save were called, but that's not possible as the method is static.
Any suggestions?
Sebastian Bergmann, the author of PHPUnit, recently had a blog post about Stubbing and Mocking Static Methods. With PHPUnit 3.5 and PHP 5.3 as well as consistent use of late static binding, you can do
$class::staticExpects($this->any())
->method('helper')
->will($this->returnValue('bar'));
Update: staticExpects is deprecated as of PHPUnit 3.8 and will be removed completely with later versions.
There is now the AspectMock library to help with this:
https://github.com/Codeception/AspectMock
$this->assertEquals('users', UserModel::tableName());
$userModel = test::double('UserModel', ['tableName' => 'my_users']);
$this->assertEquals('my_users', UserModel::tableName());
$userModel->verifyInvoked('tableName');
I would make a new class in the unit test namespace that extends the Model_User and test that. Here's an example:
Original class:
class Model_User extends Doctrine_Record
{
public static function create($userData)
{
$newUser = new self();
$newUser->fromArray($userData);
$newUser->save();
}
}
Mock Class to call in unit test(s):
use \Model_User
class Mock_Model_User extends Model_User
{
/** \PHPUnit\Framework\TestCase */
public static $test;
// This class inherits all the original classes functions.
// However, you can override the methods and use the $test property
// to perform some assertions.
}
In your unit test:
use Module_User;
use PHPUnit\Framework\TestCase;
class Model_UserTest extends TestCase
{
function testCanInitialize()
{
$userDataFixture = []; // Made an assumption user data would be an array.
$sut = new Mock_Model_User::create($userDataFixture); // calls the parent ::create method, so the real thing.
$sut::test = $this; // This is just here to show possibilities.
$this->assertInstanceOf(Model_User::class, $sut);
}
}
Found the working solution, would to share it despite the topic is old.
class_alias can substitute classes which are not autoloaded yet (works only if you use autoloading, not include/require files directly).
For example, our code:
class MyClass
{
public function someAction() {
StaticHelper::staticAction();
}
}
Our test:
class MyClassTest
{
public function __construct() {
// works only if StaticHelper is not autoloaded yet!
class_alias(StaticHelperMock::class, StaticHelper::class);
}
public function test_some_action() {
$sut = new MyClass();
$myClass->someAction();
}
}
Our mock:
class StaticHelperMock
{
public static function staticAction() {
// here implement the mock logic, e.g return some pre-defined value, etc
}
}
This simple solution doesn't need any special libs or extensions.
Mockery's Alias functionality can be used to mock public static methods
http://docs.mockery.io/en/latest/reference/creating_test_doubles.html#creating-test-doubles-aliasing
Another possible approach is with the Moka library:
$modelClass = Moka::mockClass('Model_User', [
'fromArray' => null,
'save' => null
]);
$modelClass::create('DATA');
$this->assertEquals(['DATA'], $modelClass::$moka->report('fromArray')[0]);
$this->assertEquals(1, sizeof($modelClass::$moka->report('save')));
One more approach:
class Experiment
{
public static function getVariant($userId, $experimentName)
{
$experiment = self::loadExperimentJson($experimentName):
return $userId % 10 > 5; // some sort of bucketing
}
protected static function loadExperimentJson($experimentName)
{
// ... do something
}
}
In my ExperimentTest.php
class ExperimentTest extends \Experiment
{
public static function loadExperimentJson($experimentName)
{
return "{
"name": "TestExperiment",
"variants": ["a", "b"],
... etc
}"
}
}
And then I would use it like so:
public function test_Experiment_getVariantForExperiment()
{
$variant = ExperimentTest::getVariant(123, 'blah');
$this->assertEquals($variant, 'a');
$variant = ExperimentTest::getVariant(124, 'blah');
$this->assertEquals($variant, 'b');
}
Testing static methods is generally considered as a bit hard (as you probably already noticed), especially before PHP 5.3.
Could you not modify your code to not use static a method ? I don't really see why you're using a static method here, in fact ; this could probably be re-written to some non-static code, could it not ?
For instance, could something like this not do the trick :
class Model_User extends Doctrine_Record
{
public function saveFromArray($userData)
{
$this->fromArray($userData);
$this->save();
}
}
Not sure what you'll be testing ; but, at least, no static method anymore...

Categories