Skip to content

Commit

Permalink
Add tests for #40
Browse files Browse the repository at this point in the history
  • Loading branch information
kitar committed Dec 15, 2023
1 parent 89efb21 commit cdcfef6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
37 changes: 37 additions & 0 deletions tests/Helpers/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Kitar\Dynamodb\Tests\Helpers;

use Kitar\Dynamodb\Helpers\Collection;
use PHPUnit\Framework\TestCase;

class CollectionTest extends TestCase
{
/** @test */
public function it_can_set_and_get_meta()
{
$items = new Collection([]);

$this->assertNull($items->getMeta());

$meta = ['LastEvaluatedKey' => ['id' => ['S' => '1']]];

$items->setMeta($meta);

$this->assertSame($meta, $items->getMeta());
}

/** @test */
public function it_can_get_last_evaluated_key()
{
$items = new Collection([]);

$this->assertNull($items->getLastEvaluatedKey());

$lastEvaluatedKey = ['id' => ['S' => '1']];

$items->setMeta(['LastEvaluatedKey' => $lastEvaluatedKey]);

$this->assertSame($lastEvaluatedKey, $items->getLastEvaluatedKey());
}
}
39 changes: 37 additions & 2 deletions tests/Model/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\ConnectionResolver;
use Kitar\Dynamodb\Model\KeyMissingException;
use BadMethodCallException;
use Kitar\Dynamodb\Helpers\Collection;

class ModelTest extends TestCase
{
Expand Down Expand Up @@ -36,6 +37,15 @@ protected function newConnectionMock()
return $connection;
}

protected function sampleAwsResult()
{
return new Result([
'Items' => [],
'LastEvaluatedKey' => ['id' => ['S' => '1']],
'@metadata' => ['statusCode' => 200],
]);
}

protected function sampleAwsResultEmpty()
{
return new Result([
Expand Down Expand Up @@ -400,14 +410,39 @@ public function it_can_process_all()
];

$return = new Result([
'Items' => []
'Items' => [
['name' => ['S' => 'User 1']],
['name' => ['S' => 'User 2']]
]
]);

$connection = $this->newConnectionMock();
$connection->shouldReceive('scan')->with($params)->andReturn($return)->once();
$this->setConnectionResolver($connection);

UserA::all();
$res = UserA::all();

$this->assertSame(2, $res->count());
$this->assertInstanceOf(Collection::class, $res);
$this->assertInstanceOf(UserA::class, $res->first());
$this->assertSame('User 1', $res->first()->name);
$this->assertNull($res->getLastEvaluatedKey());
}

/** @test */
public function it_can_get_last_evaluated_key()
{
$params = [
'TableName' => 'User'
];

$connection = $this->newConnectionMock();
$connection->shouldReceive('scan')->with($params)->andReturn($this->sampleAwsResult())->once();
$this->setConnectionResolver($connection);

$res = UserA::all();

$this->assertSame(['id' => ['S' => '1']], $res->getLastEvaluatedKey());
}

/** @test */
Expand Down

0 comments on commit cdcfef6

Please sign in to comment.