2

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @Matteo I spend hello lot of time creating a reusable object inside a class. this one worked setUpBeforeClass()
0

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;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.