123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Eav\Test\Unit\Model\Attribute\Data;
- use Magento\Eav\Model\Attribute\Data\Text;
- class AbstractDataTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Eav\Model\Attribute\Data\AbstractData
- */
- protected $model;
- protected function setUp()
- {
- $timezoneMock = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
- $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
- $localeResolverMock = $this->createMock(\Magento\Framework\Locale\ResolverInterface::class);
- $stringMock = $this->createMock(\Magento\Framework\Stdlib\StringUtils::class);
- /* testing abstract model through its child */
- $this->model = new Text($timezoneMock, $loggerMock, $localeResolverMock, $stringMock);
- }
- /**
- * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::getEntity
- * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::setEntity
- */
- public function testGetEntity()
- {
- $entityMock = $this->createMock(\Magento\Framework\Model\AbstractModel::class);
- $this->model->setEntity($entityMock);
- $this->assertEquals($entityMock, $this->model->getEntity());
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- * @expectedExceptionMessage Entity object is undefined
- *
- * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::getEntity
- */
- public function testGetEntityWhenEntityNotSet()
- {
- $this->model->getEntity();
- }
- /**
- * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::getExtractedData
- * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::setExtractedData
- *
- * @param string $index
- * @param mixed $expectedResult
- *
- * @dataProvider extractedDataDataProvider
- */
- public function testGetExtractedData($index, $expectedResult)
- {
- $extractedData = ['index' => 'value', 'otherIndex' => 'otherValue'];
- $this->model->setExtractedData($extractedData);
- $this->assertEquals($expectedResult, $this->model->getExtractedData($index));
- }
- /**
- * @return array
- */
- public function extractedDataDataProvider()
- {
- return [
- [
- 'index' => 'index',
- 'expectedResult' => 'value',
- ],
- [
- 'index' => null,
- 'expectedResult' => ['index' => 'value', 'otherIndex' => 'otherValue']
- ],
- [
- 'index' => 'customIndex',
- 'expectedResult' => null
- ]
- ];
- }
- /**
- * @covers \Magento\Eav\Model\Attribute\Data\AbstractData::_getRequestValue
- *
- * @param string $requestScope
- * @param string $value
- * @param string $expectedResult
- * @param array $params
- * @param bool $requestScopeOnly
- * @param string|null $filter
- * @dataProvider getRequestValueDataProvider
- */
- public function testGetRequestValue($requestScope, $value, $params, $requestScopeOnly, $expectedResult, $filter)
- {
- $requestMock = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getParams', 'getParam']);
- $requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap([
- ['attributeCode', false, $value],
- [$requestScope, $value],
- ]));
- $requestMock->expects($this->any())->method('getParams')->will($this->returnValue($params));
- $attributeMock = $this->createPartialMock(
- \Magento\Eav\Model\Attribute::class,
- ['getInputFilter', 'getAttributeCode']
- );
- $attributeMock->expects($this->any())->method('getAttributeCode')->will($this->returnValue('attributeCode'));
- if ($filter) {
- $attributeMock->expects($this->any())->method('getInputFilter')->will($this->returnValue($filter));
- }
- $this->model->setAttribute($attributeMock);
- $this->model->setRequestScope($requestScope);
- $this->model->setRequestScopeOnly($requestScopeOnly);
- $this->assertEquals($expectedResult, $this->model->extractValue($requestMock));
- }
- /**
- * @return array
- */
- public function getRequestValueDataProvider()
- {
- return [
- [
- 'requestScope' => false,
- 'value' => 'value',
- 'params' => [],
- 'requestScopeOnly' => true,
- 'expectedResult' => 'value',
- 'filter' => null,
- ],
- [
- 'requestScope' => 'scope/scope',
- 'value' => 'value',
- 'params' => ['scope' => ['scope' => ['attributeCode' => 'data']]],
- 'requestScopeOnly' => true,
- 'expectedResult' => 'data',
- 'filter' => null,
- ],
- [
- 'requestScope' => 'scope/scope',
- 'value' => 'value',
- 'params' => ['scope' => ['scope' => []]],
- 'requestScopeOnly' => true,
- 'expectedResult' => false,
- 'filter' => null,
- ],
- [
- 'requestScope' => 'scope/scope',
- 'value' => 'value',
- 'params' => ['scope'],
- 'requestScopeOnly' => true,
- 'expectedResult' => false,
- 'filter' => null,
- ],
- [
- 'requestScope' => 'scope',
- 'value' => 'value',
- 'params' => ['otherScope' => 1],
- 'requestScopeOnly' => true,
- 'expectedResult' => false,
- 'filter' => null,
- ],
- [
- 'requestScope' => 'scope',
- 'value' => 'value',
- 'params' => ['otherScope' => 1],
- 'requestScopeOnly' => false,
- 'expectedResult' => 'value',
- 'filter' => null,
- ],
- [
- 'requestScope' => 'scope',
- 'value' => '1970-01-01',
- 'params' => [],
- 'requestScopeOnly' => false,
- 'expectedResult' => '1970-01-01',
- 'filter' => 'date'
- ]
- ];
- }
- }
|