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());