Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Sharding / PoolingShardManagerTest.php
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->getMock('Doctrine\DBAL\Sharding\PoolingShardConnection', array('connect', 'getParams', 'fetchAll'), array(), '', false);
28     }
29
30     private function createPassthroughShardChoser()
31     {
32         $mock = $this->getMock('Doctrine\DBAL\Sharding\ShardChoser\ShardChoser');
33         $mock->expects($this->any())
34              ->method('pickShard')
35              ->will($this->returnCallback(function($value) { return $value; }));
36         return $mock;
37     }
38
39     public function testSelectGlobal()
40     {
41         $conn = $this->createConnectionMock();
42         $conn->expects($this->once())->method('connect')->with($this->equalTo(0));
43
44         $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
45         $shardManager->selectGlobal();
46
47         $this->assertNull($shardManager->getCurrentDistributionValue());
48     }
49
50     public function testSelectShard()
51     {
52         $shardId = 10;
53         $conn = $this->createConnectionMock();
54         $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(array('shardChoser' => $this->createPassthroughShardChoser())));
55         $conn->expects($this->at(1))->method('connect')->with($this->equalTo($shardId));
56
57         $shardManager = new PoolingShardManager($conn);
58         $shardManager->selectShard($shardId);
59
60         $this->assertEquals($shardId, $shardManager->getCurrentDistributionValue());
61     }
62
63     public function testGetShards()
64     {
65         $conn = $this->createConnectionMock();
66         $conn->expects($this->any())->method('getParams')->will(
67             $this->returnValue(
68                 array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
69             )
70         );
71
72         $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
73         $shards = $shardManager->getShards();
74
75         $this->assertEquals(array(array('id' => 1), array('id' => 2)), $shards);
76     }
77
78     public function testQueryAll()
79     {
80         $sql = "SELECT * FROM table";
81         $params = array(1);
82         $types = array(1);
83
84         $conn = $this->createConnectionMock();
85         $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(
86             array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
87         ));
88         $conn->expects($this->at(1))->method('getParams')->will($this->returnValue(
89             array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
90         ));
91         $conn->expects($this->at(2))->method('connect')->with($this->equalTo(1));
92         $conn->expects($this->at(3))
93              ->method('fetchAll')
94              ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
95              ->will($this->returnValue(array( array('id' => 1) ) ));
96         $conn->expects($this->at(4))->method('connect')->with($this->equalTo(2));
97         $conn->expects($this->at(5))
98              ->method('fetchAll')
99              ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
100              ->will($this->returnValue(array( array('id' => 2) ) ));
101
102         $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
103         $result = $shardManager->queryAll($sql, $params, $types);
104
105         $this->assertEquals(array(array('id' => 1), array('id' => 2)), $result);
106     }
107 }
108