Completed
Push — master ( 4ad6bd...3873e4 )
by Ingo
11:53
created

KernelTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testNesting() 0 37 1
A testInvalidInjectorDetection() 0 13 1
A testInvalidConfigDetection() 0 13 1
1
<?php
2
3
namespace SilverStripe\Core\Tests;
4
5
use BadMethodCallException;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Config\ConfigLoader;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Core\Injector\InjectorLoader;
11
use SilverStripe\Core\Kernel;
12
use SilverStripe\Dev\SapphireTest;
13
14
class KernelTest extends SapphireTest
15
{
16
    public function testNesting()
17
    {
18
        /** @var Kernel $kernel */
19
        $kernel = Injector::inst()->get(Kernel::class);
20
21
        $nested1 = $kernel->nest();
22
        Director::config()->set('alternate_base_url', '/mysite/');
23
        $this->assertEquals($nested1->getConfigLoader(), ConfigLoader::inst());
24
        $this->assertEquals($nested1->getInjectorLoader(), InjectorLoader::inst());
25
        $this->assertEquals(1, ConfigLoader::inst()->countManifests());
26
        $this->assertEquals(1, InjectorLoader::inst()->countManifests());
27
28
        // Re-nest
29
        $nested2 = $nested1->nest();
30
31
        // Nesting config / injector should increase this count
32
        Injector::nest();
33
        Config::nest();
34
        $this->assertEquals($nested2->getConfigLoader(), ConfigLoader::inst());
35
        $this->assertEquals($nested2->getInjectorLoader(), InjectorLoader::inst());
36
        $this->assertEquals(2, ConfigLoader::inst()->countManifests());
37
        $this->assertEquals(2, InjectorLoader::inst()->countManifests());
38
        Director::config()->set('alternate_base_url', '/anothersite/');
39
40
        // Nesting always resets sub-loaders to 1
41
        $nested2->nest();
42
        $this->assertEquals(1, ConfigLoader::inst()->countManifests());
43
        $this->assertEquals(1, InjectorLoader::inst()->countManifests());
44
45
        // Calling ->activate() on a previous kernel restores
46
        $nested1->activate();
47
        $this->assertEquals($nested1->getConfigLoader(), ConfigLoader::inst());
48
        $this->assertEquals($nested1->getInjectorLoader(), InjectorLoader::inst());
49
        $this->assertEquals('/mysite/', Director::config()->get('alternate_base_url'));
50
        $this->assertEquals(1, ConfigLoader::inst()->countManifests());
51
        $this->assertEquals(1, InjectorLoader::inst()->countManifests());
52
    }
53
54
    public function testInvalidInjectorDetection()
55
    {
56
        $this->expectException(BadMethodCallException::class);
57
        $this->expectExceptionMessage(
58
            "Non-current injector manifest cannot be accessed. Please call ->activate() first"
59
        );
60
61
        /** @var Kernel $kernel */
62
        $kernel = Injector::inst()->get(Kernel::class);
63
        $kernel->nest(); // $kernel is no longer current kernel
64
65
        $kernel->getInjectorLoader()->getManifest();
66
    }
67
68
    public function testInvalidConfigDetection()
69
    {
70
        $this->expectException(BadMethodCallException::class);
71
        $this->expectExceptionMessage(
72
            "Non-current config manifest cannot be accessed. Please call ->activate() first"
73
        );
74
75
        /** @var Kernel $kernel */
76
        $kernel = Injector::inst()->get(Kernel::class);
77
        $kernel->nest(); // $kernel is no longer current kernel
78
79
        $kernel->getConfigLoader()->getManifest();
80
    }
81
}
82