Completed
Push — master ( 6ab23e...25d83e )
by
unknown
10:42
created

PHPUnit_Framework_TestCase::createMock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Helpers;
13
14
/**
15
 * This is helpers class for supporting old and new PHPUnit versions.
16
 *
17
 * @todo NEXT_MAJOR: Remove this class when dropping support for < PHPUnit 5.4.
18
 *
19
 * @author Oskar Stark <[email protected]>
20
 */
21
class PHPUnit_Framework_TestCase extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function expectException($exception, $message = '', $code = null)
27
    {
28
        if (is_callable('parent::expectException')) {
29
            parent::expectException($exception);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PHPUnit_Framework_TestCase as the method expectException() does only exist in the following sub-classes of PHPUnit_Framework_TestCase: Sonata\DoctrineMongoDBAd...in\FieldDescriptionTest, Sonata\DoctrineMongoDBAd...lder\FormContractorTest, Sonata\DoctrineMongoDBAd...ilter\BooleanFilterTest, Sonata\DoctrineMongoDBAd...lter\CallbackFilterTest, Sonata\DoctrineMongoDBAd...Filter\ChoiceFilterTest, Sonata\DoctrineMongoDBAd...terWithQueryBuilderTest, Sonata\DoctrineMongoDBAd...\Filter\ModelFilterTest, Sonata\DoctrineMongoDBAd...Filter\NumberFilterTest, Sonata\DoctrineMongoDBAd...Filter\StringFilterTest, Sonata\DoctrineMongoDBAd...Unit_Framework_TestCase, Sonata\DoctrineMongoDBAd...\Model\ModelManagerTest. 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...
30
31
            if ($message !== '') {
32
                parent::expectExceptionMessage($message);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (expectExceptionMessage() instead of expectException()). Are you sure this is correct? If so, you might want to change this to $this->expectExceptionMessage().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Bug introduced by
The method expectExceptionMessage() does not seem to exist on object<PHPUnit_Framework_TestCase>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
            }
34
35
            if ($code !== null) {
36
                parent::expectExceptionCode($code);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (expectExceptionCode() instead of expectException()). Are you sure this is correct? If so, you might want to change this to $this->expectExceptionCode().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Bug introduced by
The method expectExceptionCode() does not seem to exist on object<PHPUnit_Framework_TestCase>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
            }
38
        }
39
40
        return parent::setExpectedException($exception, $message, $code);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setExpectedException() instead of expectException()). Are you sure this is correct? If so, you might want to change this to $this->setExpectedException().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function createMock($originalClassName)
47
    {
48
        if (is_callable('parent::createMock')) {
49
            return parent::createMock($originalClassName);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PHPUnit_Framework_TestCase as the method createMock() does only exist in the following sub-classes of PHPUnit_Framework_TestCase: Sonata\CoreBundle\Test\AbstractWidgetTestCase, Sonata\DoctrineMongoDBAd...in\FieldDescriptionTest, Sonata\DoctrineMongoDBAd...lder\FormContractorTest, Sonata\DoctrineMongoDBAd...ilter\BooleanFilterTest, Sonata\DoctrineMongoDBAd...lter\CallbackFilterTest, Sonata\DoctrineMongoDBAd...Filter\ChoiceFilterTest, Sonata\DoctrineMongoDBAd...terWithQueryBuilderTest, Sonata\DoctrineMongoDBAd...\Filter\ModelFilterTest, Sonata\DoctrineMongoDBAd...Filter\NumberFilterTest, Sonata\DoctrineMongoDBAd...Filter\StringFilterTest, Sonata\DoctrineMongoDBAd...Unit_Framework_TestCase, Sonata\DoctrineMongoDBAd...\Model\ModelManagerTest. 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...
50
        }
51
52
        return parent::getMockBuilder($originalClassName)
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getMockBuilder() instead of createMock()). Are you sure this is correct? If so, you might want to change this to $this->getMockBuilder().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
53
            ->disableOriginalConstructor()
54
            ->disableOriginalClone()
55
            ->disableArgumentCloning()
56
            ->getMock();
57
    }
58
}
59