Skip to content

Commit 441352a

Browse files
authored
🔧 Driver unit tests (1 / 3) (LaraCrafts#29)
1 parent a038d42 commit 441352a

12 files changed

+308
-63
lines changed

‎composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
"php": "^7.1",
2828
"guzzlehttp/guzzle": "^6.2",
2929
"guzzlehttp/promises": "^1.0",
30-
"guzzlehttp/psr7": "^1.0",
30+
"guzzlehttp/psr7": "^1.4",
3131
"illuminate/support": "^5.1,<5.9",
3232
"psr/http-message": "^1.0"
3333
},
3434
"require-dev": {
3535
"orchestra/testbench": "^3.1",
36-
"phpunit/phpunit": "^5.0 || ^6.0 || ^7.0 || ^8.0"
36+
"phpunit/phpunit": "^5.2 || ^6.0 || ^7.0 || ^8.0"
3737
},
3838
"autoload": {
3939
"psr-4": {
@@ -48,6 +48,9 @@
4848
"config": {
4949
"sort-packages": true
5050
},
51+
"scripts": {
52+
"tests": "./vendor/bin/phpunit --colors=always"
53+
},
5154
"extra": {
5255
"laravel": {
5356
"aliases": {

‎tests/Concerns/HasUrlAssertions.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ public static function assertValidUrl($actual)
3333
/**
3434
* Create a IsValidUrl instance
3535
*
36-
* @return IsValidUrl
36+
* @return \LaraCrafts\UrlShortener\Tests\Constraints\IsValidUrl
3737
*/
38-
public static function isValidUrl(): IsValidUrl
38+
public static function isValidUrl()
3939
{
4040
return new IsValidUrl;
4141
}
4242

4343
/**
4444
* Create a RedirectsTo instance
4545
*
46-
* @param $expected
46+
* @param \Psr\Http\Message\UriInterface|string $expected
4747
* @param int $redirects
48-
* @return RedirectsTo
48+
* @return \LaraCrafts\UrlShortener\Tests\Constraints\RedirectsTo
4949
*/
50-
public static function redirectsTo($expected, int $redirects = 1): RedirectsTo
50+
public static function redirectsTo($expected, int $redirects = 1)
5151
{
5252
return new RedirectsTo($expected, $redirects);
5353
}

‎tests/Constraints/RedirectsTo.php

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class RedirectsTo extends Constraint
2121
*/
2222
public function __construct($destination, int $redirects = 1)
2323
{
24+
parent::__construct();
25+
2426
$this->client = new Client();
2527
$this->destination = rtrim($destination, '/');
2628
$this->redirects = $redirects;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use function GuzzleHttp\Psr7\parse_response;
4+
5+
return parse_response(trim('
6+
HTTP/2 200
7+
date: Sat, 01 Jun 2019 06:40:07 GMT
8+
content-type: text/html; charset=UTF-8
9+
set-cookie: __cfduid=d0cf4961cf498445e99cd9a4d9564f3291559371207; expires=Sun, 31-May-20 06:40:07 GMT; path=/; domain=.is.gd; HttpOnly; Secure
10+
expect-ct: max-age=604800, report-uri=\'https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\'
11+
server: cloudflare
12+
cf-ray: 4dff31bc2b35d8f5-AMS
13+
14+
https://is.gd/jAxBiv
15+
'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use function GuzzleHttp\Psr7\parse_response;
4+
5+
return parse_response(trim('
6+
HTTP/2 400
7+
date: Sat, 01 Jun 2019 07:06:24 GMT
8+
content-type: text/html; charset=UTF-8
9+
set-cookie: __cfduid=d8ddac9f1ceba2876dd2407d383fae8af1559372784; expires=Sun, 31-May-20 07:06:24 GMT; path=/; domain=.is.gd; HttpOnly; Secure
10+
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
11+
server: cloudflare
12+
cf-ray: 4dff583d2f3fc76d-AMS
13+
14+
Error: Please specify a URL to shorten.
15+
'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use function GuzzleHttp\Psr7\parse_response;
4+
5+
return parse_response(trim('
6+
HTTP/1.1 200 OK
7+
Date: Fri, 07 Jun 2019 20:19:33 GMT
8+
Content-Type: application/json
9+
Transfer-Encoding: chunked
10+
Connection: keep-alive
11+
Set-Cookie: __cfduid=d74a3fd364bebf9c3a34a19897547d70b1559938773; expires=Sat, 06-Jun-20 20:19:33 GMT; path=/; domain=.polr.me; HttpOnly
12+
X-Powered-By: PHP/7.1.6
13+
Cache-Control: no-cache
14+
Access-Control-Allow-Origin: *
15+
Set-Cookie: laravel_session=eyJpdiI6Impkb2NvdFozZHlzMVlEMU85TmVmblE9PSIsInZhbHVlIjoiMVRpUWZ6dVBjbW1JYlIwazJ1YXY5OHlKZ0pBVFwvRGY2VVBxUmtTMnNVeHVcL1VEbXdybEgrWllndnFRMis1c3dVVEY4blk0cWVLUFFOXC92eFROOHZrWmc9PSIsIm1hYyI6IjE0ZTc0ZDUxZThjNDg3Mzk0YzhiYWQzNjA4MzFmZDc0N2ExNDBjZWQzNTI2MGI1MjIwZmUxNzYzMTllZjNjM2UifQ%3D%3D; expires=Fri, 07-Jun-2019 22:19:33 GMT; Max-Age=7200; path=/; HttpOnly
16+
Server: cloudflare
17+
CF-RAY: 4e355253fbd9d8c9-AMS
18+
19+
{"action":"shorten","result":"http:\/\/demo.polr.me\/0"}
20+
'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use function GuzzleHttp\Psr7\parse_response;
4+
5+
return parse_response(trim('
6+
HTTP/1.1 400 Bad Request
7+
Date: Fri, 07 Jun 2019 20:09:35 GMT
8+
Content-Type: text/plain;charset=UTF-8
9+
Transfer-Encoding: chunked
10+
Connection: keep-alive
11+
Set-Cookie: __cfduid=d1b584c6da86f8ac69e8adf86f419452e1559938174; expires=Sat, 06-Jun-20 20:09:34 GMT; path=/; domain=.polr.me; HttpOnly
12+
X-Powered-By: PHP/7.1.6
13+
Cache-Control: no-cache
14+
Access-Control-Allow-Origin: *
15+
Server: cloudflare
16+
CF-RAY: 4e3543b93c08d915-AMS
17+
18+
400 Invalid or missing parameters.
19+
'));

‎tests/Unit/Http/HttpTestCase.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace LaraCrafts\UrlShortener\Tests\Unit\Http;
4+
5+
use LaraCrafts\UrlShortener\Tests\Concerns\HasUrlAssertions;
6+
use PHPUnit\Framework\TestCase;
7+
8+
abstract class HttpTestCase extends TestCase
9+
{
10+
use HasUrlAssertions;
11+
12+
/**
13+
* @var \LaraCrafts\UrlShortener\Tests\Unit\Http\MockClient
14+
*/
15+
protected $client;
16+
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
protected function setUp(): void
21+
{
22+
$this->client = new MockClient();
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
protected function tearDown(): void
29+
{
30+
if ($this->client->hasQueuedMessages()) {
31+
$this->fail(sprintf('HTTP client contains %d unused message(s)', $this->client->getQueueSize()));
32+
}
33+
}
34+
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace LaraCrafts\UrlShortener\Tests\Unit\Http;
4+
5+
use GuzzleHttp\Exception\ClientException;
6+
use LaraCrafts\UrlShortener\Http\IsGdShortener;
7+
8+
class IsGdShortenerTest extends HttpTestCase
9+
{
10+
/**
11+
* @var \LaraCrafts\UrlShortener\Http\IsGdShortener
12+
*/
13+
protected $shortener;
14+
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->shortener = new IsGdShortener($this->client, 'https://is.gd', false);
23+
}
24+
25+
/**
26+
* Test the shortening of URLs through is.gd
27+
*
28+
* @return void
29+
*/
30+
public function testShortening()
31+
{
32+
$this->client->queue(require __DIR__ . '/../../Fixtures/is.gd/shorten.http-200.php');
33+
34+
$shortenedUrl = $this->shortener->shorten('https://google.com');
35+
$request = $this->client->getRequest(0);
36+
37+
$this->assertNotNull($request);
38+
$this->assertEquals('GET', $request->getMethod());
39+
$this->assertEquals('is.gd', $request->getUri()->getHost());
40+
41+
$this->assertEquals(
42+
'/create.php?format=simple&logstats=0&url=https%3A%2F%2Fgoogle.com',
43+
$request->getRequestTarget()
44+
);
45+
46+
$this->assertEquals('https://is.gd/jAxBiv', $shortenedUrl);
47+
}
48+
49+
/**
50+
* Test failure of shortening through is.gd.
51+
*
52+
* @return void
53+
* @depends testShortening
54+
*/
55+
public function testFailure()
56+
{
57+
$this->client->queue(require __DIR__ . '/../../Fixtures/is.gd/shorten.http-400.php');
58+
59+
$this->expectException(ClientException::class);
60+
$this->shortener->shorten('https://google.com');
61+
}
62+
}

‎tests/Unit/Http/MockClient.php

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace LaraCrafts\UrlShortener\Tests\Unit\Http;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\HandlerStack;
8+
use GuzzleHttp\Middleware;
9+
use Psr\Http\Message\ResponseInterface;
10+
use function GuzzleHttp\choose_handler;
11+
12+
class MockClient extends Client
13+
{
14+
protected $handler;
15+
protected $history;
16+
17+
/**
18+
* Create a new test client.
19+
*
20+
* @param array $config
21+
* @return void
22+
*/
23+
public function __construct(array $config = [])
24+
{
25+
$this->handler = new MockHandler();
26+
$this->history = [];
27+
28+
parent::__construct($config + ['handler' => $this->newHandlerStack($this->handler)]);
29+
}
30+
31+
/**
32+
* Get the client history.
33+
*
34+
* @param int|null $at
35+
* @return mixed
36+
*/
37+
public function getHistory(int $at = null)
38+
{
39+
if (is_null($at)) {
40+
return $this->history;
41+
}
42+
43+
return $this->history[$at] ?? null;
44+
}
45+
46+
/**
47+
* Get a previously made request.
48+
*
49+
* @param int $at
50+
* @return \Psr\Http\Message\RequestInterface|null
51+
*/
52+
public function getRequest(int $at)
53+
{
54+
return $this->getHistory($at)['request'] ?? null;
55+
}
56+
57+
/**
58+
* Get the amount of messages waiting in the queue.
59+
*
60+
* @return int
61+
*/
62+
public function getQueueSize()
63+
{
64+
return $this->handler->count();
65+
}
66+
67+
/**
68+
* Determine if there are queued messages.
69+
*
70+
* @return bool
71+
*/
72+
public function hasQueuedMessages()
73+
{
74+
return $this->getQueueSize() > 0;
75+
}
76+
77+
/**
78+
* Get a fresh handler stack.
79+
*
80+
* @param callable|null $handler
81+
* @return \GuzzleHttp\HandlerStack
82+
*/
83+
protected function newHandlerStack($handler = null)
84+
{
85+
$stack = new HandlerStack($handler ?: choose_handler());
86+
$stack->push(Middleware::history($this->history));
87+
$stack->push(Middleware::httpErrors());
88+
89+
return $stack;
90+
}
91+
92+
/**
93+
* Queue the given response.
94+
*
95+
* @param \Psr\Http\Message\ResponseInterface ...$responses
96+
* @return $this
97+
*/
98+
public function queue(ResponseInterface ...$responses)
99+
{
100+
$this->handler->append(...$responses);
101+
102+
return $this;
103+
}
104+
}

0 commit comments

Comments
 (0)