Skip to content

Commit c75d9cd

Browse files
committed
Fix method chaining
1 parent e7e9d7b commit c75d9cd

File tree

5 files changed

+48
-88
lines changed

5 files changed

+48
-88
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"autoload-dev": {
2828
"classmap": [
2929
"tests/TestCase.php",
30-
"tests/ModelTest.php"
30+
"tests/ModelTest.php",
31+
"tests/DynamoDbModelTest.php"
3132
]
3233
}
3334
}

src/DynamoDbModel.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ abstract class DynamoDbModel extends Model
5757

5858
protected $compositeKey = [];
5959

60+
/**
61+
* @var DynamoDbModel
62+
*/
63+
protected static $instance;
64+
6065
public function __construct(array $attributes = [], DynamoDbClientService $dynamoDb = null)
6166
{
6267
$this->bootIfNotBooted();
@@ -70,11 +75,17 @@ public function __construct(array $attributes = [], DynamoDbClientService $dynam
7075
}
7176

7277
$this->setupDynamoDb();
78+
79+
static::$instance = $this;
7380
}
7481

7582
protected static function getInstance()
7683
{
77-
return new static();
84+
if (is_null(static::$instance)) {
85+
static::$instance = new static;
86+
}
87+
88+
return static::$instance;
7889
}
7990

8091
protected function setupDynamoDb()

tests/DynamoDbCompositeModelTest.php

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class DynamoDbCompositeModelTest extends ModelTest
3+
class DynamoDbCompositeModelTest extends DynamoDbModelTest
44
{
55
protected function getTestModel()
66
{
@@ -45,16 +45,6 @@ public function testFindRecord()
4545
$this->assertEquals($seedName, $item->name);
4646
}
4747

48-
public function testGetAllRecords()
49-
{
50-
$this->seed();
51-
$this->seed();
52-
53-
$items = $this->testModel->all()->toArray();
54-
55-
$this->assertEquals(2, count($items));
56-
}
57-
5848
public function testUpdateRecord()
5949
{
6050
$seed = $this->seed();
@@ -126,81 +116,6 @@ public function testDeleteRecord()
126116
$this->assertArrayNotHasKey('Item', $record);
127117
}
128118

129-
public function testGetAllRecordsWithEqualCondition()
130-
{
131-
$this->seed(['count' => ['N' => 10]]);
132-
$this->seed(['count' => ['N' => 10]]);
133-
$this->seed(['count' => ['N' => 10]]);
134-
135-
$items = $this->testModel->where('count', 10)->get()->toArray();
136-
137-
$this->assertEquals(3, count($items));
138-
}
139-
140-
public function testGetAllRecordsWithNonEqualCondition()
141-
{
142-
$this->seed(['count' => ['N' => 10]]);
143-
$this->seed(['count' => ['N' => 11]]);
144-
$this->seed(['count' => ['N' => 11]]);
145-
146-
$items = $this->testModel->where('count', '!=', 10)->get()->toArray();
147-
148-
$this->assertEquals(2, count($items));
149-
}
150-
151-
public function testGetAllRecordsWithGTCondition()
152-
{
153-
$this->seed(['count' => ['N' => 10]]);
154-
$this->seed(['count' => ['N' => 11]]);
155-
$this->seed(['count' => ['N' => 11]]);
156-
157-
$items = $this->testModel->where('count', '>', 10)->get()->toArray();
158-
159-
$this->assertEquals(2, count($items));
160-
}
161-
162-
public function testGetAllRecordsWithLTCondition()
163-
{
164-
$this->seed(['count' => ['N' => 10]]);
165-
$this->seed(['count' => ['N' => 9]]);
166-
$this->seed(['count' => ['N' => 11]]);
167-
168-
$items = $this->testModel->where('count', '<', 10)->get()->toArray();
169-
170-
$this->assertEquals(1, count($items));
171-
}
172-
173-
public function testGetAllRecordsWithGECondition()
174-
{
175-
$this->seed(['count' => ['N' => 10]]);
176-
$this->seed(['count' => ['N' => 9]]);
177-
$this->seed(['count' => ['N' => 11]]);
178-
179-
$items = $this->testModel->where('count', '>=', 10)->get()->toArray();
180-
181-
$this->assertEquals(2, count($items));
182-
}
183-
184-
public function testGetAllRecordsWithLECondition()
185-
{
186-
$this->seed(['count' => ['N' => 10]]);
187-
$this->seed(['count' => ['N' => 9]]);
188-
$this->seed(['count' => ['N' => 11]]);
189-
190-
$items = $this->testModel->where('count', '<=', 10)->get()->toArray();
191-
192-
$this->assertEquals(2, count($items));
193-
}
194-
195-
public function testGetFirstRecord()
196-
{
197-
$firstItem = $this->seed();
198-
199-
$items = $this->testModel->first();
200-
201-
$this->assertEquals(array_get($firstItem, 'id.S'), $items->id);
202-
}
203-
204119
protected function seed($attributes = [])
205120
{
206121
$item = [

tests/DynamoDbModelTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,45 @@ public function testGetFirstRecord()
191191
$this->assertEquals(array_get($firstItem, 'id.S'), $items->id);
192192
}
193193

194+
public function testChainedMethods()
195+
{
196+
$firstItem = $this->seed([
197+
'name' => ['S' => 'Same Name'],
198+
'description' => ['S' => 'First Description'],
199+
]);
200+
201+
$secondItem = $this->seed([
202+
'name' => ['S' => 'Same Name'],
203+
'description' => ['S' => 'Second Description'],
204+
]);
205+
206+
$foundItems = $this->testModel
207+
->where('name', $firstItem['name']['S'])
208+
->where('description', $firstItem['description']['S'])
209+
->all();
210+
211+
$this->assertEquals(1, $foundItems->count());
212+
213+
$this->assertEquals($this->testModel->unmarshalItem($firstItem), $foundItems->first()->toArray());
214+
215+
$foundItems = $this->testModel
216+
->where('name', $secondItem['name']['S'])
217+
->where('description', $secondItem['description']['S'])
218+
->all();
219+
220+
$this->assertEquals(1, $foundItems->count());
221+
222+
$this->assertEquals($this->testModel->unmarshalItem($secondItem), $foundItems->first()->toArray());
223+
}
224+
194225
protected function seed($attributes = [])
195226
{
196227
$item = [
197228
'id' => ['S' => str_random(36)],
198229
'name' => ['S' => str_random(36)],
199230
'description' => ['S' => str_random(256)],
200231
'count' => ['N' => rand()],
232+
'author' => ['S' => str_random()],
201233
];
202234

203235
$item = array_merge($item, $attributes);

tests/ModelTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected function bindDynamoDbClientInstance()
4848
'version' => '2012-08-10',
4949
'endpoint' => 'http://localhost:3000',
5050
];
51+
5152
$this->dynamoDb = new DynamoDbClientService($config, new Marshaler($marshalerOptions),
5253
new EmptyAttributeFilter);
5354

0 commit comments

Comments
 (0)