|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Alma\API\Tests\Unit\Legacy\Endpoints; |
| 4 | + |
| 5 | + |
| 6 | +use Alma\API\ClientContext; |
| 7 | +use Alma\API\Endpoints\Orders; |
| 8 | + |
| 9 | +use Alma\API\Exceptions\MissingKeyException; |
| 10 | +use Alma\API\Exceptions\ParametersException; |
| 11 | +use Alma\API\Exceptions\RequestException; |
| 12 | +use Alma\API\Lib\ArrayUtils; |
| 13 | +use Alma\API\Request; |
| 14 | +use Alma\API\Response; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | + |
| 18 | +class OrdersTest extends TestCase |
| 19 | +{ |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Mockery\Mock|(\Mockery\MockInterface&Orders) |
| 23 | + */ |
| 24 | + protected $orderEndpoint; |
| 25 | + |
| 26 | + public function setUp() |
| 27 | + { |
| 28 | + $this->clientContext = \Mockery::mock(ClientContext::class); |
| 29 | + $this->orderEndpoint = \Mockery::mock(Orders::class)->makePartial(); |
| 30 | + $this->arrayUtils = \Mockery::mock(ArrayUtils::class)->makePartial(); |
| 31 | + $this->responseMock = \Mockery::mock(Response::class); |
| 32 | + $this->requestObject = \Mockery::mock(Request::class); |
| 33 | + $loggerMock = \Mockery::mock(LoggerInterface::class); |
| 34 | + $loggerMock->shouldReceive('error'); |
| 35 | + |
| 36 | + $this->orderEndpoint->arrayUtils = $this->arrayUtils; |
| 37 | + $this->externalId = 'Mon external Id'; |
| 38 | + $this->status = 'Mon Status - 1'; |
| 39 | + $this->clientContext->logger = $loggerMock; |
| 40 | + $this->orderEndpoint->setClientContext($this->clientContext); |
| 41 | + |
| 42 | + } |
| 43 | + public function testValidateStatusDataNoOrderData() |
| 44 | + { |
| 45 | + $this->expectException(ParametersException::class); |
| 46 | + $this->expectExceptionMessage('Missing in the required parameters (status, is_shipped) when calling orders.sendStatus'); |
| 47 | + $this->orderEndpoint->validateStatusData(); |
| 48 | + } |
| 49 | + |
| 50 | + public function testValidateStatusDataMissingSomeOrderData() |
| 51 | + { |
| 52 | + $orderEndpoint = \Mockery::mock(Orders::class)->makePartial(); |
| 53 | + $arrayUtils = \Mockery::mock(ArrayUtils::class)->makePartial(); |
| 54 | + $arrayUtils->shouldReceive('checkMandatoryKeys')->andThrow(new MissingKeyException()); |
| 55 | + $orderEndpoint->arrayUtils = $arrayUtils; |
| 56 | + |
| 57 | + $this->expectException(ParametersException::class); |
| 58 | + $this->expectExceptionMessage('Error in the required parameters (status, is_shipped) when calling orders.sendStatus'); |
| 59 | + $orderEndpoint->validateStatusData(array('status')); |
| 60 | + } |
| 61 | + |
| 62 | + public function testValidateStatusDataIsShippedNotBool() |
| 63 | + { |
| 64 | + $orderEndpoint = \Mockery::mock(Orders::class)->makePartial(); |
| 65 | + $arrayUtils = \Mockery::mock(ArrayUtils::class)->makePartial(); |
| 66 | + $arrayUtils->shouldReceive('checkMandatoryKeys')->andReturn(null); |
| 67 | + $orderEndpoint->arrayUtils = $arrayUtils; |
| 68 | + |
| 69 | + $this->expectException(ParametersException::class); |
| 70 | + $this->expectExceptionMessage('Parameter "is_shipped" must be a boolean'); |
| 71 | + |
| 72 | + $orderEndpoint->validateStatusData(array( |
| 73 | + 'status' => $this->status, |
| 74 | + 'is_shipped' => 'oui', |
| 75 | + )); |
| 76 | + } |
| 77 | + |
| 78 | + public function testValidateStatusDataStatusIsEmpty() |
| 79 | + { |
| 80 | + $orderEndpoint = \Mockery::mock(Orders::class)->makePartial(); |
| 81 | + $arrayUtils = \Mockery::mock(ArrayUtils::class)->makePartial(); |
| 82 | + $arrayUtils->shouldReceive('checkMandatoryKeys')->andReturn(null); |
| 83 | + $orderEndpoint->arrayUtils = $arrayUtils; |
| 84 | + |
| 85 | + $this->expectException(ParametersException::class); |
| 86 | + $this->expectExceptionMessage('Missing the required parameter "status" when calling orders.sendStatus'); |
| 87 | + |
| 88 | + $orderEndpoint->validateStatusData(array( |
| 89 | + 'status' => '', |
| 90 | + 'is_shipped' => true, |
| 91 | + )); |
| 92 | + } |
| 93 | + |
| 94 | + public function testSendStatusOK() |
| 95 | + { |
| 96 | + $this->orderEndpoint->shouldReceive('validateStatusData')->andReturn(null); |
| 97 | + |
| 98 | + $this->responseMock->shouldReceive('isError')->once()->andReturn(false); |
| 99 | + $this->requestObject->shouldReceive('setRequestBody')->andReturn($this->requestObject); |
| 100 | + |
| 101 | + $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); |
| 102 | + $this->orderEndpoint->shouldReceive('request') |
| 103 | + ->with(Orders::ORDERS_PATH_V2 . "/{$this->externalId}/status") |
| 104 | + ->once() |
| 105 | + ->andReturn($this->requestObject); |
| 106 | + |
| 107 | + $this->orderEndpoint->sendStatus($this->externalId , array( |
| 108 | + 'status' => $this->status, |
| 109 | + 'is_shipped' => true, |
| 110 | + )); |
| 111 | + } |
| 112 | + |
| 113 | + public function testSendStatusRequestError() |
| 114 | + { |
| 115 | + $this->orderEndpoint->shouldReceive('validateStatusData')->andReturn(null); |
| 116 | + |
| 117 | + $this->responseMock->shouldReceive('isError')->once()->andReturn(true); |
| 118 | + $this->requestObject->shouldReceive('setRequestBody')->andReturn($this->requestObject); |
| 119 | + |
| 120 | + $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); |
| 121 | + $this->orderEndpoint->shouldReceive('request') |
| 122 | + ->with(Orders::ORDERS_PATH_V2 . "/{$this->externalId}/status") |
| 123 | + ->once() |
| 124 | + ->andReturn($this->requestObject); |
| 125 | + |
| 126 | + $this->expectException(RequestException::class); |
| 127 | + $this->orderEndpoint->sendStatus($this->externalId , array( |
| 128 | + 'status' => $this->status, |
| 129 | + 'is_shipped' => true, |
| 130 | + )); |
| 131 | + |
| 132 | + } |
| 133 | + public function testSendStatusWithException() |
| 134 | + { |
| 135 | + $this->orderEndpoint->shouldReceive('validateStatusData')->andReturn(null); |
| 136 | + |
| 137 | + $this->responseMock->shouldReceive('isError')->once()->andReturn(false); |
| 138 | + $this->requestObject->shouldReceive('setRequestBody')->andReturn($this->requestObject); |
| 139 | + |
| 140 | + $this->requestObject->shouldReceive('post')->andThrow(new RequestException()); |
| 141 | + $this->orderEndpoint->shouldReceive('request') |
| 142 | + ->with(Orders::ORDERS_PATH_V2 . "/{$this->externalId}/status") |
| 143 | + ->once() |
| 144 | + ->andReturn($this->requestObject); |
| 145 | + |
| 146 | + $this->expectException(RequestException::class); |
| 147 | + $this->orderEndpoint->sendStatus($this->externalId , array( |
| 148 | + 'status' => $this->status, |
| 149 | + 'is_shipped' => true, |
| 150 | + )); |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + public function tearDown() |
| 155 | + { |
| 156 | + $this->orderEndpoint = null; |
| 157 | + $this->arrayUtils = null; |
| 158 | + $this->responseMock = null; |
| 159 | + $this->requestObject = null; |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments