/
ShardKeyTest.php
137 lines (111 loc) · 3.51 KB
/
ShardKeyTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
namespace Doctrine\ODM\MongoDB\Tests\Functional;
use Doctrine\ODM\MongoDB\APM\CommandLogger;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
use Documents\Sharded\ShardedOne;
use MongoDB\BSON\ObjectId;
use function end;
use function get_class;
/**
* @group sharding
*/
class ShardKeyTest extends BaseTest
{
/** @var CommandLogger */
private $logger;
public function setUp()
{
parent::setUp();
$class = ShardedOne::class;
$this->skipTestIfNotSharded($class);
$schemaManager = $this->dm->getSchemaManager();
$schemaManager->ensureDocumentSharding($class);
$this->logger = new CommandLogger();
$this->logger->register();
}
public function tearDown()
{
$this->logger->unregister();
return parent::tearDown();
}
public function testUpdateAfterSave()
{
$o = new ShardedOne();
$this->dm->persist($o);
$this->dm->flush();
/** @var ShardedOne $o */
$o = $this->dm->find(get_class($o), $o->id);
$o->title = 'test2';
$this->dm->flush();
$queries = $this->logger->getAll();
$lastQuery = end($queries);
$this->assertSame('update', $lastQuery->getCommandName());
$command = $lastQuery->getCommand();
$this->assertCount(1, $command->updates);
$this->assertEquals($o->key, $command->updates[0]->q->k);
}
public function testUpsert()
{
$o = new ShardedOne();
$o->id = new ObjectId();
$this->dm->persist($o);
$this->dm->flush();
$queries = $this->logger->getAll();
$lastQuery = end($queries);
$this->assertSame('update', $lastQuery->getCommandName());
$command = $lastQuery->getCommand();
$this->assertCount(1, $command->updates);
$this->assertEquals($o->key, $command->updates[0]->q->k);
$this->assertTrue($command->updates[0]->upsert);
}
public function testRemove()
{
$o = new ShardedOne();
$this->dm->persist($o);
$this->dm->flush();
$this->dm->remove($o);
$this->dm->flush();
$queries = $this->logger->getAll();
$lastQuery = end($queries);
$this->assertSame('delete', $lastQuery->getCommandName());
$command = $lastQuery->getCommand();
$this->assertCount(1, $command->deletes);
$this->assertEquals($o->key, $command->deletes[0]->q->k);
}
public function testRefresh()
{
$o = new ShardedOne();
$this->dm->persist($o);
$this->dm->flush();
$this->dm->refresh($o);
$queries = $this->logger->getAll();
$lastQuery = end($queries);
$this->assertSame('find', $lastQuery->getCommandName());
$command = $lastQuery->getCommand();
$this->assertSame(1, $command->limit);
$this->assertEquals($o->key, $command->filter->k);
}
/**
* @expectedException \Doctrine\ODM\MongoDB\MongoDBException
*/
public function testUpdateWithShardKeyChangeException()
{
$o = new ShardedOne();
$this->dm->persist($o);
$this->dm->flush();
$o->key = 'testing2';
$this->dm->flush();
}
/**
* @expectedException \Doctrine\ODM\MongoDB\MongoDBException
*/
public function testUpdateWithUpsertTrue()
{
$o = new ShardedOne();
$this->dm->persist($o);
$this->dm->flush();
$o->key = 'testing2';
$this->dm->flush(['upsert' => true]);
}
}