123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Downloadable\Test\Unit\Model;
- use Magento\Downloadable\Model\SampleRepository;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class SampleRepositoryTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $repositoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $productTypeMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $contentValidatorMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $contentUploaderMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $jsonEncoderMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $sampleFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $productMock;
- /**
- * @var SampleRepository
- */
- protected $service;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $sampleDataObjectFactory;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $metadataPoolMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $sampleHandlerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $entityMetadataMock;
- protected function setUp()
- {
- $this->productMock = $this->createPartialMock(
- \Magento\Catalog\Model\Product::class,
- ['__wakeup', 'getTypeId', 'setDownloadableData', 'save', 'getId', 'getStoreId', 'getData']
- );
- $this->repositoryMock = $this->createMock(\Magento\Catalog\Model\ProductRepository::class);
- $this->productTypeMock = $this->createMock(\Magento\Downloadable\Model\Product\Type::class);
- $this->contentValidatorMock = $this->createMock(\Magento\Downloadable\Model\Sample\ContentValidator::class);
- $this->contentUploaderMock = $this->createMock(
- \Magento\Downloadable\Api\Data\File\ContentUploaderInterface::class
- );
- $this->jsonEncoderMock = $this->createMock(
- \Magento\Framework\Json\EncoderInterface::class
- );
- $this->sampleFactoryMock = $this->createPartialMock(
- \Magento\Downloadable\Model\SampleFactory::class,
- ['create']
- );
- $this->productTypeMock = $this->getMockBuilder(\Magento\Downloadable\Model\Product\Type::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->sampleDataObjectFactory = $this->getMockBuilder(
- \Magento\Downloadable\Api\Data\SampleInterfaceFactory::class
- )->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->service = new \Magento\Downloadable\Model\SampleRepository(
- $this->repositoryMock,
- $this->productTypeMock,
- $this->sampleDataObjectFactory,
- $this->contentValidatorMock,
- $this->contentUploaderMock,
- $this->jsonEncoderMock,
- $this->sampleFactoryMock
- );
- $this->entityMetadataMock = $this->getMockBuilder(
- \Magento\Framework\EntityManager\EntityMetadataInterface::class
- )->getMockForAbstractClass();
- $linkRepository = new \ReflectionClass(get_class($this->service));
- $metadataPoolProperty = $linkRepository->getProperty('metadataPool');
- $this->metadataPoolMock = $this->getMockBuilder(
- \Magento\Framework\EntityManager\MetadataPool::class
- )->disableOriginalConstructor()->getMock();
- $metadataPoolProperty->setAccessible(true);
- $metadataPoolProperty->setValue(
- $this->service,
- $this->metadataPoolMock
- );
- $saveHandlerProperty = $linkRepository->getProperty('sampleTypeHandler');
- $this->sampleHandlerMock = $this->getMockBuilder(
- \Magento\Downloadable\Model\Product\TypeHandler\Sample::class
- )->disableOriginalConstructor()->getMock();
- $saveHandlerProperty->setAccessible(true);
- $saveHandlerProperty->setValue(
- $this->service,
- $this->sampleHandlerMock
- );
- $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($this->entityMetadataMock);
- }
- /**
- * @param array $sampleData
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getSampleMock(array $sampleData)
- {
- $sampleMock = $this->createMock(\Magento\Downloadable\Api\Data\SampleInterface::class);
- if (isset($sampleData['id'])) {
- $sampleMock->expects($this->any())->method('getId')->willReturn($sampleData['id']);
- }
- $sampleMock->expects($this->any())->method('getTitle')->will($this->returnValue($sampleData['title']));
- $sampleMock->expects($this->any())->method('getSortOrder')->will($this->returnValue(
- $sampleData['sort_order']
- ));
- if (isset($sampleData['sample_type'])) {
- $sampleMock->expects($this->any())->method('getSampleType')->will($this->returnValue(
- $sampleData['sample_type']
- ));
- }
- if (isset($sampleData['sample_url'])) {
- $sampleMock->expects($this->any())->method('getSampleUrl')->will($this->returnValue(
- $sampleData['sample_url']
- ));
- }
- if (isset($sampleData['sample_file'])) {
- $sampleMock->expects($this->any())->method('getSampleFile')->will($this->returnValue(
- $sampleData['sample_file']
- ));
- }
- return $sampleMock;
- }
- public function testCreate()
- {
- $productSku = 'simple';
- $sampleData = [
- 'title' => 'Title',
- 'sort_order' => 1,
- 'sample_type' => 'url',
- 'sample_url' => 'http://example.com/',
- ];
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $this->productMock->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable'));
- $sampleMock = $this->getSampleMock($sampleData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
- ->will($this->returnValue(true));
- $this->sampleHandlerMock->expects($this->once())->method('save')->with(
- $this->productMock,
- [
- 'sample' => [
- [
- 'sample_id' => 0,
- 'is_delete' => 0,
- 'type' => $sampleData['sample_type'],
- 'sort_order' => $sampleData['sort_order'],
- 'title' => $sampleData['title'],
- 'sample_url' => $sampleData['sample_url'],
- ],
- ],
- ]
- );
- $this->service->save($productSku, $sampleMock);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage The sample title is empty. Enter the title and try again.
- */
- public function testCreateThrowsExceptionIfTitleIsEmpty()
- {
- $productSku = 'simple';
- $sampleData = [
- 'title' => '',
- 'sort_order' => 1,
- 'sample_type' => 'url',
- 'sample_url' => 'http://example.com/',
- ];
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $this->productMock->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable'));
- $sampleMock = $this->getSampleMock($sampleData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
- ->will($this->returnValue(true));
- $this->sampleHandlerMock->expects($this->never())->method('save');
- $this->service->save($productSku, $sampleMock);
- }
- public function testUpdate()
- {
- $sampleId = 1;
- $productId = 1;
- $productSku = 'simple';
- $sampleData = [
- 'id' => $sampleId,
- 'title' => 'Updated Title',
- 'sort_order' => 1,
- 'sample_type' => 'url',
- 'sample_url' => 'http://example.com/',
- ];
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $this->productMock->expects($this->any())->method('getData')->will($this->returnValue($productId));
- $existingSampleMock = $this->createPartialMock(
- \Magento\Downloadable\Model\Sample::class,
- ['__wakeup', 'getId', 'load', 'getProductId']
- );
- $this->sampleFactoryMock->expects($this->once())->method('create')
- ->will($this->returnValue($existingSampleMock));
- $sampleMock = $this->getSampleMock($sampleData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
- ->will($this->returnValue(true));
- $existingSampleMock->expects($this->any())->method('getId')->will($this->returnValue($sampleId));
- $existingSampleMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
- $existingSampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
- $this->sampleHandlerMock->expects($this->once())->method('save')->with(
- $this->productMock,
- [
- 'sample' => [
- [
- 'sample_id' => $sampleId,
- 'is_delete' => 0,
- 'type' => $sampleData['sample_type'],
- 'sort_order' => $sampleData['sort_order'],
- 'title' => $sampleData['title'],
- 'sample_url' => $sampleData['sample_url'],
- ],
- ],
- ]
- );
- $this->assertEquals($sampleId, $this->service->save($productSku, $sampleMock));
- }
- public function testUpdateWithExistingFile()
- {
- $sampleId = 1;
- $productId = 1;
- $productSku = 'simple';
- $sampleFile = '/s/a/sample.jpg';
- $encodedFile = 'something';
- $sampleData = [
- 'id' => $sampleId,
- 'title' => 'Updated Title',
- 'sort_order' => 1,
- 'sample_type' => 'file',
- 'sample_file' => $sampleFile,
- ];
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $this->productMock->expects($this->any())->method('getData')->will($this->returnValue($productId));
- $existingSampleMock = $this->createPartialMock(
- \Magento\Downloadable\Model\Sample::class,
- ['__wakeup', 'getId', 'load', 'getProductId']
- );
- $this->sampleFactoryMock->expects($this->once())->method('create')
- ->will($this->returnValue($existingSampleMock));
- $sampleMock = $this->getSampleMock($sampleData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
- ->will($this->returnValue(true));
- $existingSampleMock->expects($this->any())->method('getId')->will($this->returnValue($sampleId));
- $existingSampleMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
- $existingSampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
- $this->jsonEncoderMock->expects($this->once())
- ->method('encode')
- ->with(
- [
- [
- 'file' => $sampleFile,
- 'status' => 'old',
- ]
- ]
- )->willReturn($encodedFile);
- $this->sampleHandlerMock->expects($this->once())->method('save')->with(
- $this->productMock,
- [
- 'sample' => [
- [
- 'sample_id' => $sampleId,
- 'is_delete' => 0,
- 'type' => $sampleData['sample_type'],
- 'sort_order' => $sampleData['sort_order'],
- 'title' => $sampleData['title'],
- 'file' => $encodedFile,
- ],
- ],
- ]
- );
- $this->assertEquals($sampleId, $this->service->save($productSku, $sampleMock));
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage The sample title is empty. Enter the title and try again.
- */
- public function testUpdateThrowsExceptionIfTitleIsEmptyAndScopeIsGlobal()
- {
- $sampleId = 1;
- $productSku = 'simple';
- $productId = 1;
- $sampleData = [
- 'id' => $sampleId,
- 'title' => '',
- 'sort_order' => 1,
- ];
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $this->productMock->expects($this->any())->method('getData')->will($this->returnValue($productId));
- $existingSampleMock = $this->createPartialMock(
- \Magento\Downloadable\Model\Sample::class,
- ['__wakeup', 'getId', 'load', 'save', 'getProductId']
- );
- $existingSampleMock->expects($this->any())->method('getId')->will($this->returnValue($sampleId));
- $existingSampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
- $existingSampleMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
- $this->sampleFactoryMock->expects($this->once())->method('create')
- ->will($this->returnValue($existingSampleMock));
- $sampleMock = $this->getSampleMock($sampleData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
- ->will($this->returnValue(true));
- $this->sampleHandlerMock->expects($this->never())->method('save');
- $this->service->save($productSku, $sampleMock, true);
- }
- public function testDelete()
- {
- $sampleId = 1;
- $sampleMock = $this->createMock(\Magento\Downloadable\Model\Sample::class);
- $this->sampleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($sampleMock));
- $sampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
- $sampleMock->expects($this->any())->method('getId')->will($this->returnValue($sampleId));
- $sampleMock->expects($this->once())->method('delete');
- $this->assertTrue($this->service->delete($sampleId));
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage No downloadable sample with the provided ID was found. Verify the ID and try again.
- */
- public function testDeleteThrowsExceptionIfSampleIdIsNotValid()
- {
- $sampleId = 1;
- $sampleMock = $this->createMock(\Magento\Downloadable\Model\Sample::class);
- $this->sampleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($sampleMock));
- $sampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
- $sampleMock->expects($this->once())->method('getId');
- $sampleMock->expects($this->never())->method('delete');
- $this->service->delete($sampleId);
- }
- public function testGetList()
- {
- $productSku = 'downloadable_sku';
- $sampleData = [
- 'id' => 324,
- 'store_title' => 'rock melody sample',
- 'title' => 'just melody sample',
- 'sort_order' => 21,
- 'sample_type' => 'file',
- 'sample_url' => null,
- 'sample_file' => '/r/o/rock.melody.ogg'
- ];
- $sampleMock = $this->createPartialMock(\Magento\Downloadable\Model\Sample::class, [
- 'getId',
- 'getStoreTitle',
- 'getTitle',
- 'getSampleType',
- 'getSampleFile',
- 'getSampleUrl',
- 'getSortOrder',
- 'getData',
- '__wakeup'
- ]);
- $sampleInterfaceMock = $this->createMock(\Magento\Downloadable\Api\Data\SampleInterface::class);
- $this->repositoryMock->expects($this->once())
- ->method('get')
- ->with($productSku)
- ->will($this->returnValue($this->productMock));
- $this->productTypeMock->expects($this->once())
- ->method('getSamples')
- ->with($this->productMock)
- ->will($this->returnValue([$sampleMock]));
- $this->setSampleAssertions($sampleMock, $sampleData);
- $this->sampleDataObjectFactory->expects($this->once())->method('create')->willReturn($sampleInterfaceMock);
- $this->assertEquals([$sampleInterfaceMock], $this->service->getList($productSku));
- }
- /**
- * @param $resource
- * @param $inputData
- */
- protected function setSampleAssertions($resource, $inputData)
- {
- $resource->expects($this->any())->method('getId')->will($this->returnValue($inputData['id']));
- $resource->expects($this->any())->method('getStoreTitle')
- ->will($this->returnValue($inputData['store_title']));
- $resource->expects($this->any())->method('getTitle')
- ->will($this->returnValue($inputData['title']));
- $resource->expects($this->any())->method('getSortOrder')
- ->will($this->returnValue($inputData['sort_order']));
- $resource->expects($this->any())->method('getSampleType')
- ->will($this->returnValue($inputData['sample_type']));
- $resource->expects($this->any())->method('getSampleFile')
- ->will($this->returnValue($inputData['sample_file']));
- $resource->expects($this->any())->method('getSampleUrl')
- ->will($this->returnValue($inputData['sample_url']));
- }
- }
|