Completed
Push — master ( ee9615...483a86 )
by Robbie
14s
created

InitStateMiddlewareTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Subsites\Tests;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Subsites\Middleware\InitStateMiddleware;
8
use SilverStripe\Subsites\Model\Subsite;
9
use SilverStripe\Subsites\State\SubsiteState;
10
11
class InitStateMiddlewareTest extends BaseSubsiteTest
12
{
13
    protected static $fixture_file = 'SubsiteTest.yml';
14
15
    /**
16
     * Original value of $_REQUEST
17
     *
18
     * @var array
19
     */
20
    protected $origServer = [];
21
22
    protected function setUp()
23
    {
24
        parent::setUp();
25
26
        $this->origServer = $_SERVER;
27
    }
28
29
    protected function tearDown()
30
    {
31
        $_SERVER = $this->origServer;
32
33
        parent::tearDown();
34
    }
35
36
    public function testDomainDetectionViaServerHeaders()
37
    {
38
        $_SERVER['HTTP_HOST'] = 'one.example.org';
39
40
        $this->getMiddleware()->process($this->getRequest(), $this->getCallback());
41
42
        $expectedSubsite = $this->objFromFixture(Subsite::class, 'domaintest1');
43
        $this->assertEquals($expectedSubsite->ID, $this->getState()->getSubsiteId());
44
    }
45
46
    public function testDomainDetectionViaRequestOverridesServerHeaders()
47
    {
48
        $_SERVER['HTTP_HOST'] = 'one.example.org';
49
50
        $this->getMiddleware()->process($this->getRequest('two.mysite.com'), $this->getCallback());
51
52
        $expectedSubsite = $this->objFromFixture(Subsite::class, 'domaintest2');
53
        $this->assertEquals($expectedSubsite->ID, $this->getState()->getSubsiteId());
54
    }
55
56
    protected function getMiddleware()
57
    {
58
        return new InitStateMiddleware();
59
    }
60
61
    protected function getRequest($domain = null)
62
    {
63
        $request = new HTTPRequest('GET', '/test/url');
64
        if ($domain) {
65
            $request->addHeader('host', $domain);
66
        }
67
        return $request;
68
    }
69
70
    protected function getCallback()
71
    {
72
        return function () {
73
        };
74
    }
75
76
    protected function getState()
77
    {
78
        return Injector::inst()->get(SubsiteState::class);
79
    }
80
}
81