Skip to content

Commit

Permalink
MAGETWO-56711: Refactoring Order Invoice Business logic validation
Browse files Browse the repository at this point in the history
  • Loading branch information
valdislav committed Aug 11, 2016
1 parent 8d57692 commit 04474e5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 106 deletions.
10 changes: 2 additions & 8 deletions app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,24 @@
use Magento\Sales\Api\Data\InvoiceItemInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\Invoice\Validator\InvoiceValidatorRunner;
use Magento\Sales\Model\ValidatorInterface;

/**
* Interface InvoiceValidatorInterface
*/
class InvoiceQuantityValidator implements ValidatorInterface
{
/**
* @var InvoiceValidatorRunner
*/
private $invoiceValidatorRunner;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* InvoiceValidator constructor.
* @param InvoiceValidatorRunner $invoiceValidatorRunner
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(InvoiceValidatorRunner $invoiceValidatorRunner, OrderRepositoryInterface $orderRepository)
public function __construct(OrderRepositoryInterface $orderRepository)
{
$this->invoiceValidatorRunner = $invoiceValidatorRunner;
$this->orderRepository = $orderRepository;
}

Expand Down
23 changes: 0 additions & 23 deletions app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php

This file was deleted.

11 changes: 5 additions & 6 deletions app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: valdislav
* Date: 8/10/16
* Time: 6:51 PM
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Model\Order\Validation;


use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\ValidatorInterface;

/**
* Class CanInvoice
*/
class CanInvoice implements ValidatorInterface
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

namespace Magento\Sales\Test\Unit\Model\Order;

use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;

/**
* Test for \Magento\Sales\Model\Order\InvoiceValidator class
*/
class InvoiceValidatorTest extends \PHPUnit_Framework_TestCase
class InvoiceQuantityValidatorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Sales\Model\Order\InvoiceValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Sales\Model\Order\InvoiceQuantityValidator|\PHPUnit_Framework_MockObject_MockObject
*/
private $model;

Expand All @@ -23,15 +24,14 @@ class InvoiceValidatorTest extends \PHPUnit_Framework_TestCase
*/
private $objectManager;

/**
* @var \Magento\Sales\Model\Order\OrderValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $orderValidatorMock;

/**
* @var \Magento\Sales\Api\Data\OrderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $orderMock;
/**
* @var \Magento\Sales\Api\OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $orderRepositoryMock;

/**
* @var \Magento\Sales\Api\Data\InvoiceInterface|\PHPUnit_Framework_MockObject_MockObject
Expand All @@ -42,11 +42,6 @@ protected function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$this->orderValidatorMock = $this->getMockBuilder(\Magento\Sales\Model\Order\OrderValidatorInterface::class)
->disableOriginalConstructor()
->setMethods(['canInvoice'])
->getMockForAbstractClass();

$this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class)
->disableOriginalConstructor()
->setMethods(['getStatus'])
Expand All @@ -56,10 +51,13 @@ protected function setUp()
->disableOriginalConstructor()
->setMethods(['getTotalQty', 'getItems'])
->getMockForAbstractClass();

$this->orderRepositoryMock = $this->getMockBuilder(
OrderRepositoryInterface::class
)->disableOriginalConstructor()->getMockForAbstractClass();
$this->orderRepositoryMock->expects($this->any())->method('get')->willReturn($this->orderMock);
$this->model = $this->objectManager->getObject(
\Magento\Sales\Model\Order\InvoiceQuantityValidator::class,
['orderValidator' => $this->orderValidatorMock]
['orderRepository' => $this->orderRepositoryMock]
);
}

Expand All @@ -75,39 +73,9 @@ public function testValidate()
$this->orderMock->expects($this->once())
->method('getItems')
->willReturn([$orderItemMock]);
$this->orderValidatorMock->expects($this->once())
->method('canInvoice')
->with($this->orderMock)
->willReturn(true);
$this->assertEquals(
$expectedResult,
$this->model->validate($this->invoiceMock, $this->orderMock)
);
}

public function testValidateCanNotInvoiceOrder()
{
$orderStatus = 'Test Status';
$expectedResult = [__('An invoice cannot be created when an order has a status of %1.', $orderStatus)];
$invoiceItemMock = $this->getInvoiceItemMock(1, 1);
$this->invoiceMock->expects($this->once())
->method('getItems')
->willReturn([$invoiceItemMock]);

$orderItemMock = $this->getOrderItemMock(1, 1, true);
$this->orderMock->expects($this->once())
->method('getItems')
->willReturn([$orderItemMock]);
$this->orderMock->expects($this->once())
->method('getStatus')
->willReturn($orderStatus);
$this->orderValidatorMock->expects($this->once())
->method('canInvoice')
->with($this->orderMock)
->willReturn(false);
$this->assertEquals(
$expectedResult,
$this->model->validate($this->invoiceMock, $this->orderMock)
$this->model->validate($this->invoiceMock)
);
}

Expand All @@ -125,13 +93,9 @@ public function testValidateInvoiceQtyBiggerThanOrder()
$this->orderMock->expects($this->once())
->method('getItems')
->willReturn([$orderItemMock]);
$this->orderValidatorMock->expects($this->once())
->method('canInvoice')
->with($this->orderMock)
->willReturn(true);
$this->assertEquals(
$expectedResult,
$this->model->validate($this->invoiceMock, $this->orderMock)
$this->model->validate($this->invoiceMock)
);
}

Expand All @@ -146,13 +110,9 @@ public function testValidateNoOrderItems()
$this->orderMock->expects($this->once())
->method('getItems')
->willReturn([]);
$this->orderValidatorMock->expects($this->once())
->method('canInvoice')
->with($this->orderMock)
->willReturn(true);
$this->assertEquals(
$expectedResult,
$this->model->validate($this->invoiceMock, $this->orderMock)
$this->model->validate($this->invoiceMock)
);
}

Expand All @@ -169,13 +129,9 @@ public function testValidateNoInvoiceItems()
$this->orderMock->expects($this->once())
->method('getItems')
->willReturn([$orderItemMock]);
$this->orderValidatorMock->expects($this->once())
->method('canInvoice')
->with($this->orderMock)
->willReturn(true);
$this->assertEquals(
$expectedResult,
$this->model->validate($this->invoiceMock, $this->orderMock)
$this->model->validate($this->invoiceMock)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
/**
* Test for \Magento\Sales\Model\Order\OrderValidator class
*/
class InvoiceOrderTest extends \PHPUnit_Framework_TestCase
class CanInvoiceTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Sales\Model\Order\OrderValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Sales\Model\Order\Validation\CanInvoice|\PHPUnit_Framework_MockObject_MockObject
*/
private $model;

Expand Down Expand Up @@ -47,7 +47,7 @@ protected function setUp()
->setMethods(['getQtyToInvoice', 'getLockedDoInvoice'])
->getMockForAbstractClass();

$this->model = new \Magento\Sales\Model\Order\Invoice\Validator\InvoiceOrder();
$this->model = new \Magento\Sales\Model\Order\Validation\CanInvoice();
}

/**
Expand All @@ -62,8 +62,7 @@ public function testCanInvoiceWrongState($state)
->willReturn($state);
$this->orderMock->expects($this->never())
->method('getItems');
$this->assertEquals(
false,
$this->assertNotEmpty(
$this->model->validate($this->orderMock)
);
}
Expand Down Expand Up @@ -93,8 +92,7 @@ public function testCanInvoiceNoItems()
->method('getItems')
->willReturn([]);

$this->assertEquals(
false,
$this->assertNotEmpty(
$this->model->validate($this->orderMock)
);
}
Expand Down Expand Up @@ -124,8 +122,7 @@ public function testCanInvoice($qtyToInvoice, $itemLockedDoInvoice, $expectedRes
->willReturn($itemLockedDoInvoice);

$this->assertEquals(
$expectedResult,
$this->model->validate($this->orderMock)
$expectedResult, empty($this->model->validate($this->orderMock))
);
}

Expand Down

0 comments on commit 04474e5

Please sign in to comment.