MultiDomainTest::testGetPrimaryDomain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\MultiDomain\Tests;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\MultiDomain\MultiDomain;
8
use SilverStripe\MultiDomain\MultiDomainDomain;
9
10
class MultiDomainTest extends SapphireTest
11
{
12
    /**
13
     * Set up some test domain configuration
14
     *
15
     * {@inheritDoc}
16
     */
17
    public function setUp()
18
    {
19
        parent::setUp();
20
        Config::nest();
21
22
        Config::inst()->remove(MultiDomain::class, 'domains');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SilverStripe\Config\Coll...nfigCollectionInterface as the method remove() does only exist in the following implementations of said interface: SilverStripe\Config\Coll...s\DeltaConfigCollection, SilverStripe\Config\Coll...\MemoryConfigCollection.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
23
        Config::inst()->update(MultiDomain::class, 'domains', array(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SilverStripe\Config\Coll...nfigCollectionInterface as the method update() does only exist in the following implementations of said interface: SilverStripe\Config\Coll...s\DeltaConfigCollection, SilverStripe\Config\Coll...\MemoryConfigCollection.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
24
            'primary' => array(
25
                'hostname' => 'foo.bar',
26
                'resolves_to' => 'bar.baz'
27
            ),
28
            'secondary' => array(
29
                'hostname' => 'localhost',
30
                'resolves_to' => 'local.dev'
31
            )
32
        ));
33
    }
34
35
    /**
36
     * Test that a MultiDomainDomain can be returned from the configured domains
37
     */
38
    public function testGetDomain()
39
    {
40
        $this->assertNull(MultiDomain::get_domain('does-not-exist'));
41
        $this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary'));
42
    }
43
44
    /**
45
     * Test that all domains can be returned, with or without the primary domain
46
     *
47
     * @dataProvider getAllDomainsProvider
48
     * @param bool $withPrimary
49
     */
50
    public function testGetAllDomains($withPrimary)
51
    {
52
        $result = MultiDomain::get_all_domains($withPrimary);
53
        $this->assertInternalType('array', $result);
54
        $this->assertNotEmpty($result);
55
56
        $expectedCount = $withPrimary ? 2 : 1;
57
        $this->assertCount($expectedCount, $result);
58
    }
59
60
    /**
61
     * @return array[]
62
     */
63
    public function getAllDomainsProvider()
64
    {
65
        return array(
66
            array(true),
67
            array(false)
68
        );
69
    }
70
71
    /**
72
     * Test that the primary domain can be returned
73
     */
74
    public function testGetPrimaryDomain()
75
    {
76
        $result = MultiDomain::get_primary_domain();
77
        $this->assertInstanceOf(MultiDomainDomain::class, $result);
78
        $this->assertTrue($result->isPrimary());
79
    }
80
81
    /**
82
     * Test that the correct domain can be returned by a provided URL
83
     */
84
    public function testDomainForUrl()
85
    {
86
        $result = MultiDomain::domain_for_url('foo.bar/my-page');
87
        $this->assertInstanceOf(MultiDomainDomain::class, $result);
88
        $this->assertSame('primary', $result->getKey());
89
    }
90
91
    /**
92
     * Test that if a URL doesn't match any domain then the primary domain is returned
93
     */
94
    public function testDomainForUrlDefaultsToPrimaryDomain()
95
    {
96
        $this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary());
97
    }
98
99
    public function tearDown()
100
    {
101
        Config::unnest();
102
        parent::tearDown();
103
    }
104
}
105