ValidatorTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Sales\Test\Unit\Model;
  8. use Magento\Framework\DataObject;
  9. use Magento\Framework\Exception\ConfigurationMismatchException;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  12. use Magento\Sales\Api\Data\OrderInterface;
  13. use Magento\Sales\Model\Validator;
  14. use Magento\Sales\Model\ValidatorInterface;
  15. use Magento\Sales\Model\ValidatorResultInterface;
  16. use Magento\Sales\Model\ValidatorResultInterfaceFactory;
  17. /**
  18. * @covers \Magento\Sales\Model\Validator
  19. */
  20. class ValidatorTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * Testable Object
  24. *
  25. * @var Validator
  26. */
  27. private $validator;
  28. /**
  29. * Object Manager
  30. *
  31. * @var ObjectManager
  32. */
  33. private $objectManager;
  34. /**
  35. * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $objectManagerMock;
  38. /**
  39. * @var ValidatorResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $validatorResultFactoryMock;
  42. /**
  43. * @var ValidatorResultInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $validatorResultMock;
  46. /**
  47. * @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $validatorMock;
  50. /**
  51. * @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $entityMock;
  54. /**
  55. * Set Up
  56. *
  57. * @return void
  58. */
  59. protected function setUp()
  60. {
  61. $this->objectManagerMock = $this->createMock(ObjectManagerInterface::class);
  62. $this->entityMock = $this->createMock(OrderInterface::class);
  63. $this->validatorMock = $this->createMock(ValidatorInterface::class);
  64. $this->validatorResultFactoryMock = $this->getMockBuilder(ValidatorResultInterfaceFactory::class)
  65. ->setMethods(['create'])->disableOriginalConstructor()->getMock();
  66. $this->validatorResultMock = $this->createMock(ValidatorResultInterface::class);
  67. $this->validatorResultFactoryMock->expects($this->any())->method('create')
  68. ->willReturn($this->validatorResultMock);
  69. $this->objectManager = new ObjectManager($this);
  70. $this->validator = $this->objectManager->getObject(
  71. Validator::class,
  72. [
  73. 'objectManager' => $this->objectManagerMock,
  74. 'validatorResult' => $this->validatorResultFactoryMock,
  75. ]
  76. );
  77. }
  78. /**
  79. * Test validate method
  80. *
  81. * @return void
  82. *
  83. * @throws ConfigurationMismatchException
  84. */
  85. public function testValidate()
  86. {
  87. $validatorName = 'test';
  88. $validators = [$validatorName];
  89. $context = new DataObject();
  90. $validatorArguments = ['context' => $context];
  91. $message = __('Sample message.');
  92. $messages = [$message];
  93. $this->objectManagerMock->expects($this->once())->method('create')
  94. ->with($validatorName, $validatorArguments)->willReturn($this->validatorMock);
  95. $this->validatorMock->expects($this->once())->method('validate')->with($this->entityMock)
  96. ->willReturn($messages);
  97. $this->validatorResultMock->expects($this->once())->method('addMessage')->with($message);
  98. $expected = $this->validatorResultMock;
  99. $actual = $this->validator->validate($this->entityMock, $validators, $context);
  100. $this->assertEquals($expected, $actual);
  101. }
  102. /**
  103. * Test validate method
  104. *
  105. * @return void
  106. *
  107. * @throws ConfigurationMismatchException
  108. */
  109. public function testValidateWithException()
  110. {
  111. $validatorName = 'test';
  112. $validators = [$validatorName];
  113. $this->objectManagerMock->expects($this->once())->method('create')->willReturn(null);
  114. $this->validatorResultMock->expects($this->never())->method('addMessage');
  115. $this->expectException(ConfigurationMismatchException::class);
  116. $this->validator->validate($this->entityMock, $validators);
  117. }
  118. }