Permalink
Browse files

100% Code Coverage

  • Loading branch information...
1 parent da59441 commit d724115c5608e2c588abef808ed00be1a7def64b @dongilbert dongilbert committed Nov 1, 2013
Showing with 180 additions and 1 deletion.
  1. +16 −0 phpunit.xml.dist
  2. +1 −1 src/League/StackRobots/Robots.php
  3. +163 −0 tests/League/StackRobots/RobotsTest.php
View
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit bootstrap="vendor/autoload.php">
+ <testsuites>
+ <testsuite name="League\StackRobots Test Suite">
+ <directory>tests/League/StackRobots</directory>
+ </testsuite>
+ </testsuites>
+ <logging>
+ <log type="coverage-text" target="php://stdout" />
+ </logging>
+ <filter>
+ <whitelist addUncoveredFilesFromWhitelist="true">
+ <directory suffix=".php">src/</directory>
+ </whitelist>
+ </filter>
+</phpunit>
@@ -30,7 +30,7 @@ class Robots implements HttpKernelInterface
* @param string $env The environment variable to compare against.
* @param string $envVar The environment variable to inspect.
*/
- public function __construct(HttpKernelInterface $app, $env, $envVar = 'SERVER_ENV')
+ public function __construct(HttpKernelInterface $app, $env = 'production', $envVar = 'SERVER_ENV')
{
$this->app = $app;
$this->env = $env;
@@ -0,0 +1,163 @@
+<?php
+
+namespace League\StackRobots;
+
+use Symfony\Component\HttpFoundation\Response;
+
+putenv('SERVER_ENV=production');
+
+class RobotsTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * The object to be tested.
+ *
+ * @var Robots
+ */
+ protected $fixture;
+
+ /**
+ * Setup the tests.
+ *
+ * @return void
+ */
+ public function setUp()
+ {
+ $mockApp = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
+
+ $this->fixture = new Robots($mockApp);
+ }
+
+ /**
+ * The app property should be an instanceof HttpKernelInterface
+ *
+ * @return void
+ */
+ public function testConstructPassesApp()
+ {
+ $this->assertAttributeInstanceOf(
+ 'Symfony\\Component\\HttpKernel\\HttpKernelInterface',
+ 'app',
+ $this->fixture,
+ 'The app property should be an instanceof HttpKernelInterface'
+ );
+ }
+
+ /**
+ * The env property should be set to the value set in the constructor.
+ *
+ * @return void
+ */
+ public function testConstructDefaultEnv()
+ {
+ $this->assertAttributeEquals(
+ 'production',
+ 'env',
+ $this->fixture,
+ 'The env property should be set to the value set in the constructor.'
+ );
+ }
+
+ /**
+ * The envVar property should be set to the value set in the constructor.
+ *
+ * @return void
+ */
+ public function testConstructDefaultEnvVar()
+ {
+ $this->assertAttributeEquals(
+ 'SERVER_ENV',
+ 'envVar',
+ $this->fixture,
+ 'The envVar property should be set to the value set in the constructor.'
+ );
+ }
+
+ /**
+ * The env property should be set to the value passed to the constructor.
+ *
+ * @return void
+ */
+ public function testConstructPassesEnv()
+ {
+ $mockApp = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
+
+ $this->fixture = new Robots($mockApp, 'testing');
+
+ $this->assertAttributeEquals(
+ 'testing',
+ 'env',
+ $this->fixture,
+ 'The env property should be set to the value passed to the constructor.'
+ );
+ }
+
+ /**
+ * The envVar property should be set to the value passed to the constructor.
+ *
+ * @return void
+ */
+ public function testConstructPassesEnvVar()
+ {
+ $mockApp = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
+
+ $this->fixture = new Robots($mockApp, 'testing', 'MY_ENVVAR');
+
+ $this->assertAttributeEquals(
+ 'MY_ENVVAR',
+ 'envVar',
+ $this->fixture,
+ 'The envVar property should be set to the value passed to the constructor.'
+ );
+ }
+
+ /**
+ * Test a request for robots.txt sends the proper response.
+ *
+ * @return void
+ */
+ public function testHandleSendsResponse()
+ {
+ $mockApp = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
+ $mockRequest = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getPathInfo'));
+
+ $mockRequest->expects($this->once())
+ ->method('getPathInfo')
+ ->will($this->returnValue('/robots.txt'));
+
+ $this->fixture = new Robots($mockApp, 'testing');
+
+ $response = $this->fixture->handle($mockRequest);
+
+ $expectedResponse = new Response("User-Agent: *\nDisallow: /", 200, array('Content-Type' => 'text/plain'));
+
+ $this->assertEquals(
+ $expectedResponse,
+ $response,
+ 'When requesting the robots.txt file, the proper response should be received.'
+ );
+ }
+
+ /**
+ * Test a typical handling of a request.
+ *
+ * @return void
+ */
+ public function testHandle()
+ {
+ $mockApp = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface', array('handle'));
+ $mockRequest = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getPathInfo'));
+
+
+ $mockApp->expects($this->once())
+ ->method('handle')
+ ->will($this->returnValue('foo'));
+
+ $this->fixture = new Robots($mockApp, 'testing');
+
+ $this->assertEquals(
+ 'foo',
+ $this->fixture->handle($mockRequest),
+ 'When in the production environment, the handler should be passed on to the next middleware.'
+ );
+ }
+}

0 comments on commit d724115

Please sign in to comment.