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

KernelTestState   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A kernel() 0 4 1
A setUp() 0 4 1
A tearDown() 0 4 1
A setUpOnce() 0 9 2
A tearDownOnce() 0 4 1
A nest() 0 6 1
A unnest() 0 7 1
1
<?php
2
3
namespace SilverStripe\Dev\State;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Core\Kernel;
7
use SilverStripe\Dev\TestKernel;
8
use SilverStripe\Dev\SapphireTest;
9
10
/**
11
 * Handles nesting of kernel before / after tests
12
 */
13
class KernelTestState implements TestState
14
{
15
    /**
16
     * Stack of kernels
17
     *
18
     * @var TestKernel[]
19
     */
20
    protected $kernels = [];
21
22
    /**
23
     * Get active Kernel instance
24
     *
25
     * @return \SilverStripe\Dev\TestKernel
26
     */
27
    protected function kernel()
28
    {
29
        return end($this->kernels);
30
    }
31
32
    /**
33
     * Called on setup
34
     *
35
     * @param SapphireTest $test
36
     */
37
    public function setUp(SapphireTest $test)
38
    {
39
        $this->nest();
40
    }
41
42
    /**
43
     * Called on tear down
44
     *
45
     * @param SapphireTest $test
46
     */
47
    public function tearDown(SapphireTest $test)
48
    {
49
        $this->unnest();
50
    }
51
52
    /**
53
     * Called once on setup
54
     *
55
     * @param string $class Class being setup
56
     */
57
    public function setUpOnce($class)
58
    {
59
        // If first run, get initial kernel
60
        if (empty($this->kernels)) {
61
            $this->kernels[] = Injector::inst()->get(Kernel::class);
62
        }
63
64
        $this->nest();
65
    }
66
67
    /**
68
     * Called once on tear down
69
     *
70
     * @param string $class Class being torn down
71
     */
72
    public function tearDownOnce($class)
73
    {
74
        $this->unnest();
75
    }
76
77
    /**
78
     * Nest the current kernel
79
     */
80
    protected function nest()
81
    {
82
        // Reset state
83
        $this->kernel()->reset();
84
        $this->kernels[] = $this->kernel()->nest();
85
    }
86
87
    protected function unnest()
88
    {
89
        // Unnest and reset
90
        array_pop($this->kernels);
91
        $this->kernel()->activate();
92
        $this->kernel()->reset();
93
    }
94
}
95