Completed
Pull Request — master (#6930)
by Daniel
09:37
created

FulltextSearchableTest::testIndexesAdded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Tests\Search;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\ORM\Connect\MySQLSchemaManager;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\Search\FulltextSearchable;
11
12
class FulltextSearchableTest extends SapphireTest
13
{
14
15
    protected function setUp()
16
    {
17
        parent::setUp();
18
19
        FulltextSearchable::enable(File::class);
20
    }
21
22
    /**
23
     * FulltextSearchable::enable() leaves behind remains that don't get cleaned up
24
     * properly at the end of the test. This becomes apparent when a later test tries to
25
     * ALTER TABLE File and add fulltext indexes with the InnoDB table type.
26
     */
27
    protected function tearDown()
28
    {
29
        parent::tearDown();
30
31
        File::remove_extension(FulltextSearchable::class);
32
        Config::inst()->update(
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...\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...
33
            File::class,
34
            'create_table_options',
35
            array(
36
            MySQLSchemaManager::ID => 'ENGINE=InnoDB')
37
        );
38
    }
39
40
    public function testEnable()
41
    {
42
        $this->assertTrue(File::has_extension(FulltextSearchable::class));
43
    }
44
45
    public function testEnableWithCustomClasses()
46
    {
47
        FulltextSearchable::enable(array(File::class));
48
        $this->assertTrue(File::has_extension(FulltextSearchable::class));
49
50
        File::remove_extension(FulltextSearchable::class);
51
        $this->assertFalse(File::has_extension(FulltextSearchable::class));
52
    }
53
54
    public function testIndexesAdded()
55
    {
56
        $indexes = DataObject::getSchema()->databaseIndexes(File::class);
57
        $this->assertArrayHasKey('SearchFields', $indexes);
58
        $this->assertCount(2, $indexes['SearchFields']['columns']);
59
        $this->assertContains('Name', $indexes['SearchFields']['columns']);
60
        $this->assertContains('Title', $indexes['SearchFields']['columns']);
61
    }
62
}
63