AdapterTestIgnore   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 10
dl 0
loc 75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 21 1
A testCanSetPaginator() 0 7 1
A testContainedItemsCount() 0 7 1
A testGetsItemsAtOffsetZero() 0 9 2
A testGetsItemsAtOffsetTen() 0 9 2
1
<?php
2
3
namespace DoctrineORMModuleTest\Paginator;
4
5
use DoctrineORMModuleTest\Framework\TestCase;
6
use DoctrineORMModuleTest\Assets\Fixture\TestFixture;
7
8
use Doctrine\Common\DataFixtures\Loader as FixtureLoader;
9
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
10
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
11
12
use Doctrine\ORM\QueryBuilder;
13
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
14
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as PaginatorAdapter;
15
16
class AdapterTestIgnore extends TestCase
17
{
18
    /**
19
     * @var QueryBuilder
20
     */
21
    protected $qb;
22
23
    /**
24
     * @var PaginatorAdapter
25
     */
26
    protected $paginatorAdapter;
27
28
    /**
29
     * @var DoctrinePaginator
30
     */
31
    protected $paginator;
32
33
    public function setUp()
34
    {
35
        parent::setUp();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class DoctrineORMModuleTest\Framework\TestCase as the method setUp() does only exist in the following sub-classes of DoctrineORMModuleTest\Framework\TestCase: DoctrineORMModuleTest\Form\AnnotationBuilderTest, DoctrineORMModuleTest\Fo...AnnotationsListenerTest, DoctrineORMModuleTest\Paginator\AdapterTestIgnore. 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...
36
37
        $this->createDb();
38
        $loader = new FixtureLoader();
39
        $loader->addFixture(new TestFixture());
40
        $purger = new ORMPurger();
41
        $executor = new ORMExecutor($this->getEntityManager(), $purger);
42
        $executor->execute($loader->getFixtures());
43
44
        $this->qb = $this
45
            ->getEntityManager()
46
            ->createQueryBuilder()
47
            ->select('t')
48
            ->from(\DoctrineORMModuleTest\Assets\Entity\Test::class, 't')
49
            ->orderBy('t.id', 'ASC');
50
51
        $this->paginator = new DoctrinePaginator($this->qb);
52
        $this->paginatorAdapter = new PaginatorAdapter($this->paginator);
53
    }
54
55
    public function testCanSetPaginator()
56
    {
57
        $this->assertSame($this->paginator, $this->paginatorAdapter->getPaginator());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...ator\AdapterTestIgnore>.

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...
58
        $doctrinePaginator = new DoctrinePaginator($this->getEntityManager()->createQuery(''));
59
        $this->paginatorAdapter->setPaginator($doctrinePaginator);
60
        $this->assertSame($doctrinePaginator, $this->paginatorAdapter->getPaginator());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...ator\AdapterTestIgnore>.

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...
61
    }
62
63
    public function testContainedItemsCount()
64
    {
65
        $itemsCount = $this->qb->select('COUNT(t)')->getQuery()->getSingleScalarResult();
66
67
        $this->assertEquals($itemsCount, $this->paginatorAdapter->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...ator\AdapterTestIgnore>.

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...
68
        $this->assertEquals($itemsCount, count($this->paginatorAdapter->getItems(0, $itemsCount + 100)));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...ator\AdapterTestIgnore>.

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...
69
    }
70
71
    public function testGetsItemsAtOffsetZero()
72
    {
73
        $expected = $this->qb->setMaxResults(10)->getQuery()->getResult();
74
        $actual = $this->paginatorAdapter->getItems(0, 10);
75
76
        foreach ($expected as $key => $expectedItem) {
77
            $this->assertEquals($expectedItem, $actual[$key]);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...ator\AdapterTestIgnore>.

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...
78
        }
79
    }
80
81
    public function testGetsItemsAtOffsetTen()
82
    {
83
        $expected = $this->qb->setFirstResult(10)->setMaxResults(10)->getQuery()->getResult();
84
        $actual = $this->paginatorAdapter->getItems(10, 10);
85
86
        foreach ($expected as $key => $expectedItem) {
87
            $this->assertEquals($expectedItem, $actual[$key]);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...ator\AdapterTestIgnore>.

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...
88
        }
89
    }
90
}
91