123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Downloadable\Test\Unit\Model\Sample;
- use Magento\Downloadable\Model\Sample\ContentValidator;
- class ContentValidatorTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var ContentValidator
- */
- protected $validator;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $fileValidatorMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $urlValidatorMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $linkFileMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $sampleFileMock;
- protected function setUp()
- {
- $this->fileValidatorMock = $this->createMock(\Magento\Downloadable\Model\File\ContentValidator::class);
- $this->urlValidatorMock = $this->createMock(\Magento\Framework\Url\Validator::class);
- $this->sampleFileMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
- $this->validator = new ContentValidator($this->fileValidatorMock, $this->urlValidatorMock);
- }
- public function testIsValid()
- {
- $sampleFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
- $sampleContentData = [
- 'title' => 'Title',
- 'sort_order' => 1,
- 'sample_type' => 'file',
- 'sample_file_content' => $sampleFileContentMock,
- ];
- $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
- $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
- $contentMock = $this->getSampleContentMock($sampleContentData);
- $this->assertTrue($this->validator->isValid($contentMock));
- }
- /**
- * @param string|int|float $sortOrder
- * @dataProvider getInvalidSortOrder
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage Sort order must be a positive integer.
- */
- public function testIsValidThrowsExceptionIfSortOrderIsInvalid($sortOrder)
- {
- $sampleContentData = [
- 'title' => 'Title',
- 'sort_order' => $sortOrder,
- 'sample_type' => 'file',
- ];
- $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
- $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
- $this->validator->isValid($this->getSampleContentMock($sampleContentData));
- }
- /**
- * @return array
- */
- public function getInvalidSortOrder()
- {
- return [
- [-1],
- [1.1],
- ['string'],
- ];
- }
- /**
- * @param array $sampleContentData
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getSampleContentMock(array $sampleContentData)
- {
- $contentMock = $this->createMock(\Magento\Downloadable\Api\Data\SampleInterface::class);
- $contentMock->expects($this->any())->method('getTitle')->will($this->returnValue(
- $sampleContentData['title']
- ));
- $contentMock->expects($this->any())->method('getSortOrder')->will($this->returnValue(
- $sampleContentData['sort_order']
- ));
- $contentMock->expects($this->any())->method('getSampleType')->will($this->returnValue(
- $sampleContentData['sample_type']
- ));
- if (isset($sampleContentData['sample_url'])) {
- $contentMock->expects($this->any())->method('getSampleUrl')->will($this->returnValue(
- $sampleContentData['sample_url']
- ));
- }
- if (isset($sampleContentData['sample_file_content'])) {
- $contentMock->expects($this->any())->method('getSampleFileContent')
- ->willReturn($sampleContentData['sample_file_content']);
- }
- $contentMock->expects($this->any())->method('getSampleFile')->will($this->returnValue(
- $this->sampleFileMock
- ));
- return $contentMock;
- }
- }
|