BatchWriteItemOperationTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 43
c 2
b 0
f 0
dl 0
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testBadBatchWrite() 0 8 1
A testBatchDelete() 0 21 1
A testBatchInsert() 0 27 1
A setUp() 0 7 1
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->assertNotEquals($items[0]['name'], $items[1]['name']);
50
    }
51
52
    public function testBatchDelete()
53
    {
54
        $timestamp1 = time();
55
        $timestamp2 = strtotime('tomorrow');
56
        $this->adapter->useTable('test')->insert(['name' => 'me', 'date' => $timestamp1, 'age' => 20]);
57
        $this->adapter->useTable('test')->insert(['name' => 'us', 'date' => $timestamp2, 'age' => 22]);
58
        $this->adapter->useTable('test')->insert(['name' => 'me', 'date' => $timestamp2, 'age' => 24]);
59
60
        $operation = new BatchWriteItemOperation(
61
            $this->adapter->getClient(),
62
            $this->adapter->getMarshaler(),
63
            ['test' => [
64
                ['name' => 'me', 'date' => $timestamp1],
65
                ['name' => 'me', 'date' => $timestamp2]
66
            ]]
67
        );
68
        $operation->execute();
69
70
        $items = $this->adapter->useTable('test')->findAll();
71
        $this->assertCount(1, $items);
72
        $this->assertEquals(22, $items[0]['age']);
73
    }
74
75
    protected function setUp(): void
76
    {
77
        parent::setUp();
78
        $adapter = $this->adapter->useTable('test');
79
        $adapter->createTable([
80
            'name' => [AttributeTypes::STRING, KeyTypes::HASH],
81
            'date' => [AttributeTypes::NUMBER, KeyTypes::RANGE],
82
        ]);
83
    }
84
}
85