Skip to content

Commit ad93ab3

Browse files
committed
Completed requirements 1 with unit and integration tests
1 parent 5d9be83 commit ad93ab3

8 files changed

+119
-2
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
},
2323
"autoload-dev": {
2424
"psr-4": {
25-
"ModusCreate\\Test\\": "test/"
25+
"ModusCreate\\Test\\": "test/",
26+
"ModusCreate\\Test\\Integration\\": "test/integration"
2627
}
2728
}
2829
}

docker-compose.test.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "3"
2+
services:
3+
test:
4+
depends_on:
5+
- app
6+
- composer
7+
image: phpunit/phpunit:6.0.6
8+
command: -v -c phpunit-integration.xml
9+
volumes:
10+
- .:/app
File renamed without changes.

integration-test.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
RED='\033[0;31m'
4+
GREEN='\033[0;32m'
5+
NC='\033[0m'
6+
# kill and remove any running containers
7+
cleanup () {
8+
docker-compose -p ci kill
9+
docker-compose -p ci rm -f
10+
}
11+
# catch unexpected failures, do cleanup and output an error message
12+
trap 'cleanup ; printf "${RED}Tests Failed For Unexpected Reasons${NC}\n"'\
13+
HUP INT QUIT PIPE TERM
14+
# build and run the composed services
15+
docker-compose -p ci -f docker-compose.yml -f docker-compose.test.yml up -d
16+
if [ $? -ne 0 ] ; then
17+
printf "${RED}Docker Compose Failed${NC}\n"
18+
exit -1
19+
fi
20+
# wait for the test service to complete and grab the exit code
21+
TEST_EXIT_CODE=`docker wait ci_test_1`
22+
# output the logs for the test (for clarity)
23+
docker logs ci_app_1
24+
docker logs ci_test_1
25+
# inspect the output of the test and display respective message
26+
if [ -z ${TEST_EXIT_CODE+x} ] || [ "$TEST_EXIT_CODE" -ne 0 ] ; then
27+
printf "${RED}Tests Failed${NC} - Exit Code: $TEST_EXIT_CODE\n"
28+
else
29+
printf "${GREEN}Tests Passed${NC}\n"
30+
fi
31+
# call the cleanup fuction
32+
cleanup
33+
# exit the script with the same code as the test service code
34+
exit $TEST_EXIT_CODE

phpunit-integration.xml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.4/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
verbose="true">
7+
<testsuite>
8+
<directory suffix="Test.php">test/integration</directory>
9+
</testsuite>
10+
</phpunit>

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
verbose="true">
77
<testsuite>
88
<directory suffix="Test.php">test</directory>
9+
<exclude>test/integration</exclude>
910
</testsuite>
1011

1112
<filter>

test/fixtures/notFound.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Count":0,"Message":"No results found for this request","Results":[]}
1+
{"Count":0,"Results":[]}

test/integration/HTTPRequestTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
declare (strict_types = 1);
3+
namespace ModusCreate\Test\Integration;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\Psr7\Request;
8+
9+
class HTTPRequestTest extends TestCase
10+
{
11+
/**
12+
* @var Client
13+
*/
14+
private $client;
15+
16+
public function setUp()
17+
{
18+
$this->client = new Client;
19+
parent::setUp();
20+
}
21+
22+
/**
23+
* @dataProvider requestProvider
24+
*
25+
* @param Request $request
26+
* @param string $expected
27+
* @return void
28+
*/
29+
public function testEndpointSucceeds(Request $request, string $expected)
30+
{
31+
$actual = $this->client->send($request);
32+
$this->assertEquals($expected, (string)$actual->getBody());
33+
}
34+
35+
/**
36+
* @return void
37+
*/
38+
public function requestProvider() : array
39+
{
40+
$notFoundResponse = file_get_contents(__DIR__ . '/../fixtures/notFound.json');
41+
42+
return [
43+
[
44+
new Request('GET', 'http://app:8000/vehicles/2015/Audi/A3'),
45+
file_get_contents(__DIR__ . '/../fixtures/2015AudiA3.json')
46+
],
47+
[
48+
new Request('GET', 'http://app:8000/vehicles/2015/Toyota/Yaris'),
49+
file_get_contents(__DIR__ . '/../fixtures/2015ToyotaYaris.json')
50+
],
51+
[
52+
new Request('GET', 'http://app:8000/vehicles/2015/Ford/Crown%20Victoria'),
53+
$notFoundResponse
54+
],
55+
[
56+
new Request('GET', 'http://app:8000/vehicles/undefined/Ford/Fusion'),
57+
$notFoundResponse
58+
]
59+
];
60+
}
61+
}

0 commit comments

Comments
 (0)