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() |
|||
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; })); |
||||
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)); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
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)); |
||||
0 ignored issues
–
show
$this->equalTo($shardId) 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
Loading history...
|
|||||
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)); |
||||
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
Loading history...
|
|||||
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() |
|||
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
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()); |
||||
145 | $result = $shardManager->queryAll($sql, $params, $types); |
||||
146 | |||||
147 | self::assertEquals(array(array('id' => 1), array('id' => 2)), $result); |
||||
148 | } |
||||
149 | } |
||||
150 | |||||
151 |