From 1563b6fabb17bf767a0cc232daa316a74a9e72a5 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 4 Mar 2017 22:08:13 -0500 Subject: [PATCH] Fix tests failing due to environment pollution. In some environments (my laptop) tests are run in a different order which causes them to fail as we muck about with global state and don't restore it. --- .../Component/AuthComponentTest.php | 2 +- .../Component/RequestHandlerComponentTest.php | 9 ++++++ .../Component/SecurityComponentTest.php | 8 +++++ tests/TestCase/Http/ServerRequestTest.php | 24 +++++++-------- tests/TestCase/Network/ResponseTest.php | 29 +++++++++++++++++++ 5 files changed, 57 insertions(+), 15 deletions(-) diff --git a/tests/TestCase/Controller/Component/AuthComponentTest.php b/tests/TestCase/Controller/Component/AuthComponentTest.php index 22c16595ef9..5b80b2120d9 100644 --- a/tests/TestCase/Controller/Component/AuthComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthComponentTest.php @@ -1191,7 +1191,7 @@ public function testStatelessAuthWorksWithUser() $result = $this->Auth->user('username'); $this->assertEquals('mariano', $result); - $this->assertFalse(isset($_SESSION)); + $this->assertFalse(isset($_SESSION['Auth']), 'No user data in session'); } /** diff --git a/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php b/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php index 2371ff82ac8..786fa91a5c4 100644 --- a/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php +++ b/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php @@ -44,6 +44,13 @@ class RequestHandlerComponentTest extends TestCase */ public $RequestHandler; + /** + * Backup of $_SERVER + * + * @var array + */ + protected $server = []; + /** * setUp method * @@ -52,6 +59,7 @@ class RequestHandlerComponentTest extends TestCase public function setUp() { parent::setUp(); + $this->server = $_SERVER; Configure::write('App.namespace', 'TestApp'); DispatcherFactory::add('Routing'); DispatcherFactory::add('ControllerFactory'); @@ -90,6 +98,7 @@ public function tearDown() DispatcherFactory::clear(); Router::reload(); Router::$initialized = false; + $_SERVER = $this->server; unset($this->RequestHandler, $this->Controller); } diff --git a/tests/TestCase/Controller/Component/SecurityComponentTest.php b/tests/TestCase/Controller/Component/SecurityComponentTest.php index e808160cdad..5952fba5134 100644 --- a/tests/TestCase/Controller/Component/SecurityComponentTest.php +++ b/tests/TestCase/Controller/Component/SecurityComponentTest.php @@ -125,6 +125,12 @@ public function header($status) */ class SecurityComponentTest extends TestCase { + /** + * SERVER variable backup. + * + * @var array + */ + protected $server = []; /** * Controller property @@ -151,6 +157,7 @@ public function setUp() { parent::setUp(); + $this->server = $_SERVER; $session = new Session(); $request = $this->getMockBuilder('Cake\Network\Request') ->setMethods(['here']) @@ -180,6 +187,7 @@ public function setUp() public function tearDown() { parent::tearDown(); + $_SERVER = $this->server; $this->Security->session->delete('_Token'); unset($this->Controller->Security); unset($this->Controller->Component); diff --git a/tests/TestCase/Http/ServerRequestTest.php b/tests/TestCase/Http/ServerRequestTest.php index d469ba4586b..7bc311d5155 100644 --- a/tests/TestCase/Http/ServerRequestTest.php +++ b/tests/TestCase/Http/ServerRequestTest.php @@ -24,10 +24,16 @@ use Zend\Diactoros\Uri; /** - * TestRequest + * ServerRequest Test */ class ServerRequestTest extends TestCase { + /** + * SERVER variable backup. + * + * @var array + */ + protected $server = []; /** * Setup callback @@ -37,26 +43,16 @@ class ServerRequestTest extends TestCase public function setUp() { parent::setUp(); - $this->_case = null; - if (isset($_GET['case'])) { - $this->_case = $_GET['case']; - unset($_GET['case']); - } + $this->server = $_SERVER; Configure::write('App.baseUrl', false); } - /** - * TearDown - * - * @return void - */ public function tearDown() { parent::tearDown(); - if (!empty($this->_case)) { - $_GET['case'] = $this->_case; - } + + $_SERVER = $this->server; } /** diff --git a/tests/TestCase/Network/ResponseTest.php b/tests/TestCase/Network/ResponseTest.php index 62995d0537e..c38c5c13010 100644 --- a/tests/TestCase/Network/ResponseTest.php +++ b/tests/TestCase/Network/ResponseTest.php @@ -27,6 +27,35 @@ */ class ResponseTest extends TestCase { + /** + * SERVER variable backup. + * + * @var array + */ + protected $server = []; + + /** + * setup + * + * @return void + */ + public function setUp() + { + parent::setUp(); + $this->server = $_SERVER; + } + + /** + * teardown + * + * @return void + */ + public function tearDown() + { + parent::tearDown(); + $_SERVER = $this->server; + } + /** * Tests the request object constructor *