Skip to content

Commit ee3963d

Browse files
committed
Add more missing tests
1 parent 4534722 commit ee3963d

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

src/Kitar/Dynamodb/Connection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kitar\Dynamodb;
44

55
use Aws\Sdk as AwsSdk;
6+
use Aws\DynamoDb\DynamoDbClient;
67
use Illuminate\Database\Connection as BaseConnection;
78

89
class Connection extends BaseConnection
@@ -71,6 +72,16 @@ public function createClient(array $config)
7172
return $sdk->createDynamoDb();
7273
}
7374

75+
/**
76+
* Set the DynamoDB client.
77+
* @param DynamoDbClient $client
78+
* @return void
79+
*/
80+
public function setClient(DynamoDbClient $client)
81+
{
82+
$this->client = $client;
83+
}
84+
7485
/**
7586
* @inheritdoc
7687
*/

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
361361
// assume that the developer is just short-cutting the '=' operators and
362362
// we will set the operators to '=' and set the values appropriately.
363363
if ($this->invalidOperator($operator)) {
364+
$operator = $this->expression_attributes->addValue($operator);
364365
[$value, $operator] = [$operator, '='];
365366
}
366367

tests/ConnectionTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22

33
namespace Kitar\Dynamodb\Tests;
44

5+
use Mockery as m;
56
use Kitar\Dynamodb\Connection;
67
use Kitar\Dynamodb\Query\Builder;
78
use Aws\DynamoDb\DynamoDbClient;
89
use PHPUnit\Framework\TestCase;
10+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
911

1012
class ConnectionTest extends TestCase
1113
{
14+
use MockeryPHPUnitIntegration;
15+
1216
protected $connection;
1317

1418
protected function setUp() :void
1519
{
1620
$this->connection = new Connection([]);
1721
}
1822

23+
protected function tearDown() :void
24+
{
25+
m::close();
26+
}
27+
1928
/** @test */
2029
public function it_creates_connection()
2130
{
@@ -57,4 +66,34 @@ public function it_returns_query_builder_instance()
5766

5867
$this->assertEquals('test', $query->from);
5968
}
69+
70+
/** @test */
71+
public function it_can_call_client_query()
72+
{
73+
$client = m::mock(DynamoDbClient::class);
74+
$client->shouldReceive('query')->with([
75+
'TableName' => 'User'
76+
]);
77+
78+
$connection = new Connection([]);
79+
$connection->setClient($client);
80+
$connection->clientQuery([
81+
'TableName' => 'User'
82+
]);
83+
}
84+
85+
/** @test */
86+
public function it_can_forward_call_to_dynamodb_client()
87+
{
88+
$client = m::mock(DynamoDbClient::class);
89+
$client->shouldReceive('getItem')->with([
90+
'TableName' => 'User'
91+
]);
92+
93+
$connection = new Connection([]);
94+
$connection->setClient($client);
95+
$connection->getItem([
96+
'TableName' => 'User'
97+
]);
98+
}
6099
}

tests/Helpers/NumberIteratorTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Kitar\Dynamodb\Tests;
4+
5+
use Kitar\Dynamodb\Helpers\NumberIterator;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class NumberIteratorTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_can_use_prefix()
12+
{
13+
$iterator = new NumberIterator(1, '#');
14+
15+
$this->assertEquals('#1', $iterator->current());
16+
}
17+
18+
/** @test */
19+
public function it_can_increment()
20+
{
21+
$iterator = new NumberIterator(1, ':');
22+
23+
$iterator->next();
24+
25+
$this->assertEquals(':2', $iterator->current());
26+
27+
$iterator->next();
28+
29+
$this->assertEquals(':3', $iterator->current());
30+
}
31+
32+
/** @test */
33+
public function key_returns_number_without_prefix()
34+
{
35+
$iterator = new NumberIterator(1, '#');
36+
37+
$iterator->next();
38+
39+
$this->assertEquals(2, $iterator->key());
40+
}
41+
42+
/** @test */
43+
public function it_can_rewind()
44+
{
45+
$iterator = new NumberIterator(1, '#');
46+
47+
$iterator->next();
48+
$iterator->next();
49+
$iterator->next();
50+
$iterator->rewind();
51+
52+
$this->assertEquals('#1', $iterator->current());
53+
}
54+
55+
/** @test */
56+
public function it_is_always_valid()
57+
{
58+
$iterator = new NumberIterator(1);
59+
60+
$this->assertTrue($iterator->valid());
61+
}
62+
}

tests/Query/BuilderTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kitar\Dynamodb\Tests\Query;
44

55
use Aws\Result;
6+
use BadMethodCallException;
67
use Mockery as m;
78
use Kitar\Dynamodb\Connection;
89
use Kitar\Dynamodb\Model\Model;
@@ -179,6 +180,28 @@ public function it_can_process_filter()
179180
$this->assertEquals($params, $query['params']);
180181
}
181182

183+
/** @test */
184+
public function it_can_process_filter_with_short_syntax()
185+
{
186+
$params = [
187+
'TableName' => 'Thread',
188+
'FilterExpression' => '#1 = :1',
189+
'ExpressionAttributeNames' => [
190+
'#1' => 'ForumName'
191+
],
192+
'ExpressionAttributeValues' => [
193+
':1' => [
194+
'S' => 'Amazon DynamoDB'
195+
]
196+
]
197+
];
198+
$query = $this->newQuery('Thread')
199+
->filter('ForumName', 'Amazon DynamoDB')
200+
->scan();
201+
202+
$this->assertEquals($params, $query['params']);
203+
}
204+
182205
/** @test */
183206
public function it_can_process_condition()
184207
{
@@ -780,4 +803,14 @@ public function it_can_process_process_with_no_processor()
780803
'Subject' => 'Laravel Thread 1'
781804
]);
782805
}
806+
807+
/** @test */
808+
public function it_can_forward_call_to_unknown_method()
809+
{
810+
$query = $this->newQuery('Thread');
811+
812+
$this->expectException(BadMethodCallException::class);
813+
814+
$query->filterNotIn(['foo', 'bar']);
815+
}
783816
}

0 commit comments

Comments
 (0)