Skip to content

Commit

Permalink
Add tests to check GET parameters parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Finesse committed Mar 1, 2020
1 parent 6c3fda6 commit f1e6f1b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Controllers/CSSGeneratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
* ]
* </pre>
* @throws \InvalidArgumentException If the parameter value is formatted badly. The message may be sent back to the
* client.
* client.
*/
protected function parseFamilyParameter(string $value): array
{
Expand Down Expand Up @@ -121,8 +121,8 @@ protected function parseFamilyParameter(string $value): array
*
* @param mixed $fontDisplay Parameter value
* @return string Valid font-display css value, or empty string, if $fontDisplay is null or empty.
* @throws \InvalidArgumentException If the parameter set, but has not valid value. The message may be sent
* back to the client.
* @throws \InvalidArgumentException If the parameter set, but has not valid value. The message may be sent back to
* the client.
*/
protected function parseDisplayParameter($fontDisplay): string
{
Expand Down
74 changes: 64 additions & 10 deletions tests/Functional/CssGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,81 @@ class CssGeneratorTest extends FunctionalTestCase
*/
public function testHeaders()
{
$response = $this->runApp('GET', '/css?family=Open+Sans:400,700');
$response = $this->runApp('GET', '/css?family=Open+Sans');

$this->assertEquals(200, $response->getStatusCode());
$this->assertTrue((bool)preg_match('~^text/css(;|$)~', $response->getHeaderLine('Content-Type')));
$this->assertTrue($response->hasHeader('Cache-Control'));
$this->assertTrue($response->hasHeader('Pragma'));
}

/**
* Test the controller `family` parameter parsing
*/
public function testFamilyParsing()
{
$app = $this->makeApp();
$container = $app->getContainer();
$container['webfontCSSGenerator'] = function () {
$generator = \Mockery::mock(WebfontCSSGenerator::class);
$generator->shouldReceive('makeCSS')
->once()
->with(['Roboto' => ['400']], '')
->andReturn('');
$generator->shouldReceive('makeCSS')
->once()
->with([
'Open Sans' => ['400'],
'Roboto' => ['400', '400i', '700i'],
], '')
->andReturn('');
return $generator;
};

$this->assertEquals(200, $this->runSpecificApp($app, 'GET', '/css?family=Roboto')->getStatusCode());
$this->assertEquals(200, $this->runSpecificApp($app, 'GET', '/css?family=Open+Sans|Roboto:400,400i,700i')->getStatusCode());

$this->assertEquals(200, $this->runApp('GET', '/css?family=Open+Sans')->getStatusCode());
$this->assertEquals(422, $this->runSpecificApp($app, 'GET', '/css?family=')->getStatusCode());
$this->assertEquals(422, $this->runSpecificApp($app, 'GET', '/css?family=|Open+Sans:400,700')->getStatusCode());
$this->assertEquals(422, $this->runSpecificApp($app, 'GET', '/css?family=:400,700')->getStatusCode());
$this->assertEquals(422, $this->runSpecificApp($app, 'GET', '/css')->getStatusCode());
}

/**
* Tests that the route returns an client error status with bad requests
* Test the controller `display` parameter parsing
*/
public function testBadRequests()
public function testDisplayParsing()
{
$this->assertEquals(422, $this->runApp('GET', '/css?family=')->getStatusCode());
$this->assertEquals(422, $this->runApp('GET', '/css?family=|Open+Sans:400,700')->getStatusCode());
$this->assertEquals(422, $this->runApp('GET', '/css?family=:400,700')->getStatusCode());
$this->assertEquals(422, $this->runApp('GET', '/css')->getStatusCode());
$this->assertEquals(422, $this->runApp('GET', '/css?family=Open+Sans:400,700&display[]=bad')->getStatusCode());
$app = $this->makeApp();
$container = $app->getContainer();
$container['webfontCSSGenerator'] = function () {
$generator = \Mockery::mock(WebfontCSSGenerator::class);
$generator->shouldReceive('makeCSS')
->once()
->with(['Open Sans' => ['400']], '')
->andReturn('');
$generator->shouldReceive('makeCSS')
->once()
->with(['Open Sans' => ['400']], '')
->andReturn('');
$generator->shouldReceive('makeCSS')
->once()
->with(['Open Sans' => ['400']], 'swap')
->andReturn('');
return $generator;
};

$this->assertEquals(200, $this->runSpecificApp($app, 'GET', '/css?family=Open+Sans')->getStatusCode());
$this->assertEquals(200, $this->runSpecificApp($app, 'GET', '/css?family=Open+Sans&display=')->getStatusCode());
$this->assertEquals(200, $this->runSpecificApp($app, 'GET', '/css?family=Open+Sans&display=swap')->getStatusCode());
$this->assertEquals(422, $this->runSpecificApp($app, 'GET', '/css?family=Open+Sans&display[]=bad')->getStatusCode());
}

/**
* Test handling errors from webfontCSSGenerator
*/
public function testGeneratorInputError()
{
$app = $this->makeApp();
$container = $app->getContainer();
$container['webfontCSSGenerator'] = function () {
Expand All @@ -42,7 +96,7 @@ public function testBadRequests()
return $generator;
};

$this->assertEquals(422, $this->runSpecificApp($app, 'GET', '/css?family=Open+Sans:400,700')->getStatusCode());
$this->assertEquals(422, $this->runSpecificApp($app, 'GET', '/css?family=Open+Sans')->getStatusCode());
}

/**
Expand Down

0 comments on commit f1e6f1b

Please sign in to comment.