Skip to content

Commit 502d101

Browse files
committed
Add find and all
1 parent fac7a95 commit 502d101

File tree

5 files changed

+245
-10
lines changed

5 files changed

+245
-10
lines changed

src/Kitar/Dynamodb/Model/AuthUserProvider.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,7 @@ public function __construct(Authenticatable $model)
2323
*/
2424
public function retrieveById($identifier)
2525
{
26-
$identifierName = $this->model->getAuthIdentifierName();
27-
28-
$this->model->$identifierName = $identifier;
29-
30-
$user = $this->model->getItem(
31-
$this->model->getKey()
32-
);
33-
34-
return $user;
26+
return $this->model->find($identifier);
3527
}
3628

3729
/**

src/Kitar/Dynamodb/Model/Model.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,38 @@ public function newQuery()
9797
return $this->getConnection()->table($this->table)->usingModel(static::class);
9898
}
9999

100+
/**
101+
* Find a model by its primary (partition) key or key array.
102+
*
103+
* @param string|array $key
104+
* @return static|null
105+
*/
106+
public static function find($key)
107+
{
108+
if (empty($key)) {
109+
throw new KeyMissingException("Primary (Partition) key has no value.");
110+
}
111+
112+
if (is_string($key)) {
113+
$model = new static;
114+
$model->setAttribute($model->getKeyName(), $key);
115+
$key = $model->getKey();
116+
}
117+
118+
return static::getItem($key);
119+
}
120+
121+
/**
122+
* Get all of the models from the database.
123+
*
124+
* @param array $columns
125+
* @return Illuminate\Database\Eloquent\Collection
126+
*/
127+
public static function all($columns = [])
128+
{
129+
return static::scan($columns);
130+
}
131+
100132
/**
101133
* Save the model to the database.
102134
*

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,15 @@ public function query()
308308
/**
309309
* Scan.
310310
*
311+
* @param array $columns
311312
* @return Illuminate\Support\Collection|array
312313
*/
313-
public function scan()
314+
public function scan($columns = [])
314315
{
316+
if (! empty($columns)) {
317+
$this->select($columns);
318+
}
319+
315320
return $this->process('scan', 'processMultipleItems');
316321
}
317322

tests/Model/ModelTest.php

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kitar\Dynamodb\Tests\Model;
44

5+
use Aws\Result;
56
use PHPUnit\Framework\TestCase;
67
use Mockery as m;
78
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
@@ -213,6 +214,189 @@ public function get_key_raise_exception_if_primary_and_sort_key_is_missing()
213214
$user->getKey();
214215
}
215216

