Browse Source

Always use a fresh EventManager for tests

Robbert Noordzij 10 years ago
parent
commit
dfab29b126
3 changed files with 29 additions and 12 deletions
  1. 0 12
      src/TestSuite/IntegrationTestCase.php
  2. 3 0
      src/TestSuite/TestCase.php
  3. 26 0
      tests/TestCase/BasicsTest.php

+ 0 - 12
src/TestSuite/IntegrationTestCase.php

@@ -14,7 +14,6 @@
 namespace Cake\TestSuite;
 
 use Cake\Core\Configure;
-use Cake\Event\EventManager;
 use Cake\Network\Request;
 use Cake\Network\Session;
 use Cake\Routing\DispatcherFactory;
@@ -100,17 +99,6 @@ abstract class IntegrationTestCase extends TestCase
     protected $_requestSession;
 
     /**
-     * Resets the EventManager for before each test.
-     *
-     * @return void
-     */
-    public function setUp()
-    {
-        parent::setUp();
-        EventManager::instance(new EventManager());
-    }
-
-    /**
      * Clears the state used for requests.
      *
      * @return void

+ 3 - 0
src/TestSuite/TestCase.php

@@ -16,6 +16,7 @@ namespace Cake\TestSuite;
 use Cake\Core\App;
 use Cake\Core\Configure;
 use Cake\Datasource\ConnectionManager;
+use Cake\Event\EventManager;
 use Cake\ORM\Exception\MissingTableClassException;
 use Cake\ORM\TableRegistry;
 use Cake\Routing\Router;
@@ -100,6 +101,8 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
         if (class_exists('Cake\Routing\Router', false)) {
             Router::reload();
         }
+ 
+        EventManager::instance(new EventManager());
     }
 
     /**

+ 26 - 0
tests/TestCase/BasicsTest.php

@@ -19,6 +19,7 @@ namespace Cake\Test\TestCase;
 use Cake\Cache\Cache;
 use Cake\Core\App;
 use Cake\Core\Configure;
+use Cake\Event\EventManager;
 use Cake\Filesystem\Folder;
 use Cake\Log\Log;
 use Cake\Network\Response;
@@ -560,4 +561,29 @@ EXPECTED;
         $this->assertInstanceOf('Cake\Collection\Collection', $collection);
         $this->assertSame($items, $collection->toArray());
     }
+
+    /**
+     * Test that works in tandem with testEventManagerReset2 to
+     * test the EventManager reset.
+     *
+     * The return value is passed to testEventManagerReset2 as
+     * an arguments.
+     *
+     * @return \Cake\Event\EventManager
+     */
+    public function testEventManagerReset1()
+    {
+        return EventManager::instance();
+    }
+
+    /**
+     * Test if the EventManager is reset between tests.
+     *
+     * @depends testEventManagerReset1
+     * @return void
+     */
+    public function testEventManagerReset2($prevEventManager)
+    {
+        $this->assertNotSame($prevEventManager, EventManager::instance());
+    }
 }