Created
September 29, 2022 09:32
-
-
Save rafaelola/2a70cc93dd7e97df92d71f98515d80e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Develodesign\BarcodeScanner\Test\Unit\Controller\Adminhtml; | |
use Develodesign\BarcodeScanner\Controller\Adminhtml\Barcodescanner\Scan; | |
use Magento\Framework\App\RequestInterface; | |
use Magento\Framework\Controller\Result\Json; | |
use Magento\Framework\Controller\Result\JsonFactory; | |
use Magento\Framework\Exception\LocalizedException; | |
use Magento\Framework\Exception\NoSuchEntityException; | |
use Magento\Sales\Api\Data\OrderInterface; | |
use Magento\Sales\Api\OrderRepositoryInterface; | |
use PHPUnit\Framework\MockObject\MockObject; | |
use PHPUnit\Framework\TestCase; | |
class ScanTest extends TestCase | |
{ | |
/** | |
* @var Scan | |
*/ | |
private $controller; | |
/** | |
* @var RequestInterface|MockObject; | |
*/ | |
private $requestInterfaceMock; | |
/** | |
* @var OrderRepositoryInterface|MockObject | |
*/ | |
private $orderRepositoryMock; | |
/** | |
* @var Json|MockObject | |
*/ | |
private $resultJsonMock; | |
/** | |
* @var JsonFactory|MockObject | |
*/ | |
private $resultJsonFactoryMock; | |
protected function setUp(): void | |
{ | |
$this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class) | |
->disableOriginalConstructor() | |
->getMockForAbstractClass(); | |
$this->requestInterfaceMock = $this->getMockBuilder(RequestInterface::class) | |
->disableOriginalConstructor() | |
->getMockForAbstractClass(); | |
$this->resultJsonFactoryMock = $this->createMock(JsonFactory::class); | |
$this->resultJsonMock = $this->createMock(Json::class); | |
$this->resultJsonFactoryMock->expects($this->once()) | |
->method('create') | |
->willReturn($this->resultJsonMock); | |
$this->controller = new Scan( | |
$this->requestInterfaceMock, | |
$this->orderRepositoryMock, | |
$this->resultJsonFactoryMock | |
); | |
} | |
/** | |
* @return void | |
*/ | |
public function testExecuteNullRequestParam(): void | |
{ | |
$orderIdParam = null; | |
$param = 'order_id'; | |
$this->requestInterfaceMock->expects($this->once())->method('getParam')->with($param)->willReturn($orderIdParam); | |
try { | |
$this->controller->execute(); | |
} catch (LocalizedException $e) { | |
$this->resultJsonMock->expects($this->once()) | |
->method('setData') | |
->with([ | |
'success' => false, | |
'error' => true, | |
'error_messages' => 'Query parameter, Order ID is required to perform this action' | |
]) | |
->willReturn($this->resultJsonMock); | |
$this->assertEquals('Query parameter, Order ID is required to perform this action.', $e->getMessage()); | |
} | |
} | |
/** | |
* @throws LocalizedException | |
*/ | |
public function testNonExistingOrderEntity(): void | |
{ | |
$orderIdParam = 787; | |
$param = 'order_id'; | |
$this->requestInterfaceMock->expects($this->once())->method('getParam')->with($param)->willReturn($orderIdParam); | |
$this->orderRepositoryMock->expects($this->once()) | |
->method('get') | |
->with($orderIdParam) | |
->willThrowException( | |
new NoSuchEntityException( | |
__("The entity that was requested doesn't exist. Verify the entity and try again.") | |
) | |
); | |
$this->resultJsonMock->expects($this->once()) | |
->method('setData') | |
->with([ | |
'success' => false, | |
'error' => true, | |
'error_messages' => 'The entity that was requested doesn\'t exist. Verify the entity and try again.' | |
]) | |
->willReturn($this->resultJsonMock); | |
$this->assertEquals($this->resultJsonMock, $this->controller->execute()); | |
} | |
/** | |
* @throws LocalizedException | |
*/ | |
public function testRetrieveOrderData(): void | |
{ | |
$orderIdParam = 787; | |
$param = 'order_id'; | |
$incrementId = '100000009'; | |
$entityId = 788; | |
$orderGrandTotal = 290.29; | |
$customerId = 79; | |
$customerEmail = 'team@develo.co.uk'; | |
$customerFirstname = 'Develo'; | |
$customerLastname = 'Fela'; | |
$createdAt = '2021-04-04 10:43:18'; | |
$orderMock = $this->getMockBuilder(OrderInterface::class) | |
->disableOriginalConstructor() | |
->addMethods(['getData']) | |
->getMockForAbstractClass(); | |
$this->requestInterfaceMock->expects($this->once())->method('getParam')->with($param)->willReturn($orderIdParam); | |
$this->orderRepositoryMock->expects($this->once()) | |
->method('get') | |
->with($orderIdParam) | |
->willReturn($orderMock); | |
$orderMock->expects($this->once()) | |
->method('getData') | |
->willReturn([ | |
'increment_id' => $incrementId, | |
'order_id' => $orderIdParam, | |
'entity_id' => $entityId, | |
'created_at' => $createdAt, | |
'grand_total' => $orderGrandTotal, | |
'customer_id' => $customerId, | |
'customer_email' => $customerEmail, | |
'customer_firstname' => $customerFirstname, | |
'customer_lastname' => $customerLastname | |
]); | |
$this->resultJsonMock->expects($this->once()) | |
->method('setData') | |
->with([ | |
'success' => true, | |
'error' => false, | |
'data' => [ | |
'increment_id' => $incrementId, | |
'order_id' => $orderIdParam, | |
'entity_id' => $entityId, | |
'created_at' => $createdAt, | |
'grand_total' => $orderGrandTotal, | |
'customer_id' => $customerId, | |
'customer_email' => $customerEmail, | |
'customer_firstname' => $customerFirstname, | |
'customer_lastname' => $customerLastname | |
] | |
]) | |
->willReturn($this->resultJsonMock); | |
$this->assertEquals($this->resultJsonMock, $this->controller->execute()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment