Skip to content

Commit

Permalink
Made a test css route
Browse files Browse the repository at this point in the history
  • Loading branch information
Finesse committed Jul 29, 2017
1 parent b235a56 commit 26bd1e3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 52 deletions.
10 changes: 5 additions & 5 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

/*
* Routes
*
* Docs: https://www.slimframework.com/docs/objects/router.html
*/

// CSS generator
$app->get('/css', function (RequestInterface $request, ResponseInterface $response) {

})->setName('cssGenerator');
$app->get('/css', \Src\Controllers\CSSGeneratorController::class)->setName('cssGenerator');

// Index page
$app->get('/', function (RequestInterface $request, ResponseInterface $response) {
$app->get('/', function (RequestInterface $request, ResponseInterface $response) use ($app) {
return $this->renderer->render($response, 'index.phtml', [
'cssApiUrl' => $app->get('router')
'cssApiUrl' => $app->getContainer()->get('router')->pathFor('cssGenerator')
]);
});
48 changes: 48 additions & 0 deletions src/Controllers/CSSGeneratorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Src\Controllers;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Class CSSGeneratorController
*
* The controller for generating webfonts CSS files.
*
* @author Finesse
* @package Src\Controllers
*/
class CSSGeneratorController
{
/**
* @var ContainerInterface Dependencies container
*/
protected $container;

/**
* @param ContainerInterface $container Dependencies container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

/**
* Runs the controller action.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function __invoke(RequestInterface $request, ResponseInterface $response): ResponseInterface
{
$response = $response->withHeader('Content-Type', 'text/css; charset=UTF-8');

$body = $response->getBody();
$body->write('body {color: #555;}');

return $response;
}
}
33 changes: 3 additions & 30 deletions templates/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,10 @@
<html>
<head>
<meta charset="utf-8"/>
<title>Slim 3</title>
<link href='//fonts.googleapis.com/css?family=Lato:300' rel='stylesheet' type='text/css'>
<style>
body {
margin: 50px 0 0 0;
padding: 0;
width: 100%;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-align: center;
color: #aaa;
font-size: 18px;
}

h1 {
color: #719e40;
letter-spacing: -3px;
font-family: 'Lato', sans-serif;
font-size: 100px;
font-weight: 200;
margin-bottom: 0;
}
</style>
<title>Web fonts repository</title>
</head>
<body>
<h1>Slim</h1>
<div>a microframework for PHP</div>

<?php if (isset($name)) : ?>
<h2>Hello <?= htmlspecialchars($name); ?>!</h2>
<?php else: ?>
<p>Try <a href="http://www.slimframework.com">SlimFramework</a>
<?php endif; ?>
<h1>Web fonts repository</h1>
<p>Use the URL <a href="<?= htmlspecialchars($cssApiUrl) ?>"><?= htmlspecialchars($cssApiUrl) ?></a> to get a font CSS.</p>
</body>
</html>
17 changes: 17 additions & 0 deletions tests/Functional/CssGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Functional;

class CssGeneratorTest extends BaseTestCase
{
/**
* Test that the route returns a CSS file response
*/
public function testStatusAndContentType()
{
$response = $this->runApp('GET', '/css');

$this->assertEquals(200, $response->getStatusCode());
$this->assertTrue((bool)preg_match('~^text/css(;|$)~', $response->getHeaderLine('Content-Type')));
}
}
22 changes: 5 additions & 17 deletions tests/Functional/HomepageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,24 @@
class HomepageTest extends BaseTestCase
{
/**
* Test that the index route returns a rendered response containing the text 'SlimFramework' but not a greeting
* Test that the index route returns a rendered response
*/
public function testGetHomepageWithoutName()
public function testGetHomepage()
{
$response = $this->runApp('GET', '/');

$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('SlimFramework', (string)$response->getBody());
$this->assertNotContains('Hello', (string)$response->getBody());
}

/**
* Test that the index route with optional name argument returns a rendered greeting
*/
public function testGetHomepageWithGreeting()
{
$response = $this->runApp('GET', '/name');

$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('Hello name!', (string)$response->getBody());
$this->assertContains('Web fonts repository', (string)$response->getBody());
}

/**
* Test that the index route won't accept a post request
*/
public function testPostHomepageNotAllowed()
{
$response = $this->runApp('POST', '/', ['test']);
$response = $this->runApp('POST', '/');

$this->assertEquals(405, $response->getStatusCode());
$this->assertContains('Method not allowed', (string)$response->getBody());
}
}
}

0 comments on commit 26bd1e3

Please sign in to comment.