ContentValidatorTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Test\Unit\Model\Sample;
  7. use Magento\Downloadable\Model\Sample\ContentValidator;
  8. class ContentValidatorTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var ContentValidator
  12. */
  13. protected $validator;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $fileValidatorMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $urlValidatorMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $linkFileMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $sampleFileMock;
  30. protected function setUp()
  31. {
  32. $this->fileValidatorMock = $this->createMock(\Magento\Downloadable\Model\File\ContentValidator::class);
  33. $this->urlValidatorMock = $this->createMock(\Magento\Framework\Url\Validator::class);
  34. $this->sampleFileMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
  35. $this->validator = new ContentValidator($this->fileValidatorMock, $this->urlValidatorMock);
  36. }
  37. public function testIsValid()
  38. {
  39. $sampleFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
  40. $sampleContentData = [
  41. 'title' => 'Title',
  42. 'sort_order' => 1,
  43. 'sample_type' => 'file',
  44. 'sample_file_content' => $sampleFileContentMock,
  45. ];
  46. $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  47. $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  48. $contentMock = $this->getSampleContentMock($sampleContentData);
  49. $this->assertTrue($this->validator->isValid($contentMock));
  50. }
  51. /**
  52. * @param string|int|float $sortOrder
  53. * @dataProvider getInvalidSortOrder
  54. * @expectedException \Magento\Framework\Exception\InputException
  55. * @expectedExceptionMessage Sort order must be a positive integer.
  56. */
  57. public function testIsValidThrowsExceptionIfSortOrderIsInvalid($sortOrder)
  58. {
  59. $sampleContentData = [
  60. 'title' => 'Title',
  61. 'sort_order' => $sortOrder,
  62. 'sample_type' => 'file',
  63. ];
  64. $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  65. $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  66. $this->validator->isValid($this->getSampleContentMock($sampleContentData));
  67. }
  68. /**
  69. * @return array
  70. */
  71. public function getInvalidSortOrder()
  72. {
  73. return [
  74. [-1],
  75. [1.1],
  76. ['string'],
  77. ];
  78. }
  79. /**
  80. * @param array $sampleContentData
  81. * @return \PHPUnit_Framework_MockObject_MockObject
  82. */
  83. protected function getSampleContentMock(array $sampleContentData)
  84. {
  85. $contentMock = $this->createMock(\Magento\Downloadable\Api\Data\SampleInterface::class);
  86. $contentMock->expects($this->any())->method('getTitle')->will($this->returnValue(
  87. $sampleContentData['title']
  88. ));
  89. $contentMock->expects($this->any())->method('getSortOrder')->will($this->returnValue(
  90. $sampleContentData['sort_order']
  91. ));
  92. $contentMock->expects($this->any())->method('getSampleType')->will($this->returnValue(
  93. $sampleContentData['sample_type']
  94. ));
  95. if (isset($sampleContentData['sample_url'])) {
  96. $contentMock->expects($this->any())->method('getSampleUrl')->will($this->returnValue(
  97. $sampleContentData['sample_url']
  98. ));
  99. }
  100. if (isset($sampleContentData['sample_file_content'])) {
  101. $contentMock->expects($this->any())->method('getSampleFileContent')
  102. ->willReturn($sampleContentData['sample_file_content']);
  103. }
  104. $contentMock->expects($this->any())->method('getSampleFile')->will($this->returnValue(
  105. $this->sampleFileMock
  106. ));
  107. return $contentMock;
  108. }
  109. }