Completed
Push — master ( 005d83...940387 )
by Guillermo A.
02:52
created

BatchWriteItemOperationTest::testBatchInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 28
rs 9.6666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GuillermoandraeTest\DynamoDb\Operation;
4
5
use Guillermoandrae\DynamoDb\Constant\AttributeTypes;
6
use Guillermoandrae\DynamoDb\Constant\KeyTypes;
7
use Guillermoandrae\DynamoDb\Exception\Exception;
8
use Guillermoandrae\DynamoDb\Operation\BatchWriteItemOperation;
9
use GuillermoandraeTest\DynamoDb\TestCase;
10
11
final class BatchWriteItemOperationTest extends TestCase
12
{
13
    public function testBadBatchWrite()
14
    {
15
        $this->expectException(Exception::class);
16
        $operation = new BatchWriteItemOperation(
17
            $this->adapter->getClient(),
18
            $this->adapter->getMarshaler()
19
        );
20
        $operation->setDeleteRequest([])->execute();
21
    }
22
23
    public function testBatchInsert()
24
    {
25
        $timestamp1 = time();
26
        $timestamp2 = strtotime('tomorrow');
27
        $operation = new BatchWriteItemOperation(
28
            $this->adapter->getClient(),
29
            $this->adapter->getMarshaler(),
30
            [],
31
            [
32
                'test' => [
33
                    ['name' => 'me', 'date' => $timestamp1, 'age' => 20],
34
                    ['name' => 'us', 'date' => $timestamp2, 'age' => 22],
35
                    ['name' => 'me', 'date' => $timestamp2, 'age' => 24]
36
                ]
37
            ]
38
        );
39
        $request = $operation->toArray();
40
        $this->assertEquals('me', $request['RequestItems']['test'][0]['PutRequest']['Item']['name']['S']);
41
        $operation->execute();
42
43
        $items = $this->adapter->find([
44
            'test' => [
45
                ['name' => 'me', 'date' => $timestamp1],
46
                ['name' => 'us', 'date' => $timestamp2],
47
            ]
48
        ]);
49
        $this->assertEquals('us', $items[1]['name']);
50
        $this->assertNotEquals($items[0]['name'], $items[1]['name']);
51
    }
52
53
    public function testBatchDelete()
54
    {
55
        $timestamp1 = time();
56
        $timestamp2 = strtotime('tomorrow');
57
        $this->adapter->useTable('test')->insert(['name' => 'me', 'date' => $timestamp1, 'age' => 20]);
58
        $this->adapter->useTable('test')->insert(['name' => 'us', 'date' => $timestamp2, 'age' => 22]);
59
        $this->adapter->useTable('test')->insert(['name' => 'me', 'date' => $timestamp2, 'age' => 24]);
60
61
        $operation = new BatchWriteItemOperation(
62
            $this->adapter->getClient(),
63
            $this->adapter->getMarshaler(),
64
            ['test' => [
65
                ['name' => 'me', 'date' => $timestamp1],
66
                ['name' => 'me', 'date' => $timestamp2]
67
            ]]
68
        );
69
        $operation->execute();
70
71
        $items = $this->adapter->useTable('test')->findAll();
72
        $this->assertCount(1, $items);
73
        $this->assertEquals(22, $items[0]['age']);
74
    }
75
76
    protected function setUp(): void
77
    {
78
        parent::setUp();
79
        $adapter = $this->adapter->useTable('test');
80
        $adapter->createTable([
81
            'name' => [AttributeTypes::STRING, KeyTypes::HASH],
82
            'date' => [AttributeTypes::NUMBER, KeyTypes::RANGE],
83
        ]);
84
    }
85
}
86