Skip to content

Commit

Permalink
Add somes tests #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Robatel committed Jun 30, 2015
1 parent 70c4e62 commit 8fe56a9
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 7 deletions.
50 changes: 50 additions & 0 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Created by PhpStorm.
* User: Kevin
* Date: 30.06.2015
* Time: 11:58
*/

namespace Kevinrob\GuzzleCache;


use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;

class BaseTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Client
*/
protected $client;

public function setUp()
{
// Create default HandlerStack
$stack = HandlerStack::create(function(RequestInterface $request, array $options) {
return new FulfilledPromise(
(new Response())
->withBody(\GuzzleHttp\Psr7\stream_for('Hello world!'))
);
});

// Add this middleware to the top with `push`
$stack->push(CacheMiddleware::getMiddleware(), 'cache');

// Initialize the client with the handler option
$this->client = new Client(['handler' => $stack]);
}

public function testNoBreakClient()
{
$response = $this->client->get("anything");

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('Hello world!', $response->getBody());
}

}
71 changes: 71 additions & 0 deletions tests/HeaderCacheControlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
namespace Kevinrob\GuzzleCache;

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;

/**
* Created by IntelliJ IDEA.
* User: Kevin
* Date: 30.06.2015
* Time: 12:58
*/
class HeaderCacheControlTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Client
*/
protected $client;

public function setUp()
{
// Create default HandlerStack
$stack = HandlerStack::create(function(RequestInterface $request, array $options) {
switch ($request->getUri()->getPath()) {
case '/2s':
return new FulfilledPromise(
(new Response())
->withAddedHeader("Cache-Control", "max-age=2")
);
case '/no-store':
return new FulfilledPromise(
(new Response())
->withAddedHeader("Cache-Control", "no-store")
);
}

throw new \InvalidArgumentException();
});

// Add this middleware to the top with `push`
$stack->push(CacheMiddleware::getMiddleware(), 'cache');

// Initialize the client with the handler option
$this->client = new Client(['handler' => $stack]);
}

public function testMaxAgeHeader()
{
$this->client->get("http://test.com/2s");

$response = $this->client->get("http://test.com/2s");
$this->assertEquals("HIT", $response->getHeaderLine("X-Cache"));

sleep(3);

$response = $this->client->get("http://test.com/2s");
$this->assertEquals("", $response->getHeaderLine("X-Cache"));
}

public function testNoStoreHeader()
{
$this->client->get("http://test.com/no-store");

$response = $this->client->get("http://test.com/no-store");
$this->assertEquals("", $response->getHeaderLine("X-Cache"));
}

}
37 changes: 30 additions & 7 deletions tests/HeaderExpireTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ public function setUp()
{
// Create default HandlerStack
$stack = HandlerStack::create(function(RequestInterface $request, array $options) {
return new FulfilledPromise(
(new Response())
->withHeader("Expires", gmdate('D, d M Y H:i:s T', time() + 2))
);
switch ($request->getUri()->getPath()) {
case '/expired':
return new FulfilledPromise(
(new Response())
->withHeader("Expires", gmdate('D, d M Y H:i:s T', time() - 10))
);
case '/2s':
return new FulfilledPromise(
(new Response())
->withHeader("Expires", gmdate('D, d M Y H:i:s T', time() + 2))
);
}

throw new \InvalidArgumentException();
});

// Add this middleware to the top with `push`
Expand All @@ -37,11 +47,24 @@ public function setUp()
$this->client = new Client(['handler' => $stack]);
}

public function testMockedResponse()
public function testAlreadyExpiredHeader()
{
$this->client->get("http://test.com/expired");
$response = $this->client->get("http://test.com/expired");
$this->assertEquals("", $response->getHeaderLine("X-Cache"));
}

public function testExpiredHeader()
{
$response = $this->client->get("anything");
$this->client->get("http://test.com/2s");

$response = $this->client->get("http://test.com/2s");
$this->assertEquals("HIT", $response->getHeaderLine("X-Cache"));

sleep(3);

$this->assertEquals(200, $response->getStatusCode());
$response = $this->client->get("http://test.com/2s");
$this->assertEquals("", $response->getHeaderLine("X-Cache"));
}

}
84 changes: 84 additions & 0 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
namespace Kevinrob\GuzzleCache;

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;

/**
* Created by IntelliJ IDEA.
* User: Kevin
* Date: 30.06.2015
* Time: 12:58
*/
class ValidationTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Client
*/
protected $client;

public function setUp()
{
// Create default HandlerStack
$stack = HandlerStack::create(function(RequestInterface $request, array $options) {
switch ($request->getUri()->getPath()) {
case '/etag':
if ($request->getHeaderLine("If-None-Match") == 'MyBeautifulHash') {
return new FulfilledPromise(new Response(304));
}

return new FulfilledPromise(
(new Response())
->withHeader("Etag", 'MyBeautifulHash')
->withAddedHeader("Cache-Control", 'max-age=0')
);
case '/etag-changed':
if ($request->getHeaderLine("If-None-Match") == 'MyBeautifulHash') {
return new FulfilledPromise(
(new Response())
->withHeader("Etag", 'MyBeautifulHash2')
->withAddedHeader("Cache-Control", 'max-age=0')
);
}

return new FulfilledPromise(
(new Response())
->withHeader("Etag", 'MyBeautifulHash')
->withAddedHeader("Cache-Control", 'max-age=0')
);
}

throw new \InvalidArgumentException();
});

// Add this middleware to the top with `push`
$stack->push(CacheMiddleware::getMiddleware(), 'cache');

// Initialize the client with the handler option
$this->client = new Client(['handler' => $stack]);
}

public function testEtagHeader()
{
$this->client->get("http://test.com/etag");

sleep(1);

$response = $this->client->get("http://test.com/etag");
$this->assertEquals("HIT with validation", $response->getHeaderLine("X-Cache"));
}

public function testEtagChangeHeader()
{
$this->client->get("http://test.com/etag-changed");

sleep(1);

$response = $this->client->get("http://test.com/etag-changed");
$this->assertEquals("", $response->getHeaderLine("X-Cache"));
}

}

0 comments on commit 8fe56a9

Please sign in to comment.