ValidatorTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Test\Unit\Model\Option;
  7. use Magento\Framework\Validator\NotEmpty;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. class ValidatorTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Bundle\Model\Option\Validator
  13. */
  14. private $validator;
  15. /**
  16. * SetUp method for unit test
  17. */
  18. protected function setUp()
  19. {
  20. $helper = new ObjectManager($this);
  21. $validate = $helper->getObject(\Magento\Framework\Validator\NotEmpty::class, ['options' => NotEmpty::ALL]);
  22. $validateFactory = $this->getMockBuilder(\Magento\Framework\Validator\NotEmptyFactory::class)
  23. ->setMethods(['create'])
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $validateFactory->expects($this->once())
  27. ->method('create')
  28. ->willReturn($validate);
  29. $this->validator = $helper->getObject(
  30. \Magento\Bundle\Model\Option\Validator::class,
  31. ['notEmptyFactory' => $validateFactory]
  32. );
  33. }
  34. /**
  35. * Test for method isValid
  36. *
  37. * @param string $title
  38. * @param string $type
  39. * @param bool $isValid
  40. * @param string[] $expectedMessages
  41. * @dataProvider providerIsValid
  42. */
  43. public function testIsValid($title, $type, $isValid, $expectedMessages)
  44. {
  45. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Bundle\Model\Option $option */
  46. $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)
  47. ->setMethods(['getTitle', 'getType'])
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $option->expects($this->once())
  51. ->method('getTitle')
  52. ->willReturn($title);
  53. $option->expects($this->once())
  54. ->method('getType')
  55. ->willReturn($type);
  56. $this->assertEquals($isValid, $this->validator->isValid($option));
  57. $this->assertEquals($expectedMessages, $this->validator->getMessages());
  58. }
  59. /**
  60. * Provider for testIsValid
  61. */
  62. public function providerIsValid()
  63. {
  64. return [
  65. ['title', 'select', true, []],
  66. ['title', null, false, ['type' => '"type" is required. Enter and try again.']],
  67. [null, 'select', false, ['title' => '"title" is required. Enter and try again.']],
  68. [
  69. null,
  70. null,
  71. false,
  72. [
  73. 'type' => '"type" is required. Enter and try again.',
  74. 'title' => '"title" is required. Enter and try again.'
  75. ]
  76. ]
  77. ];
  78. }
  79. }