Skip to content

Commit ae0409c

Browse files
committed
working on coverage
1 parent a105f19 commit ae0409c

11 files changed

Lines changed: 477 additions & 61 deletions

src/Message/Events/ShipEngineEvent.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ShipEngine\Message\Events;
44

55
use DateTime;
6+
use ShipEngine\Message\InvalidFieldValueException;
67
use ShipEngine\Message\ShipEngineException;
78
use ShipEngine\ShipEngineConfig;
89
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -98,7 +99,11 @@ public static function emitEvent(string $eventType, $eventData, ShipEngineConfig
9899
$dispatcher->dispatch($responseReceivedEvent, $responseReceivedEvent::RESPONSE_RECEIVED);
99100
return $responseReceivedEvent;
100101
default:
101-
throw new ShipEngineException("Event type [$eventType] is not a valid type of event.");
102+
throw new InvalidFieldValueException(
103+
'eventType',
104+
"Event type [$eventType] is not a valid type of event.",
105+
$eventType
106+
);
102107
}
103108
}
104109
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Message\Events;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use ShipEngine\Message\Events\EventMessage;
7+
use ShipEngine\Message\ShipEngineException;
8+
use ShipEngine\Util\Constants\RPCMethods;
9+
10+
/**
11+
* Class EventMessageTest
12+
*
13+
* @covers \ShipEngine\Message\Events\EventMessage
14+
* @uses \ShipEngine\Message\ShipEngineException
15+
*/
16+
final class EventMessageTest extends TestCase
17+
{
18+
/**
19+
* Test the **newEventMessage** method.
20+
*/
21+
public function testNewEventMessage(): void
22+
{
23+
$baseUri = 'https://www.google.com';
24+
$method = RPCMethods::ADDRESS_VALIDATE;
25+
$validMessageTypeArray = $this->validMessageTypes();
26+
$validMessages = $this->validMessages($method, $baseUri);
27+
28+
foreach ($validMessageTypeArray as $message) {
29+
$eventMessage = EventMessage::newEventMessage(
30+
$method,
31+
$baseUri,
32+
$message
33+
);
34+
35+
$this->assertIsString($eventMessage);
36+
}
37+
38+
$this->assertEquals("Calling the ShipEngine $method API at $baseUri", $validMessages[0]);
39+
$this->assertEquals("Retrying the ShipEngine $method API at $baseUri", $validMessages[1]);
40+
}
41+
42+
/**
43+
* Tests the eventMessage exception case.
44+
*/
45+
public function testNewEventMessageExceptionCase(): void
46+
{
47+
try {
48+
$eventMessage = EventMessage::newEventMessage(
49+
RPCMethods::ADDRESS_VALIDATE,
50+
'https://www.google.com',
51+
'pizza_ready'
52+
);
53+
} catch (ShipEngineException $err) {
54+
$this->assertInstanceOf(ShipEngineException::class, $err);
55+
}
56+
}
57+
58+
/**
59+
* A method that constructs the a message in the correct format.
60+
*
61+
* @param string $method
62+
* @param string $baseUri
63+
* @return string[]
64+
*/
65+
private function validMessages(string $method, string $baseUri): array
66+
{
67+
return array(
68+
"Calling the ShipEngine $method API at $baseUri",
69+
"Retrying the ShipEngine $method API at $baseUri"
70+
);
71+
}
72+
73+
/**
74+
* A method that returns an array of valid **messageTypes**
75+
*
76+
* @return string[]
77+
*/
78+
private function validMessageTypes(): array
79+
{
80+
return array('base_message', 'retry_message');
81+
}
82+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Message\Events;
4+
5+
use ShipEngine\Message\Events\EventOptions;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* Class EventOptionsTest
10+
*
11+
* @covers \ShipEngine\Message\Events\EventOptions
12+
*/
13+
final class EventOptionsTest extends TestCase
14+
{
15+
/**
16+
* Test instantiation of the **EventOptions** object.
17+
*/
18+
public function testConstruct(): void
19+
{
20+
$requestEventData = new EventOptions([
21+
'test event options message',
22+
'id',
23+
'baseUri',
24+
'requestHeaders',
25+
'body',
26+
'retry',
27+
'timeout'
28+
]);
29+
30+
$this->assertInstanceOf(EventOptions::class, $requestEventData);
31+
}
32+
}

tests/Message/Events/RequestSentEventTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,22 @@ private function testConfig(object $eventListener, int $retries): array
279279
'eventListener' => $eventListener
280280
);
281281
}
282+
283+
/**
284+
* Test the jsonSerialize method on the the RequestSentEvent.
285+
*/
286+
public function testJsonSerialize(): void
287+
{
288+
$requestSentEvent = new RequestSentEvent(
289+
'testing the request sent event.',
290+
'req_h08s7fe7h3f4fhq4fw4f5',
291+
'https://google.com',
292+
array(),
293+
array(),
294+
300,
295+
new DateInterval('PT300S')
296+
);
297+
298+
$this->assertJson(json_encode($requestSentEvent));
299+
}
282300
}

