Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ContentValidatorTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class ContentValidatorTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $validator;
16 
20  protected $fileValidatorMock;
21 
25  protected $urlValidatorMock;
26 
30  protected $linkFileMock;
31 
35  protected $sampleFileMock;
36 
37  protected function setUp()
38  {
39  $this->fileValidatorMock = $this->createMock(\Magento\Downloadable\Model\File\ContentValidator::class);
40  $this->urlValidatorMock = $this->createMock(\Magento\Framework\Url\Validator::class);
41  $this->sampleFileMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
42  $this->validator = new ContentValidator($this->fileValidatorMock, $this->urlValidatorMock);
43  }
44 
45  public function testIsValid()
46  {
47  $sampleFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
48  $sampleContentData = [
49  'title' => 'Title',
50  'sort_order' => 1,
51  'sample_type' => 'file',
52  'sample_file_content' => $sampleFileContentMock,
53  ];
54  $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
55  $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
56  $contentMock = $this->getSampleContentMock($sampleContentData);
57  $this->assertTrue($this->validator->isValid($contentMock));
58  }
59 
67  {
68  $sampleContentData = [
69  'title' => 'Title',
70  'sort_order' => $sortOrder,
71  'sample_type' => 'file',
72  ];
73  $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
74  $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
75  $this->validator->isValid($this->getSampleContentMock($sampleContentData));
76  }
77 
81  public function getInvalidSortOrder()
82  {
83  return [
84  [-1],
85  [1.1],
86  ['string'],
87  ];
88  }
89 
94  protected function getSampleContentMock(array $sampleContentData)
95  {
96  $contentMock = $this->createMock(\Magento\Downloadable\Api\Data\SampleInterface::class);
97  $contentMock->expects($this->any())->method('getTitle')->will($this->returnValue(
98  $sampleContentData['title']
99  ));
100 
101  $contentMock->expects($this->any())->method('getSortOrder')->will($this->returnValue(
102  $sampleContentData['sort_order']
103  ));
104  $contentMock->expects($this->any())->method('getSampleType')->will($this->returnValue(
105  $sampleContentData['sample_type']
106  ));
107  if (isset($sampleContentData['sample_url'])) {
108  $contentMock->expects($this->any())->method('getSampleUrl')->will($this->returnValue(
109  $sampleContentData['sample_url']
110  ));
111  }
112  if (isset($sampleContentData['sample_file_content'])) {
113  $contentMock->expects($this->any())->method('getSampleFileContent')
114  ->willReturn($sampleContentData['sample_file_content']);
115  }
116  $contentMock->expects($this->any())->method('getSampleFile')->will($this->returnValue(
117  $this->sampleFileMock
118  ));
119  return $contentMock;
120  }
121 }