Skip to content

Commit

Permalink
Merge pull request #1811 from jim-parry/testing22
Browse files Browse the repository at this point in the history
Test: Config module coverage improved
  • Loading branch information
jim-parry authored Mar 12, 2019
2 parents bab5281 + 9592877 commit 9af0a89
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 9 deletions.
5 changes: 3 additions & 2 deletions system/Config/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,9 @@ protected function registerProperties()

if (! static::$didDiscovery)
{
$locator = \Config\Services::locator();
static::$registrars = $locator->search('Config/Registrar.php');
$locator = \Config\Services::locator();
static::$registrars = $locator->search('Config/Registrar.php');
static::$didDiscovery = true;
}

$shortName = (new \ReflectionClass($this))->getShortName();
Expand Down
2 changes: 2 additions & 0 deletions system/Config/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ protected static function discoverServices(string $name, array $arguments)

if (empty($files))
{
// no files at all found - this would be really, really bad
return null;
}

Expand All @@ -271,6 +272,7 @@ protected static function discoverServices(string $name, array $arguments)

if (! static::$services)
{
// we found stuff, but no services - this would be really bad
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Config/BadRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class BadRegistrar
{

public static function RegistrarConfig2()
public static function RegistrarConfig()
{
return 'I am not worthy';
}
Expand Down
28 changes: 28 additions & 0 deletions tests/_support/Config/MockServices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Tests\Support\Config;

use \CodeIgniter\Config\BaseService;

class MockServices extends BaseService
{

public $psr4 = [
'Tests/Support' => TESTPATH . '_support/',
];
public $classmap = [];

//--------------------------------------------------------------------

public function __construct()
{
// Don't call the parent since we don't want the default mappings.
// parent::__construct();
}

//--------------------------------------------------------------------
public static function locator(bool $getShared = true)
{
return new \CodeIgniter\Autoloader\FileLocator(static::autoloader());
}

}
30 changes: 30 additions & 0 deletions tests/system/Config/BaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,40 @@ public function testBadRegistrar()
$config = new \RegistrarConfig();
$config::$registrars = ['\Tests\Support\Config\BadRegistrar'];
$this->setPrivateProperty($config, 'didDiscovery', true);

$this->expectException(\RuntimeException::class);
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();

$this->assertEquals('bar', $config->foo);
}

public function testNotEnabled()
{
$modulesConfig = config('Modules');
$modulesConfig->enabled = false;

$config = new \RegistrarConfig();
$expected = $config::$registrars;

$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();

$this->assertEquals($expected, $config::$registrars);
}

public function testDidDiscovery()
{
$modulesConfig = config('Modules');
$modulesConfig->enabled = true;

$config = new \RegistrarConfig();
$this->setPrivateProperty($config, 'didDiscovery', false);

$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
$method();

$this->assertEquals(true, $this->getPrivateProperty($config, 'didDiscovery'));
}

}
17 changes: 15 additions & 2 deletions tests/system/Config/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace CodeIgniter\Config;
<?php
namespace CodeIgniter\Config;

use Config\DocTypes;

Expand All @@ -23,7 +24,7 @@ public function testCreateInvalidInstance()

public function testCreateSharedInstance()
{
$Config = Config::get('DocTypes' );
$Config = Config::get('DocTypes');
$Config2 = Config::get('Config\\DocTypes');

$this->assertTrue($Config === $Config2);
Expand All @@ -35,4 +36,16 @@ public function testCreateNonConfig()

$this->assertNull($Config);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testInjection()
{
Config::reset();
Config::injectMock('Banana', '\stdClass');
$this->assertNotNull(Config::get('Banana'));
}

}
8 changes: 4 additions & 4 deletions user_guide_src/source/concepts/services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ within the class, and, if not, creates a new one. All of the factory methods pro
Service Discovery
-----------------

CodeIgniter can automatically discover any Config\Services.php files you may have created within any defined namespaces.
CodeIgniter can automatically discover any Config\\Services.php files you may have created within any defined namespaces.
This allows simple use of any module Services files. In order for custom Services files to be discovered, they must
meet these requirements:

- It's namespace must be defined ``Config\Autoload.php``
- Its namespace must be defined in ``Config\Autoload.php``
- Inside the namespace, the file must be found at ``Config\Services.php``
- It must extend ``CodeIgniter\Config\BaseService``

A small example should clarify this.

Imagine that you've created a new directory, ``Blog`` in your root directory. This will hold a blog module with controllers,
Imagine that you've created a new directory, ``Blog`` in your root directory. This will hold a **blog module** with controllers,
models, etc, and you'd like to make some of the classes available as a service. The first step is to create a new file:
``Blog\Config\Services.php``. The skeleton of the file should be::

Expand All @@ -164,4 +164,4 @@ would simply use the framework's ``Config\Services`` class to grab your service:

$postManager = Config\Services::postManager();

.. note:: If multiple Services file have the same method name, the first one found will be the instance returned.
.. note:: If multiple Services files have the same method name, the first one found will be the instance returned.

0 comments on commit 9af0a89

Please sign in to comment.