Skip to content

Commit cbfeb6f

Browse files
committed
Add tests for Query Processor
1 parent 5366430 commit cbfeb6f

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

tests/Query/BuilderTest.php

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

55
use Kitar\Dynamodb\Connection;
6+
use Kitar\Dynamodb\Model\Model;
67
use PHPUnit\Framework\TestCase;
78

9+
class Product extends Model
10+
{
11+
}
12+
813
class BuilderTest extends TestCase
914
{
1015
protected $connection;
@@ -130,6 +135,15 @@ public function it_can_set_consistent_read()
130135
$this->assertEquals($params, $query['params']);
131136
}
132137

138+
/** @test */
139+
public function it_can_set_model_class()
140+
{
141+
$query = $this->newQuery('ProductCatalog')
142+
->usingModel(Product::class);
143+
144+
$this->assertEquals(Product::class, $query->model_class);
145+
}
146+
133147
/** @test */
134148
public function it_can_process_filter()
135149
{

tests/Query/ProcessorTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Kitar\Dynamodb\Tests\Query;
4+
5+
use Aws\Result;
6+
use Kitar\Dynamodb\Model\Model;
7+
use Kitar\Dynamodb\Query\Processor;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class User extends Model
11+
{
12+
}
13+
14+
class ProcessorTest extends TestCase
15+
{
16+
protected $processor;
17+
18+
protected $mocks = [
19+
'single_item_result' => '{"Item":{"Threads":{"N":"2"},"Category":{"S":"Amazon Web Services"},"Messages":{"N":"4"},"Views":{"N":"1000"},"Name":{"S":"Amazon DynamoDB"}},"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
20+
'single_item_processed' => '{"Item":{"Threads":2,"Category":"Amazon Web Services","Messages":4,"Views":1000,"Name":"Amazon DynamoDB"},"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
21+
'single_item_empty_result' => '{"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
22+
'single_item_empty_processed' => '{"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
23+
'multiple_items_result' => '{"Items":[{"Category":{"S":"Amazon Web Services"},"Name":{"S":"Amazon S3"}},{"Threads":{"N":"2"},"Category":{"S":"Amazon Web Services"},"Messages":{"N":"4"},"Views":{"N":"1000"},"Name":{"S":"Amazon DynamoDB"}}],"Count":2,"ScannedCount":2,"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
24+
'multiple_items_processed' => '{"Items":[{"Category":"Amazon Web Services","Name":"Amazon S3"},{"Threads":2,"Category":"Amazon Web Services","Messages":4,"Views":1000,"Name":"Amazon DynamoDB"}],"Count":2,"ScannedCount":2,"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
25+
'multiple_items_empty_result' => '{"Items":[],"Count":0,"ScannedCount":2,"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
26+
'multiple_items_empty_processed' => '{"Items":[],"Count":0,"ScannedCount":2,"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}'
27+
];
28+
29+
protected function setUp() :void
30+
{
31+
$this->processor = new Processor;
32+
}
33+
34+
/** @test */
35+
public function it_can_process_single_item_result()
36+
{
37+
$expected = json_decode($this->mocks['single_item_processed'], true);
38+
39+
$awsResult = new Result(json_decode($this->mocks['single_item_result'], true));
40+
41+
$result = $this->processor->processSingleItem($awsResult, null);
42+
43+
$this->assertEquals($expected, $result);
44+
}
45+
46+
/** @test */
47+
public function it_can_process_single_item_empty_result()
48+
{
49+
$expected = json_decode($this->mocks['single_item_empty_processed'], true);
50+
51+
$awsResult = new Result(json_decode($this->mocks['single_item_empty_result'], true));
52+
53+
$result = $this->processor->processSingleItem($awsResult, null);
54+
55+
$this->assertEquals($expected, $result);
56+
}
57+
58+
/** @test */
59+
public function it_can_process_multiple_items_result()
60+
{
61+
$expected = json_decode($this->mocks['multiple_items_processed'], true);
62+
63+
$awsResult = new Result(json_decode($this->mocks['multiple_items_result'], true));
64+
65+
$result = $this->processor->processMultipleItems($awsResult, null);
66+
67+
$this->assertEquals($expected, $result);
68+
}
69+
70+
/** @test */
71+
public function it_can_process_multiple_items_empty_result()
72+
{
73+
$expected = json_decode($this->mocks['multiple_items_empty_processed'], true);
74+
75+
$awsResult = new Result(json_decode($this->mocks['multiple_items_empty_result'], true));
76+
77+
$result = $this->processor->processMultipleItems($awsResult, null);
78+
79+
$this->assertEquals($expected, $result);
80+
}
81+
82+
/** @test */
83+
public function it_can_convert_single_result_to_model_instance()
84+
{
85+
$awsResult = new Result(json_decode($this->mocks['single_item_result'], true));
86+
87+
$result = $this->processor->processSingleItem($awsResult, User::class);
88+
89+
$item = $result['Item'];
90+
91+
$this->assertEquals(User::class, get_class($item));
92+
$this->assertEquals([
93+
'Threads' => 2,
94+
'Category' => 'Amazon Web Services',
95+
'Messages' => 4,
96+
'Views' => 1000,
97+
'Name' => 'Amazon DynamoDB'
98+
], $item->toArray());
99+
}
100+
101+
/** @test */
102+
public function it_can_convert_multiple_results_to_model_instance()
103+
{
104+
$awsResult = new Result(json_decode($this->mocks['multiple_items_result'], true));
105+
106+
$result = $this->processor->processMultipleItems($awsResult, User::class);
107+
108+
$item = $result['Items'][0];
109+
110+
$this->assertEquals(User::class, get_class($item));
111+
$this->assertEquals([
112+
'Category' => 'Amazon Web Services',
113+
'Name' => 'Amazon S3'
114+
], $item->toArray());
115+
}
116+
}

0 commit comments

Comments
 (0)