Skip to content

Commit 2089b80

Browse files
committed
Added PHPUnit test to prove correctness
1 parent 1d8723d commit 2089b80

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ matrix:
3030
- php: 7.1
3131
- php: 7.2
3232
env: COVERAGE=true TEST_COMMAND="composer test-ci" DEPENDENCIES="henrikbjorn/phpspec-code-coverage:^1.0"
33+
- php: 7.2
34+
env: TEST_COMMAND="./vendor/bin/phpunit" DEPENDENCIES="phpunit/phpunit:^7.5 nyholm/psr7:^1.0"
3335

3436
# Test LTS versions
3537
- php: 7.1

phpunit.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="./vendor/autoload.php"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true">
8+
<php>
9+
<env name="SHELL_VERBOSITY" value="-1" />
10+
</php>
11+
12+
<testsuites>
13+
<testsuite name="HTTPlug unit tests">
14+
<directory suffix="Test.php">./tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>

tests/Plugin/AddPathPluginTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace tests\Http\Client\Common\Plugin;
4+
5+
use Http\Client\Common\Plugin;
6+
use Http\Client\Common\Plugin\AddPathPlugin;
7+
use Nyholm\Psr7\Request;
8+
use Nyholm\Psr7\Uri;
9+
use PHPUnit\Framework\TestCase;
10+
use Psr\Http\Message\RequestInterface;
11+
12+
class AddPathPluginTest extends TestCase
13+
{
14+
/**
15+
* @var Plugin
16+
*/
17+
private $plugin;
18+
19+
/**
20+
* @var callable empty
21+
*/
22+
private $callable;
23+
24+
protected function setUp()
25+
{
26+
$this->callable = function () {};
27+
$this->plugin = new AddPathPlugin(new Uri('/api'));
28+
}
29+
30+
public function testRewriteSameObject()
31+
{
32+
$request = new Request('GET', 'https://example.com/foo');
33+
$this->plugin->handleRequest($request, function (RequestInterface $request) {
34+
$this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString());
35+
}, $this->callable);
36+
37+
// Make a second call with the same $request object
38+
$this->plugin->handleRequest($request, function (RequestInterface $request) {
39+
$this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString());
40+
}, $this->callable);
41+
}
42+
43+
public function testRewriteOnRestart()
44+
{
45+
$request = new Request('GET', 'https://example.com/foo');
46+
$this->plugin->handleRequest($request, function (RequestInterface $request) {
47+
$this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString());
48+
49+
// Run the plugin again with the modified request
50+
$this->plugin->handleRequest($request, function (RequestInterface $request) {
51+
$this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString());
52+
}, $this->callable);
53+
}, $this->callable);
54+
}
55+
56+
public function testRewriteWithDifferentUrl()
57+
{
58+
$request = new Request('GET', 'https://example.com/foo');
59+
$this->plugin->handleRequest($request, function (RequestInterface $request) {
60+
$this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString());
61+
}, $this->callable);
62+
63+
$request = new Request('GET', 'https://example.com/bar');
64+
$this->plugin->handleRequest($request, function (RequestInterface $request) {
65+
$this->assertEquals('https://example.com/api/bar', $request->getUri()->__toString());
66+
}, $this->callable);
67+
}
68+
69+
public function testRewriteWithDifferentUrlWhenSecondUrlIncludesAddedPath()
70+
{
71+
$request = new Request('GET', 'https://example.com/foo');
72+
$this->plugin->handleRequest($request, function (RequestInterface $request) {
73+
$this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString());
74+
}, $this->callable);
75+
76+
$request = new Request('GET', 'https://example.com/api/foo');
77+
$this->plugin->handleRequest($request, function (RequestInterface $request) {
78+
$this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString());
79+
}, $this->callable);
80+
}
81+
}

0 commit comments

Comments
 (0)