Skip to content

Commit

Permalink
Add missing tests.
Browse files Browse the repository at this point in the history
Fix missing and incorrect DocBlocks.
  • Loading branch information
reinink committed Jan 8, 2015
1 parent 00aa643 commit 44e6e16
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Factories/HttpSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class HttpSignature
{
/**
* Create HttpSignature instance.
* @param string $signKey Secret key used to generate signature.
* @return League\Glide\HttpSignature The HttpSignature instance.
* @param string $signKey Secret key used to generate signature.
* @return \League\Glide\HttpSignature The HttpSignature instance.
*/
public static function create($signKey)
{
Expand Down
12 changes: 7 additions & 5 deletions src/Factories/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ class UrlBuilder
{
/**
* Create UrlBuilder instance.
* @param string $baseUrl URL prefixed to generated URL.
* @param string $signKey Secret key used to secure URLs.
* @return League\Glide\UrlBuilder The UrlBuilder instance.
* @param string $baseUrl URL prefixed to generated URL.
* @param string $signKey Secret key used to secure URLs.
* @return \League\Glide\UrlBuilder The UrlBuilder instance.
*/
public static function create($baseUrl, $signKey = null)
{
$httpSignature = null;

if ($signKey) {
$signKey = HttpSignature::create($signKey);
$httpSignature = HttpSignature::create($signKey);
}

return new \League\Glide\UrlBuilder($baseUrl, $signKey);
return new \League\Glide\UrlBuilder($baseUrl, $httpSignature);
}
}
2 changes: 1 addition & 1 deletion src/HttpSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct($signKey)
}

/**
* Add an HTTP signature to param array.
* Add an HTTP signature to manipulation parameters.
* @param string $path The resource path.
* @param array $params The manipulation parameters.
* @return array The updated manipulation parameters.
Expand Down
12 changes: 12 additions & 0 deletions src/Interfaces/HttpSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

interface HttpSignature
{
/**
* Add an HTTP signature to manipulation parameters.
* @param string $path The resource path.
* @param array $params The manipulation parameters.
* @return array The updated manipulation parameters.
*/
public function addSignature($path, array $params);

/**
* Validate a request signature.
* @param Request $request The request object.
* @throws InvalidSignatureException
*/
public function validateRequest(Request $request);
}
13 changes: 12 additions & 1 deletion src/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace League\Glide;

use InvalidArgumentException;
use League\Glide\Interfaces\HttpSignature as HttpSignatureInterace;

class UrlBuilder
Expand Down Expand Up @@ -39,6 +40,10 @@ public function getUrl($path, array $params = [])
{
$parts = parse_url(trim($this->baseUrl, '/').'/'.trim($path, '/'));

if ($parts === false) {
throw new InvalidArgumentException('Not a valid path.');
}

$parts['path'] = '/'.trim($parts['path'], '/');

if ($this->httpSignature) {
Expand Down Expand Up @@ -66,6 +71,12 @@ private function buildUrl($parts, $params)
}
}

return $url.$parts['path'].'?'.http_build_query($params);
$url .= $parts['path'];

if (count($params)) {
$url .= '?'.http_build_query($params);
}

return $url;
}
}
11 changes: 11 additions & 0 deletions tests/Factories/HttpSignatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace League\Glide\Factories;

class HttpSignatureTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{
$this->assertInstanceOf('League\Glide\HttpSignature', HttpSignature::create('example'));
}
}
9 changes: 9 additions & 0 deletions tests/Factories/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ public function testGetManipulatorsWithMaxImageSize()
$this->assertEquals(2000*2000, $sizeManipulator->getMaxImageSize());
}

public function testGetBaseUrl()
{
$server = new Server([
'base_url' => 'img/',
]);

$this->assertEquals('img/', $server->getBaseUrl());
}

public function testCreate()
{
$server = Server::create([
Expand Down
24 changes: 24 additions & 0 deletions tests/Factories/UrlBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace League\Glide\Factories;

class UrlBuilderTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{
$urlBuilder = UrlBuilder::create('/img');

$this->assertInstanceOf('League\Glide\UrlBuilder', $urlBuilder);
$this->assertEquals('/img/image.jpg', $urlBuilder->getUrl('image.jpg'));
}

public function testCreateWithSignKey()
{
$urlBuilder = UrlBuilder::create('/img', 'example');

$this->assertEquals(
'/img/image.jpg?s=2aed6cf637d60951a66200eda3f5e568',
$urlBuilder->getUrl('image.jpg')
);
}
}
20 changes: 20 additions & 0 deletions tests/UrlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ public function testGetUrl()
);
}

public function testGetUrlWithNoDomain()
{
$urlBuilder = new UrlBuilder();

$this->assertEquals(
'/image.jpg?w=100',
$urlBuilder->getUrl('image.jpg', ['w' => '100'])
);
}

public function testGetUrlWithDomainAndPort()
{
$urlBuilder = new UrlBuilder('http://localhost:8000');

$this->assertEquals(
'http://localhost:8000/image.jpg?w=100',
$urlBuilder->getUrl('image.jpg', ['w' => '100'])
);
}

public function testGetUrlWithToken()
{
$urlBuilder = new UrlBuilder('http://example.com', new HttpSignature('example'));
Expand Down

0 comments on commit 44e6e16

Please sign in to comment.