Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回注入对象的单元测试_Php_Unit Testing_Testing - Fatal编程技术网

Php 返回注入对象的单元测试

Php 返回注入对象的单元测试,php,unit-testing,testing,Php,Unit Testing,Testing,我有一个基本类,我把它注入到另一个类中 AClass { protected $thing; public function setThing($thing) { $this->thing = $thing; } public function getThing() { return $this->thing; } } 这门课是SUT AnotherClass { protecte

我有一个基本类,我把它注入到另一个类中

AClass
{
    protected $thing;

    public function setThing($thing)
    {
        $this->thing = $thing;
    }

    public function getThing() 
    {
        return $this->thing;
    }
}
这门课是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);
    }
}
所以我想测试另一个类,所以我模拟另一个类,并将其注入SUT。但是,我创建了一个新的
AClass
,并将其插入,因为我不想模拟函数(因为这样做毫无意义)

$anotherClass
对象返回时,我需要调用一个函数来检查测试中的数据,这仍然是一个单元测试吗

$someOtherClassMock = m::mock(SomeOtherClass::class, [
    // mocking the functions here
]);
$aClass = new AClass();
$anotherClass = new AnotherClass($aClass, $someOtherClassMock);

$this->assertEquals('Something', $anotherClass->getThing());