Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

Tests/DBAL/Sharding/PoolingShardManagerTest.php (5 issues)

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
namespace Doctrine\Tests\DBAL\Sharding;
20
21
use Doctrine\DBAL\Sharding\PoolingShardManager;
22
23
class PoolingShardManagerTest extends \PHPUnit\Framework\TestCase
24
{
25
    private function createConnectionMock()
26
    {
27
        return $this->getMockBuilder('Doctrine\DBAL\Sharding\PoolingShardConnection')
28
            ->setMethods(array('connect', 'getParams', 'fetchAll'))
29
            ->disableOriginalConstructor()
30
            ->getMock();
31
    }
32
33 View Code Duplication
    private function createPassthroughShardChoser()
34
    {
35
        $mock = $this->createMock('Doctrine\DBAL\Sharding\ShardChoser\ShardChoser');
36
        $mock->expects($this->any())
37
             ->method('pickShard')
38
             ->will($this->returnCallback(function($value) { return $value; }));
39
        return $mock;
40
    }
41
42 View Code Duplication
    private function createStaticShardChoser()
0 ignored issues
show
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...
43
    {
44
        $mock = $this->createMock('Doctrine\DBAL\Sharding\ShardChoser\ShardChoser');
45
        $mock->expects($this->any())
46
            ->method('pickShard')
47
            ->will($this->returnCallback(function($value) { return 1; }));
0 ignored issues
show
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
            ->will($this->returnCallback(function(/** @scrutinizer ignore-unused */ $value) { return 1; }));

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
        return $mock;
49
    }
50
51
    public function testSelectGlobal()
52
    {
53
        $conn = $this->createConnectionMock();
54
        $conn->expects($this->once())->method('connect')->with($this->equalTo(0));
55
56
        $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
57
        $shardManager->selectGlobal();
58
59
        self::assertNull($shardManager->getCurrentDistributionValue());
60
    }
61
62
    public function testSelectShard()
63
    {
64
        $shardId = 10;
65
        $conn = $this->createConnectionMock();
66
        $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(array('shardChoser' => $this->createPassthroughShardChoser())));
67
        $conn->expects($this->at(1))->method('connect')->with($this->equalTo($shardId));
68
69
        $shardManager = new PoolingShardManager($conn);
70
        $shardManager->selectShard($shardId);
71
72
        self::assertEquals($shardId, $shardManager->getCurrentDistributionValue());
73
    }
74
75
    public function testGetShards()
76
    {
77
        $conn = $this->createConnectionMock();
78
        $conn->expects($this->any())->method('getParams')->will(
79
            $this->returnValue(
80
                array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
81
            )
82
        );
83
84
        $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
85
        $shards = $shardManager->getShards();
86
87
        self::assertEquals(array(array('id' => 1), array('id' => 2)), $shards);
88
    }
89
90 View Code Duplication
    public function testQueryAll()
91
    {
92
        $sql = "SELECT * FROM table";
93
        $params = array(1);
94
        $types = array(1);
95
96
        $conn = $this->createConnectionMock();
97
        $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(
98
            array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
99
        ));
100
        $conn->expects($this->at(1))->method('getParams')->will($this->returnValue(
101
            array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
102
        ));
103
        $conn->expects($this->at(2))->method('connect')->with($this->equalTo(1));
104
        $conn->expects($this->at(3))
105
             ->method('fetchAll')
106
             ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
107
             ->will($this->returnValue(array( array('id' => 1) ) ));
108
        $conn->expects($this->at(4))->method('connect')->with($this->equalTo(2));
109
        $conn->expects($this->at(5))
110
             ->method('fetchAll')
111
             ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
112
             ->will($this->returnValue(array( array('id' => 2) ) ));
113
114
        $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
115
        $result = $shardManager->queryAll($sql, $params, $types);
116
117
        self::assertEquals(array(array('id' => 1), array('id' => 2)), $result);
118
    }
119
120 View Code Duplication
    public function testQueryAllWithStaticShardChoser()
0 ignored issues
show
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...
121
    {
122
        $sql = "SELECT * FROM table";
123
        $params = array(1);
124
        $types = array(1);
125
126
        $conn = $this->createConnectionMock();
127
        $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(
128
            array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createStaticShardChoser())
129
        ));
130
        $conn->expects($this->at(1))->method('getParams')->will($this->returnValue(
131
            array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createStaticShardChoser())
132
        ));
133
        $conn->expects($this->at(2))->method('connect')->with($this->equalTo(1));
0 ignored issues
show
$this->equalTo(1) of type PHPUnit\Framework\Constraint\IsEqual is incompatible with the type array expected by parameter $arguments of PHPUnit_Framework_MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
        $conn->expects($this->at(2))->method('connect')->with(/** @scrutinizer ignore-type */ $this->equalTo(1));
Loading history...
134
        $conn->expects($this->at(3))
135
            ->method('fetchAll')
136
            ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
137
            ->will($this->returnValue(array( array('id' => 1) ) ));
138
        $conn->expects($this->at(4))->method('connect')->with($this->equalTo(2));
139
        $conn->expects($this->at(5))
140
            ->method('fetchAll')
141
            ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
142
            ->will($this->returnValue(array( array('id' => 2) ) ));
143
144
        $shardManager = new PoolingShardManager($conn, $this->createStaticShardChoser());
0 ignored issues
show
The call to Doctrine\DBAL\Sharding\P...dManager::__construct() has too many arguments starting with $this->createStaticShardChoser(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        $shardManager = /** @scrutinizer ignore-call */ new PoolingShardManager($conn, $this->createStaticShardChoser());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
145
        $result = $shardManager->queryAll($sql, $params, $types);
146
147
        self::assertEquals(array(array('id' => 1), array('id' => 2)), $result);
148
    }
149
}
150
151