public function __construct( \Psr\Log\LoggerInterface $logger, \Zend_Db_Adapter_Pdo_Abstract $dba, ISomeService $service, ... ) { $this->_logger = $logger; $this->_dba = $dba; $this->_service = $service; ... }
setUp()
in unit tests can make life easier if you need to create the same mock set several times to test various implementation features of the developed class. /* create mocks */ $mLogger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock(); $mDba = $this->getMockBuilder(\Zend_Db_Adapter_Pdo_Abstract::class)->getMockForAbstractClass(); $mService = $this->getMockBuilder(\Vendor\Module\ISomeService::class)->disableOriginalConstructor()->getMock(); ... /* setup mocks behaviour */ ... /* */ $obj = new Demo($mLogger, $mDba, $mService, ...); $res = $obj->method($arg1, ...); $this->assert...
$obj = new Demo(...);
.setUp
function. First, we create properties in PHPUnit for the test object itself and dependency mocks: private $mLogger; private $mDba; private $mService; private $obj
setUp
function, called before each test method, the re-initialization of mocks and objects: private function setUp() { $this->mLogger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock(); $this->mDba = $this->getMockBuilder(\Zend_Db_Adapter_Pdo_Abstract::class)->getMockForAbstractClass(); $this->mService = $this->getMockBuilder(\Vendor\Module\ISomeService::class)->disableOriginalConstructor()->getMock(); ... $this->obj = new Demo($this->mLogger, $this->mDba, $this->mService, ...); }
public function test_method() { /* setup mocks behaviour */ $this->mLogger->expects... ... $res = $this->obj->method(); }
setUp()
, and not a crutch with extract()
.Source: https://habr.com/ru/post/277867/
All Articles