217+
/** @test */
218+
public function it_can_process_find()
219+
{
220+
$params = [
221+
'TableName' => 'User',
222+
'Key' => [
223+
'partition' => [
224+
'S' => 'p'
225+
]
226+
]
227+
];
228+
229+
$return = new Result([
230+
'Item' => [
231+
'partition' => [
232+
'S' => 'p'
233+
]
234+
]
235+
]);
236+
237+
$connection = $this->newConnectionMock();
238+
$connection->shouldReceive('getItem')->with($params)->andReturn($return);
239+
$this->setConnectionResolver($connection);
240+
241+
$user = UserA::find('p');
242+
$this->assertInstanceOf(UserA::class, $user);
243+
}
244+
245+
/** @test */
246+
public function it_can_process_find_with_primary_key_and_sort_key()
247+
{
248+
$params = [
249+
'TableName' => 'User',
250+
'Key' => [
251+
'partition' => [
252+
'S' => 'p'
253+
],
254+
'sort' => [
255+
'S' => 's'
256+
]
257+
]
258+
];
259+
260+
$return = new Result([
261+
'Item' => [
262+
'partition' => [
263+
'S' => 'p'
264+
],
265+
'sort' => [
266+
'S' => 's'
267+
]
268+
]
269+
]);
270+
271+
$connection = $this->newConnectionMock();
272+
$connection->shouldReceive('getItem')->with($params)->andReturn($return);
273+
$this->setConnectionResolver($connection);
274+
275+
$user = UserB::find(['partition' => 'p', 'sort' => 's']);
276+
$this->assertInstanceOf(UserB::class, $user);
277+
}
278+
279+
/** @test */
280+
public function it_can_process_find_with_primary_key_and_default_sort_key()
281+
{
282+
$params = [
283+
'TableName' => 'User',
284+
'Key' => [
285+
'partition' => [
286+
'S' => 'p'
287+
],
288+
'sort' => [
289+
'S' => 'sort_default'
290+
]
291+
]
292+
];
293+
294+
$return = new Result([
295+
'Item' => [
296+
'partition' => [
297+
'S' => 'p'
298+
],
299+
'sort' => [
300+
'S' => 'sort_default'
301+
]
302+
]
303+
]);
304+
305+
$connection = $this->newConnectionMock();
306+
$connection->shouldReceive('getItem')->with($params)->andReturn($return);
307+
$this->setConnectionResolver($connection);
308+
309+
$user = UserC::find('p');
310+
$this->assertInstanceOf(UserC::class, $user);
311+
}
312+
313+
/** @test */
314+
public function it_can_process_find_with_overrided_sort_key()
315+
{
316+
$params = [
317+
'TableName' => 'User',
318+
'Key' => [
319+
'partition' => [
320+
'S' => 'p'
321+
],
322+
'sort' => [
323+
'S' => 's'
324+
]
325+
]
326+
];
327+
328+
$return = new Result([
329+
'Item' => [
330+
'partition' => [
331+
'S' => 'p'
332+
],
333+
'sort' => [
334+
'S' => 's'
335+
]
336+
]
337+
]);
338+
339+
$connection = $this->newConnectionMock();
340+
$connection->shouldReceive('getItem')->with($params)->andReturn($return);
341+
$this->setConnectionResolver($connection);
342+
343+
$user = UserC::find([
344+
'partition' => 'p',
345+
'sort' => 's'
346+
]);
347+
$this->assertInstanceOf(UserC::class, $user);
348+
}
349+
350+
/** @test */
351+
public function it_can_process_find_with_keys_not_exists()
352+
{
353+
$params = [
354+
'TableName' => 'User',
355+
'Key' => [
356+
'partition' => [
357+
'S' => 'foo'
358+
]
359+
]
360+
];
361+
362+
$return = new Result([]);
363+
364+
$connection = $this->newConnectionMock();
365+
$connection->shouldReceive('getItem')->with($params)->andReturn($return);
366+
$this->setConnectionResolver($connection);
367+
368+
$user = UserA::find('foo');
369+
$this->assertNull($user);
370+
}
371+
372+
/** @test */
373+
public function it_cannot_process_find_with_empty_argument()
374+
{
375+
$this->expectException(KeyMissingException::class);
376+
UserA::find(null);
377+
378+
$this->expectException(KeyMissingException::class);
379+
UserA::find('');
380+
}
381+
382+
/** @test */
383+
public function it_can_process_all()
384+
{
385+
$params = [
386+
'TableName' => 'User'
387+
];
388+
389+
$return = new Result([
390+
'Items' => []
391+
]);
392+
393+
$connection = $this->newConnectionMock();
394+
$connection->shouldReceive('scan')->with($params)->andReturn($return);
395+
$this->setConnectionResolver($connection);
396+
397+
UserA::all();
398+
}
399+
216400
/** @test */
217401
public function it_can_save_new_instance()
218402
{

tests/Query/BuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,28 @@ public function it_can_process_scan()
766766
$this->assertEquals($processor, $query['processor']);
767767
}
768768

769+
/** @test */
770+
public function it_can_process_scan_with_columns_specified()
771+
{
772+
$method = 'scan';
773+
$params = [
774+
'TableName' => 'Forum',
775+
'ProjectionExpression' => '#1, #2',
776+
'ExpressionAttributeNames' => [
777+
'#1' => 'foo',
778+
'#2' => 'bar'
779+
]
780+
];
781+
$processor = 'processMultipleItems';
782+
783+
$query = $this->newQuery('Forum')
784+
->scan(['foo', 'bar']);
785+
786+
$this->assertEquals($method, $query['method']);
787+
$this->assertEquals($params, $query['params']);
788+
$this->assertEquals($processor, $query['processor']);
789+
}
790+
769791
/** @test */
770792
public function it_can_process_process()
771793
{

0 commit comments

Comments
 (0)