*This is a beginner's question in phpunit so i don't know if it belongs here or not.
I want to test that the var path is correct, but I'm getting Failed asserting that null matches expected '/magento2/var'.
I suppose because I am using a mock for DirectoryList, instead instantiating the class. How does one test it? Should I instantiate DirectoryList? I am having issues with it's constructor...
<?php
namespace Vendor\Module\Cron;
use Magento\Framework\App\Filesystem\DirectoryList;
class Products {
/**
* Retrieve var path
*
* @return string
*/
protected $directory_list;
public function __construct(
DirectoryList $directory_list
)
{
$this->directory_list = $directory_list;
}
/**
* Retrieve var path
*
* @return string
*/
public function getVarFolderPath()
{
return $this->directory_list->getPath('var');
}
}
and my test:
<?php
namespace Vendor\Module\Test\Unit\Model;
use Magento\Framework\App\Filesystem\DirectoryList;
use Vendor\Module\Cron\Products;
class ProductsTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string
*
* dummy var path
*/
const VAR_PATH = '/magento2/var';
/**
* @var Vendor\Module\Cron\Products;
*/
protected $products;
/**
* setup tests
*/
protected function setUp()
{
/** @var \PHPUnit_Framework_MockObject_MockObject|DirectoryList */
$directoryListMock = $this->getMockBuilder(DirectoryList::class)
->disableOriginalConstructor()
->getMock();
$this->products = new Products(
$directoryListMock
);
}
/**
* @test Vendor\Module\Cron\Products::getVarFolderPath()
*/
public function testGetVarFolderPath()
{
$this->assertEquals(self::VAR_PATH, $this->products->getVarFolderPath());
}
}
Update: so Kandy's answer was ok. in unit test you test just that specific class.