I have a class utilizing the Magento 2 AttributeRepository
. Can I write a unit test for my class which gets the attribute based on the attribute code?
namespace Company\Catalog\Test\Unit\Model;
use Company\Catalog\Helper\LandingPage;
use Company\Catalog\Model\Brand;
use Magento\Eav\Model\AttributeRepository;
use Magento\Store\Model\StoreManagerInterface;
class BrandTest extends \PHPUnit_Framework_TestCase
{
/** @var Brand */
private $brand;
/** @var AttributeRepository */
private $attributeRepository;
/** @var StoreManagerInterface */
private $storeManager;
/** @var LandingPage */
private $landingPageHelper;
protected function setUp()
{
$this->landingPageHelper = $this->getMockBuilder(LandingPage::class)
->disableOriginalConstructor()
->getMock();
$this->attributeRepository = $this->getMockBuilder(AttributeRepository::class)
->disableOriginalConstructor()
->getMock();
$this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->landingPageHelper
->method('getBrandAttributeCode')
->willReturn('brands');
$this->brand = new Brand($this->storeManager, $this->attributeRepository, $this->landingPageHelper);
}
public function testGetBrandAttributeCode()
{
$expected = 'brands';
$this->assertEquals($expected, $this->brand->getBrandAttributeCode());
}
public function testGetBrandAttribute()
{
$expected = 'brands';
$attribute = $this->brand->getBrandAttribute($expected);
echo $attribute->getAttributeCode();
$this->expectOutputString($expected);
}
}
Should I even prepare a mock data for repository or can I get the actual value from the database using the AttributeRepository
?