tests/Message/Events/ResponseReceivedEventTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Mockery;
88
use Mockery\Adapter\Phpunit\MockeryTestCase;
99
use Psr\Http\Client\ClientExceptionInterface;
10+
use ShipEngine\Message\Events\RequestSentEvent;
1011
use ShipEngine\Message\Events\ResponseReceivedEvent;
1112
use ShipEngine\Message\ShipEngineException;
1213
use ShipEngine\Model\Address\Address;
@@ -216,4 +217,23 @@ private function testConfig(object $eventListener, int $retries): array
216217
'eventListener' => $eventListener
217218
);
218219
}
220+
221+
/**
222+
* Test the jsonSerialize method on the the RequestSentEvent.
223+
*/
224+
public function testJsonSerialize(): void
225+
{
226+
$responseReceivedEvent = new ResponseReceivedEvent(
227+
'testing the request sent event.',
228+
'req_h08s7fe7h3f4fhq4fw4f5',
229+
'https://google.com',
230+
200,
231+
array(),
232+
array(),
233+
300,
234+
new DateInterval('PT3S')
235+
);
236+
237+
$this->assertJson(json_encode($responseReceivedEvent));
238+
}
219239
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Message\Events;
4+
5+
use DateInterval;
6+
use ShipEngine\Message\Events\ShipEngineEvent;
7+
use PHPUnit\Framework\TestCase;
8+
use ShipEngine\Message\InvalidFieldValueException;
9+
use ShipEngine\Message\ShipEngineException;
10+
use ShipEngine\ShipEngineConfig;
11+
use ShipEngine\Util\Constants\Endpoints;
12+
13+
/**
14+
* Class ShipEngineEventTest
15+
*
16+
* @covers \ShipEngine\Message\Events\ShipEngineEvent
17+
* @uses \ShipEngine\ShipEngineConfig
18+
* @uses \ShipEngine\Message\ShipEngineException
19+
* @uses \ShipEngine\Message\InvalidFieldValueException
20+
* @uses \ShipEngine\Util\Assert
21+
*/
22+
final class ShipEngineEventTest extends TestCase
23+
{
24+
/**
25+
* Test the the **ShipEngineEvent** throws an exception if an event that does
26+
* not exist passed into the **emitEvent()** method.
27+
*/
28+
public function testEmitEvent()
29+
{
30+
try {
31+
$config = new ShipEngineConfig(
32+
array(
33+
'apiKey' => 'baz',
34+
'baseUrl' => Endpoints::TEST_RPC_URL,
35+
'pageSize' => 75,
36+
'retries' => 1,
37+
'timeout' => new DateInterval('PT15S')
38+
)
39+
);
40+
ShipEngineEvent::emitEvent(
41+
'pizzaIsReady',
42+
'large pepperoni/sausage with mushrooms',
43+
$config
44+
);
45+
} catch (ShipEngineException $err) {
46+
$this->assertInstanceOf(InvalidFieldValueException::class, $err);
47+
}
48+
}
49+
}

tests/Model/Address/AddressTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public function testMissingcountryCode(): void
264264
* - **message** is "Invalid address. XX is not a valid countryCode code."
265265
* (where XX is the value that was specified).
266266
*/
267-
public function testInvalidcountryCode(): void
267+
public function testInvalidCountryCode(): void
268268
{
269269
try {
270270
new Address(
@@ -303,5 +303,6 @@ public function testJsonSerialize(): void
303303

304304
$this->assertNotNull($json);
305305
$this->assertIsArray($json);
306+
$this->assertJson(json_encode($json));
306307
}
307308
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Model\Carriers;
4+
5+
use ShipEngine\Message\InvalidFieldValueException;
6+
use ShipEngine\Model\Carriers\Carrier;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* Class CarrierTest
11+
*
12+
* @covers \ShipEngine\Model\Carriers\Carrier
13+
* @uses \ShipEngine\Util\Constants\Carriers
14+
* @uses \ShipEngine\Util\Constants\CarrierNames
15+
* @uses \ShipEngine\Message\ShipEngineException
16+
* @uses \ShipEngine\Message\InvalidFieldValueException
17+
*/
18+
final class CarrierTest extends TestCase
19+
{
20+
/**
21+
* Tests the successful instantiation of the **Carrier** object.
22+
*/
23+
public function testInstantiation(): void
24+
{
25+
$carrier = new Carrier('fedex');
26+
$this->assertInstanceOf(Carrier::class, $carrier);
27+
}
28+
29+
/**
30+
* Tests a failed instantiation of the **Carrier** object.
31+
*/
32+
public function testInstantiationExceptionCase(): void
33+
{
34+
try {
35+
new Carrier('pizza');
36+
} catch (InvalidFieldValueException $err) {
37+
$this->assertInstanceOf(InvalidFieldValueException::class, $err);
38+
}
39+
}
40+
41+
/**
42+
* Tests the **jsonSerialize** method on the **Carrier** object.
43+
*/
44+
public function testJsonSerialize(): void
45+
{
46+
$carrier = new Carrier('ups');
47+
$this->assertJson(json_encode($carrier));
48+
}
49+
}

0 commit comments

Comments
 (0)