Skip to content

Commit 78a09a1

Browse files
committed
Fix up tests
1 parent d4a049c commit 78a09a1

12 files changed

Lines changed: 72 additions & 80 deletions

File tree

system/Config/BaseConfig.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class BaseConfig
5454
*
5555
* @var array
5656
*/
57-
protected static $registrars = [];
57+
public static $registrars = [];
5858

5959
protected static $didDiscovery = false;
6060

@@ -126,7 +126,10 @@ public function __construct()
126126
}
127127
}
128128

129-
$this->registerProperties();
129+
if (ENVIRONMENT != 'testing')
130+
{
131+
$this->registerProperties();
132+
}
130133
}
131134

132135
//--------------------------------------------------------------------

system/Config/Config.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ public static function get(string $name, bool $getShared = true)
8484

8585
//--------------------------------------------------------------------
8686

87+
/**
88+
* Helper method for injecting mock instances while testing.
89+
*
90+
* @param string $class
91+
* @param $instance
92+
*/
93+
public static function injectMock(string $class, $instance)
94+
{
95+
self::$instances[$class] = $instance;
96+
}
97+
98+
//--------------------------------------------------------------------
99+
87100
/**
88101
* Find configuration class and create instance
89102
*

system/Events/Events.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ class Events
7777
*/
7878
protected static $performanceLog = [];
7979

80+
/**
81+
* A list of found files.
82+
* @var array
83+
*/
84+
protected static $files = [];
85+
8086
//--------------------------------------------------------------------
8187

8288
/**
@@ -94,16 +100,17 @@ public static function initialize(string $file = null)
94100

95101
$config = config('Modules');
96102

97-
if (! $config->shouldDiscover('events'))
103+
$files = [APPPATH.'Config/Events.php'];
104+
105+
if ($config->shouldDiscover('events'))
98106
{
99-
return;
107+
$locator = Services::locator();
108+
$files = $locator->search('Config/Events.php');
100109
}
101110

102-
$locator = Services::locator();
111+
static::$files = $files;
103112

104-
$files = $locator->search('Config/Events.php');
105-
106-
foreach ($files as $file)
113+
foreach (static::$files as $file)
107114
{
108115
if (! file_exists($file))
109116
{
@@ -292,11 +299,23 @@ public static function removeAllListeners($event_name = null)
292299
/**
293300
* Sets the path to the file that routes are read from.
294301
*
295-
* @param string $path
302+
* @param array $files
303+
*/
304+
public static function setFiles(array $files)
305+
{
306+
static::$files = $files;
307+
}
308+
309+
//--------------------------------------------------------------------
310+
311+
/**
312+
* Returns the files that were found/loaded during this request.
313+
*
314+
* @return mixed
296315
*/
297-
public static function setFile(string $path)
316+
public function getFiles()
298317
{
299-
self::$eventsFile = $path;
318+
return static::$files;
300319
}
301320

302321
//--------------------------------------------------------------------

tests/_support/Config/WorseRegistrar.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/system/CodeIgniterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testRun404OverrideByClosure()
125125
$_SERVER['argc'] = 2;
126126

127127
// Inject mock router.
128-
$routes = new RouteCollection(new MockFileLocator(new \Config\Autoload()));
128+
$routes = new RouteCollection(new MockFileLocator(new \Config\Autoload()), new \Config\Modules());
129129
$routes->setAutoRoute(false);
130130
$routes->set404Override(function() {
131131
echo '404 Override by Closure.';

tests/system/CommonFunctionsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function testRedirectReturnsRedirectResponse()
102102
$_SERVER['REQUEST_METHOD'] = 'GET';
103103

104104
$response = $this->createMock(\CodeIgniter\HTTP\Response::class);
105-
$routes = new \CodeIgniter\Router\RouteCollection(new \Tests\Support\Autoloader\MockFileLocator(new \Config\Autoload()));
105+
$routes = new \CodeIgniter\Router\RouteCollection(new \Tests\Support\Autoloader\MockFileLocator(new \Config\Autoload()), new \Config\Modules());
106106
\CodeIgniter\Services::injectMock('response', $response);
107107
\CodeIgniter\Services::injectMock('routes', $routes);
108108

@@ -267,7 +267,7 @@ public function testOldInput()
267267
$this->config = new App();
268268
$this->config->baseURL = 'http://example.com';
269269

270-
$this->routes = new RouteCollection(new MockFileLocator(new Autoload()));
270+
$this->routes = new RouteCollection(new MockFileLocator(new Autoload()), new \Config\Modules());
271271
Services::injectMock('routes', $this->routes);
272272

273273
$this->request = new MockIncomingRequest($this->config, new URI('http://example.com'), null, new UserAgent());

tests/system/Config/BaseConfigTest.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ public function setup()
2323
{
2424
require $this->fixturesFolder . '/RegistrarConfig.php';
2525
}
26-
if ( ! class_exists('RegistrarConfig2', false))
27-
{
28-
require $this->fixturesFolder . '/RegistrarConfig2.php';
29-
}
30-
if ( ! class_exists('RegistrarConfig3', false))
31-
{
32-
require $this->fixturesFolder . '/RegistrarConfig3.php';
33-
}
3426
}
3527

3628
//--------------------------------------------------------------------
@@ -147,6 +139,10 @@ public function testRecognizesLooseValues()
147139
public function testRegistrars()
148140
{
149141
$config = new \RegistrarConfig();
142+
$config::$registrars = ['\Tests\Support\Config\Registrar'];
143+
$this->setPrivateProperty($config, 'didDiscovery', true);
144+
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
145+
$method();
150146

151147
// no change to unmodified property
152148
$this->assertEquals('bar', $config->foo);
@@ -160,16 +156,13 @@ public function testRegistrars()
160156

161157
public function testBadRegistrar()
162158
{
163-
$this->expectException('RuntimeException');
164-
$config = new \RegistrarConfig2();
165-
$this->assertEquals('bar', $config->foo);
166-
}
159+
// Shouldn't change any values.
160+
$config = new \RegistrarConfig();
161+
$config::$registrars = ['\Tests\Support\Config\BadRegistrar'];
162+
$this->setPrivateProperty($config, 'didDiscovery', true);
163+
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
164+
$method();
167165

168-
// not very interesting, but useful for code coverage
169-
public function testWorseRegistrar()
170-
{
171-
$config = new \RegistrarConfig3();
172-
// there should be no change
173166
$this->assertEquals('bar', $config->foo);
174167
}
175168

tests/system/Config/fixtures/RegistrarConfig.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ class RegistrarConfig extends \CodeIgniter\Config\BaseConfig
77
'baz'
88
];
99

10-
protected $registrars = [
11-
\Tests\Support\Config\Registrar::class
12-
];
1310
}

tests/system/Config/fixtures/RegistrarConfig2.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/system/Config/fixtures/RegistrarConfig3.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)