Completed
Push — authenticator-refactor ( 0a18bb...b9e528 )
by Simon
08:12
created

testIncludeWithRootConfigFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\Tests\Manifest;
4
5
use SilverStripe\Core\Manifest\ManifestFileFinder;
6
use SilverStripe\Dev\SapphireTest;
7
8
/**
9
 * Tests for the {@link ManifestFileFinder} class.
10
 */
11
class ManifestFileFinderTest extends SapphireTest
12
{
13
14
    protected $base;
15
16
    public function __construct()
17
    {
18
        $this->defaultBase = dirname(__FILE__) . '/fixtures/manifestfilefinder';
0 ignored issues
show
Bug introduced by
The property defaultBase does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
        parent::__construct();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SilverStripe\Dev\SapphireTest as the method __construct() does only exist in the following sub-classes of SilverStripe\Dev\SapphireTest: SilverStripe\Core\Tests\...\ManifestFileFinderTest, SilverStripe\Security\Tests\MemberTest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
20
    }
21
22
    public function assertFinderFinds($finder, $base, $expect, $message = null)
23
    {
24
        if (!$base) {
25
            $base = $this->defaultBase;
26
        }
27
28
        $found = $finder->find($base);
29
30
        foreach ($expect as $k => $file) {
31
            $expect[$k] = "{$base}/$file";
32
        }
33
34
        sort($expect);
35
        sort($found);
36
37
        $this->assertEquals($expect, $found, $message);
38
    }
39
40
    public function testBasicOperation()
41
    {
42
        $finder = new ManifestFileFinder();
43
        $finder->setOption('name_regex', '/\.txt$/');
44
45
        $this->assertFinderFinds(
46
            $finder,
47
            null,
48
            array(
49
            'module/module.txt'
50
            )
51
        );
52
    }
53
54 View Code Duplication
    public function testIgnoreTests()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $finder = new ManifestFileFinder();
57
        $finder->setOption('name_regex', '/\.txt$/');
58
        $finder->setOption('ignore_tests', false);
59
60
        $this->assertFinderFinds(
61
            $finder,
62
            null,
63
            array(
64
            'module/module.txt',
65
            'module/tests/tests.txt',
66
            'module/code/tests/tests2.txt'
67
            )
68
        );
69
    }
70
71 View Code Duplication
    public function testIncludeThemes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $finder = new ManifestFileFinder();
74
        $finder->setOption('name_regex', '/\.txt$/');
75
        $finder->setOption('include_themes', true);
76
77
        $this->assertFinderFinds(
78
            $finder,
79
            null,
80
            array(
81
            'module/module.txt',
82
            'themes/themes.txt'
83
            )
84
        );
85
    }
86
87
    public function testIncludeWithRootConfigFile()
88
    {
89
        $finder = new ManifestFileFinder();
90
91
        $this->assertFinderFinds(
92
            $finder,
93
            dirname(__FILE__) . '/fixtures/manifestfilefinder_rootconfigfile',
94
            array(
95
                'code/code.txt',
96
            )
97
        );
98
    }
99
100
    public function testIncludeWithRootConfigFolder()
101
    {
102
        $finder = new ManifestFileFinder();
103
104
        $this->assertFinderFinds(
105
            $finder,
106
            dirname(__FILE__) . '/fixtures/manifestfilefinder_rootconfigfolder',
107
            array(
108
                '_config/config.yml',
109
                'code/code.txt',
110
            )
111
        );
112
    }
113
}
114