|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Sales\Test\Unit\Model\Order\Creditmemo; |
| 7 | + |
| 8 | +use Magento\Sales\Api\OrderItemRepositoryInterface; |
| 9 | +use Magento\Sales\Model\Order\Creditmemo\Item\Validation\CreationQuantityValidator; |
| 10 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 11 | +use Magento\Sales\Api\Data\OrderInterface; |
| 12 | +use Magento\Sales\Model\Order\Item; |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +class CreateQuantityValidatorTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var OrderItemRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 20 | + */ |
| 21 | + protected $orderItemRepositoryMock; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var Item|\PHPUnit_Framework_MockObject_MockObject |
| 25 | + */ |
| 26 | + protected $orderItemMock; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var CreationQuantityValidator|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + protected $createQuantityValidator; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + protected $contexMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \stdClass|\PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 41 | + protected $entity; |
| 42 | + |
| 43 | + protected function setUp() |
| 44 | + { |
| 45 | + $this->orderItemRepositoryMock = $this->getMockBuilder(OrderItemRepositoryInterface::class) |
| 46 | + ->disableOriginalConstructor() |
| 47 | + ->setMethods(['get']) |
| 48 | + ->getMockForAbstractClass(); |
| 49 | + |
| 50 | + $this->orderItemMock = $this->getMockBuilder(Item::class) |
| 51 | + ->disableOriginalConstructor() |
| 52 | + ->getMockForAbstractClass(); |
| 53 | + |
| 54 | + $this->entity = $this->getMockBuilder(\stdClass::class) |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->setMethods(['getOrderItemId', 'getQty']) |
| 57 | + ->getMock(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dataProvider dataProvider |
| 62 | + */ |
| 63 | + public function testValidateCreditMemoProductItems($orderItemId, $expectedResult, $withContext = false) |
| 64 | + { |
| 65 | + if ($orderItemId) { |
| 66 | + $this->entity->expects($this->once()) |
| 67 | + ->method('getOrderItemId') |
| 68 | + ->willReturn($orderItemId); |
| 69 | + |
| 70 | + $this->orderItemRepositoryMock->expects($this->once()) |
| 71 | + ->method('get') |
| 72 | + ->with($orderItemId) |
| 73 | + ->willReturn($this->orderItemMock); |
| 74 | + } else { |
| 75 | + $this->entity->expects($this->once()) |
| 76 | + ->method('getOrderItemId') |
| 77 | + ->willThrowException(new NoSuchEntityException()); |
| 78 | + } |
| 79 | + |
| 80 | + $this->contexMock = null; |
| 81 | + if ($withContext) { |
| 82 | + $this->contexMock = $this->getMockBuilder(OrderInterface::class) |
| 83 | + ->disableOriginalConstructor() |
| 84 | + ->getMockForAbstractClass(); |
| 85 | + |
| 86 | + $this->entity->expects($this->once()) |
| 87 | + ->method('getQty') |
| 88 | + ->willReturn(11); |
| 89 | + } |
| 90 | + |
| 91 | + $this->createQuantityValidator = New CreationQuantityValidator( |
| 92 | + $this->orderItemRepositoryMock, |
| 93 | + $this->contexMock |
| 94 | + ); |
| 95 | + |
| 96 | + $this->assertEquals($expectedResult, $this->createQuantityValidator->validate($this->entity)); |
| 97 | + } |
| 98 | + |
| 99 | + public function dataProvider() |
| 100 | + { |
| 101 | + return [ |
| 102 | + 'testValidateCreditMemoProductItems' => [ |
| 103 | + 1, |
| 104 | + [__('The creditmemo contains product item that is not part of the original order.')], |
| 105 | + ], |
| 106 | + 'testValidateWithException' => [ |
| 107 | + null, |
| 108 | + [__('The creditmemo contains product item that is not part of the original order.')] |
| 109 | + ], |
| 110 | + 'testValidateWithContext' => [ |
| 111 | + 1, |
| 112 | + [__('The quantity to refund must not be greater than the unrefunded quantity.')], |
| 113 | + true |
| 114 | + ], |
| 115 | + ]; |
| 116 | + } |
| 117 | +} |
0 commit comments