vendredi 16 octobre 2015

Passing variables in phpunit

I'm trying to pass an array i created in my testcase into my function i want to test. Is it possible to create a vairable in the testcase and pass or mock it to the class/function i want to test?

is it possible to use something like this:

$this->object = array(//array code here);
$this->testclass->attachVar->getAllObjects($this->objects);

Here is my code:

class myClassTest extends \PHPUnit_Framework_TestCase {

protected function setUp(){
    $this->testclass = new \stdClass();
    $this->testclass = $this->getMockBuilder('library\ixPlanta\plantChange', $this->object)
                 ->disableOriginalConstructor()                     
                 ->getMock();
}
public function testGetAllObjects() {

   $this->object = array(
                'testdb'        => array(
                    'testdb_michel' => array(
                        'dbname'        => 'testdb',
                        'projectName'   => 'testdb',
                        'projectID'     => 'bd993d2b9478582f6d3b73cda00bd2a',
                        'mainProject'   => 'test',
                        'search'        => false,
                        'webgroup'      => array(),
                        'locked'        => false
                    )
                )
            );


    $this->testclass->expects($this->once())
            ->method('GetAllObjects')
            ->with('testdb', false, "CHECKED")
            ->injectTo('object', $this->object)
            ->will();


  $result = $this->testclass->getAllObjects('testdb', false, "CHECKED");
  $this->assertTrue($result);

    }

In the function testGetAllObjects() i created an array that i want to pass to the function i want to test

public function getAllObjects($company,$selected=false,$selectText='CHECKED'){
    $objList = array();
    $i = 0;
    foreach($this->Objects[$company] as $key => $value){
        $objList[$i] = array('value'=> $key,'name' => $value['projectName'], 'objectID' => $value['projectID']);
        $objList[$i]['checked'] = '';
        if($selected !== false && !is_array($selected) && $selected === $value['dbname']){
            $objList[$i]['checked'] = $selectText;
        }elseif($selected !== false && is_array($selected) && in_array($value['dbname'], $selected)){
            $objList[$i]['checked'] = $selectText;
        }
        ++$i;
    }
    return $objList;
}

The variable i want to pass to getAllObjects is $this->objects

Aucun commentaire:

Enregistrer un commentaire