Browse Source

Add tests

Robert Pustułka 8 years ago
parent
commit
89e9ba25a4

+ 48 - 0
tests/TestCase/Console/CommandRunnerTest.php

@@ -19,10 +19,13 @@ use Cake\Console\CommandRunner;
 use Cake\Console\ConsoleIo;
 use Cake\Console\Shell;
 use Cake\Core\Configure;
+use Cake\Event\EventList;
+use Cake\Event\EventManager;
 use Cake\Http\BaseApplication;
 use Cake\TestSuite\Stub\ConsoleOutput;
 use Cake\TestSuite\TestCase;
 use TestApp\Command\DemoCommand;
+use TestApp\Http\EventApplication;
 use TestApp\Shell\SampleShell;
 
 /**
@@ -50,6 +53,25 @@ class CommandRunnerTest extends TestCase
     }
 
     /**
+     * test set on the app
+     *
+     * @return void
+     */
+    public function testSetApp()
+    {
+        $app = $this->getMockBuilder(BaseApplication::class)
+            ->setConstructorArgs([$this->config])
+            ->getMock();
+
+        $manager = new EventManager();
+        $app->method('getEventManager')
+            ->willReturn($manager);
+
+        $runner = new CommandRunner($app);
+        $this->assertSame($app->getEventManager(), $runner->getEventManager());
+    }
+
+    /**
      * Test that the console hook not returning a command collection
      * raises an error.
      *
@@ -298,6 +320,32 @@ class CommandRunnerTest extends TestCase
     }
 
     /**
+     * Test running a valid command
+     *
+     * @return void
+     */
+    public function testRunEvents()
+    {
+        $app = new EventApplication($this->config);
+        $output = new ConsoleOutput();
+
+        $manager = $app->getEventManager();
+        $manager->setEventList(new EventList());
+
+        $runner = new CommandRunner($app, 'cake');
+        $this->assertSame($manager, $runner->getEventManager());
+
+        $result = $runner->run(['cake', 'ex'], $this->getMockIo($output));
+        $this->assertSame(Shell::CODE_SUCCESS, $result);
+
+        $messages = implode("\n", $output->messages());
+        $this->assertContains('Demo Command!', $messages);
+
+        $this->assertCount(1, $manager->listeners('My.event'));
+        $this->assertEventFired('Console.buildCommands', $manager);
+    }
+
+    /**
      * Test running a command class' help
      *
      * @return void

+ 34 - 2
tests/TestCase/Http/ServerTest.php

@@ -15,10 +15,15 @@
 namespace Cake\Test\TestCase;
 
 use Cake\Event\Event;
+use Cake\Event\EventList;
+use Cake\Event\EventManager;
 use Cake\Http\CallbackStream;
 use Cake\Http\Server;
 use Cake\TestSuite\TestCase;
+use Psr\Http\Message\ResponseInterface;
+use RuntimeException;
 use TestApp\Http\BadResponseApplication;
+use TestApp\Http\EventApplication;
 use TestApp\Http\InvalidMiddlewareApplication;
 use TestApp\Http\MiddlewareApplication;
 use Zend\Diactoros\Response;
@@ -67,8 +72,14 @@ class ServerTest extends TestCase
         $app = $this->getMockBuilder('Cake\Http\BaseApplication')
             ->setConstructorArgs([$this->config])
             ->getMock();
+
+        $manager = new EventManager();
+        $app->method('getEventManager')
+            ->willReturn($manager);
+
         $server = new Server($app);
         $this->assertSame($app, $server->getApp($app));
+        $this->assertSame($app->getEventManager(), $server->getEventManager());
     }
 
     /**
@@ -120,10 +131,11 @@ class ServerTest extends TestCase
     /**
      * Test an application failing to build middleware properly
      *
+     * @return void
      */
     public function testRunWithApplicationNotMakingMiddleware()
     {
-        $this->expectException(\RuntimeException::class);
+        $this->expectException(RuntimeException::class);
         $this->expectExceptionMessage('The application `middleware` method');
         $app = new InvalidMiddlewareApplication($this->config);
         $server = new Server($app);
@@ -147,10 +159,11 @@ class ServerTest extends TestCase
     /**
      * Test middleware not creating a response.
      *
+     * @return void
      */
     public function testRunMiddlewareNoResponse()
     {
-        $this->expectException(\RuntimeException::class);
+        $this->expectException(RuntimeException::class);
         $this->expectExceptionMessage('Application did not create a response. Got "Not a response" instead.');
         $app = new BadResponseApplication($this->config);
         $server = new Server($app);
@@ -158,6 +171,25 @@ class ServerTest extends TestCase
     }
 
     /**
+     * Test application events.
+     *
+     * @return void
+     */
+    public function testRunEvents()
+    {
+        $manager = new EventManager();
+        $manager->setEventList(new EventList());
+        $app = new EventApplication($this->config, $manager);
+
+        $server = new Server($app);
+        $res = $server->run();
+
+        $this->assertCount(1, $manager->listeners('My.event'));
+        $this->assertEventFired('Server.buildMiddleware', $manager);
+        $this->assertInstanceOf(ResponseInterface::class, $res);
+    }
+
+    /**
      * Test that emit invokes the appropriate methods on the emitter.
      *
      * @return void

+ 45 - 0
tests/test_app/TestApp/Http/EventApplication.php

@@ -0,0 +1,45 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Http;
+
+use Cake\Event\EventManagerInterface;
+use Cake\Http\BaseApplication;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use TestApp\Command\DemoCommand;
+
+class EventApplication extends BaseApplication
+{
+    public function events(EventManagerInterface $eventManager)
+    {
+        $eventManager->on('My.event', function () {
+        });
+
+        return $eventManager;
+    }
+
+    public function middleware($middleware)
+    {
+        return $middleware;
+    }
+
+    public function console($commands)
+    {
+        return $commands->addMany(['ex' => DemoCommand::class]);
+    }
+
+    public function __invoke(ServerRequestInterface $req, ResponseInterface $res, $next)
+    {
+        return $res;
+    }
+}