Completed
Pull Request — 5.7 (#2813)
by Oskar
12:27
created

DoctrineDBALAdapterTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 21.79 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 17
loc 78
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructorWithIncorrectCountField() 0 7 1
A testGetQueryBuilder() 8 8 1
A testConstructorThrowsAnotherException() 9 9 1
A testGetSlice() 0 16 1
A testNbResults() 0 15 1
A testNbResultsWithZeroResults() 0 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\Tests\AdminList\Helper;
4
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use Doctrine\DBAL\Statement;
7
use Kunstmaan\AdminListBundle\Helper\DoctrineDBALAdapter;
8
use PHPUnit\Framework\TestCase;
9
10
class DoctrineDBALAdapterTest extends TestCase
11
{
12
    public function testConstructorWithIncorrectCountField()
13
    {
14
        $this->expectExceptionMessage('The $countField must contain a table alias in the string.');
15
        $this->expectException(\LogicException::class);
16
        $qb = $this->createMock(QueryBuilder::class);
17
        new DoctrineDBALAdapter($qb, 'somefield');
0 ignored issues
show
Documentation introduced by
$qb is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Query\QueryBuilder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
18
    }
19
20 View Code Duplication
    public function testGetQueryBuilder()
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...
21
    {
22
        $qb = $this->createMock(QueryBuilder::class);
23
        $qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT);
24
        $adapter = new DoctrineDBALAdapter($qb, 'table.somefield');
0 ignored issues
show
Documentation introduced by
$qb is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Query\QueryBuilder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
26
        $this->assertInstanceOf(QueryBuilder::class, $adapter->getQueryBuilder());
27
    }
28
29 View Code Duplication
    public function testConstructorThrowsAnotherException()
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...
30
    {
31
        $this->expectExceptionMessage('Only SELECT queries can be paginated.');
32
        $this->expectException(\LogicException::class);
33
        $qb = $this->createMock(QueryBuilder::class);
34
        $qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::DELETE);
35
36
        new DoctrineDBALAdapter($qb, 'table.somefield');
0 ignored issues
show
Documentation introduced by
$qb is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Query\QueryBuilder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
    }
38
39
    public function testGetSlice()
40
    {
41
        $length = 3;
42
43
        $statement = $this->createMock(Statement::class);
44
        $statement->expects($this->once())->method('fetchAll')->willReturn([1, 2, 3]);
45
        $qb = $this->createMock(QueryBuilder::class);
46
        $qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT);
47
        $qb->expects($this->once())->method('setFirstResult')->willReturn($qb);
48
        $qb->expects($this->once())->method('setMaxResults')->with($length)->willReturn($qb);
49
        $qb->expects($this->once())->method('execute')->willReturn($statement);
50
51
        $adapter = new DoctrineDBALAdapter($qb, 'table.somefield');
0 ignored issues
show
Documentation introduced by
$qb is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Query\QueryBuilder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
        $result = $adapter->getSlice(0, $length);
53
        $this->assertCount($length, $result);
54
    }
55
56
    public function testNbResults()
57
    {
58
        $statement = $this->createMock(Statement::class);
59
        $statement->expects($this->once())->method('fetchColumn')->with(0)->willReturn([1, 2, 3]);
60
        $qb = $this->createMock(QueryBuilder::class);
61
        $qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT);
62
        $qb->expects($this->once())->method('select')->willReturn($qb);
63
        $qb->expects($this->once())->method('orderBy')->willReturn($qb);
64
        $qb->expects($this->once())->method('setMaxResults')->with(1)->willReturn($qb);
65
        $qb->expects($this->once())->method('execute')->willReturn($statement);
66
67
        $adapter = new DoctrineDBALAdapter($qb, 'table.somefield');
0 ignored issues
show
Documentation introduced by
$qb is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Query\QueryBuilder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
        $result = $adapter->getNbResults();
69
        $this->assertCount(3, $result);
0 ignored issues
show
Documentation introduced by
$result is of type integer, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
    }
71
72
    public function testNbResultsWithZeroResults()
73
    {
74
        $statement = $this->createMock(Statement::class);
75
        $statement->expects($this->once())->method('fetchColumn')->with(0)->willReturn(null);
76
        $qb = $this->createMock(QueryBuilder::class);
77
        $qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT);
78
        $qb->expects($this->once())->method('select')->willReturn($qb);
79
        $qb->expects($this->once())->method('orderBy')->willReturn($qb);
80
        $qb->expects($this->once())->method('setMaxResults')->with(1)->willReturn($qb);
81
        $qb->expects($this->once())->method('execute')->willReturn($statement);
82
83
        $adapter = new DoctrineDBALAdapter($qb, 'table.somefield');
0 ignored issues
show
Documentation introduced by
$qb is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Query\QueryBuilder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
        $result = $adapter->getNbResults();
85
        $this->assertSame(0, $result);
86
    }
87
}
88