Passed
Push — master ( 7815e3...d45a7a )
by Pieter
04:53
created

StorageWriterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 44.3 %

Importance

Changes 0
Metric Value
dl 35
loc 79
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 3 1
A it_should_update_a_record() 17 17 1
A it_should_create_a_table_on_first_insert() 17 17 1
A it_should_update_the_table_on_record_change() 0 18 1
A setUp() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Avoran\RapidoBundle;
4
5
use Avoran\Rapido\ReadModel\DataType\Boolean;
6
use Avoran\Rapido\ReadModel\DataType\Integer;
7
use Avoran\Rapido\ReadModel\DataType\TextString;
8
use Avoran\Rapido\ReadModel\ReadModelConfiguration;
9
use Avoran\Rapido\ReadModel\ReadModelField;
10
use Avoran\Rapido\ReadModel\ReadModelId;
11
use Avoran\Rapido\ReadModel\Record;
12
use Avoran\Rapido\ReadModel\StorageWriter;
13
use Doctrine\DBAL\Connection;
14
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15
16
class StorageWriterTest extends KernelTestCase
17
{
18
    /** @var StorageWriter */
19
    private $writer;
20
21
    /** @var Connection */
22
    private $connection;
23
24
    public static function setUpBeforeClass()
25
    {
26
        unlink(__DIR__ . '/Config/sqlite.db');
27
    }
28
29
    protected function setUp()
30
    {
31
        $container = self::bootKernel()->getContainer();
32
        $this->writer = $container->get('rapido.storage_writer');
33
        $this->connection = $container->get('database_connection');
34
    }
35
36
    /** @test */
37 View Code Duplication
    public function it_should_create_a_table_on_first_insert()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $meta = new ReadModelConfiguration(
40
            'test',
41
            new ReadModelId(new Integer()),
42
            [
43
                new ReadModelField('f1', new Boolean()),
44
                new ReadModelField('f2', new TextString(10)),
45
            ],
46
            function ($data) { return new Record($data['id'], ['f1' => $data['f1'], 'f2' => $data['f2']]); },
47
            function () {}
48
        );
49
50
        $this->writer->writeRecord($meta, ['id' => 1, 'f1' => true, 'f2' => 'test']);
51
52
        $this->assertCount(1, $this->connection->createQueryBuilder()->select('*')->from('test_test')->execute()->fetchAll());
53
        $this->assertCount(3, $this->connection->createQueryBuilder()->select('*')->from('test_test')->execute()->fetch());
54
    }
55
56
    /** @test */
57
    public function it_should_update_the_table_on_record_change()
58
    {
59
        $meta = new ReadModelConfiguration(
60
            'test',
61
            new ReadModelId(new Integer()),
62
            [
63
                new ReadModelField('f1', new Boolean()),
64
                new ReadModelField('f2', new TextString(10)),
65
                new ReadModelField('f3', new TextString(10)),
66
            ],
67
            function ($data) { return new Record($data['id'], ['f1' => $data['f1'], 'f2' => $data['f2'], 'f3' => $data['f2']]); },
68
            function () {}
69
        );
70
71
        $this->writer->writeRecord($meta, ['id' => 2, 'f1' => true, 'f2' => 'test']);
72
73
        $this->assertEquals('test', $this->connection->createQueryBuilder()->select('f3')->from('test_test')->where('identifier = 2')->execute()->fetchColumn(0));
74
        $this->assertCount(4, $this->connection->createQueryBuilder()->select('*')->from('test_test')->execute()->fetch());
75
    }
76
77
    /** @test */
78 View Code Duplication
    public function it_should_update_a_record()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $meta = new ReadModelConfiguration(
81
            'test',
82
            new ReadModelId(new Integer()),
83
            [
84
                new ReadModelField('f1', new Boolean()),
85
                new ReadModelField('f2', new TextString(10))
86
            ],
87
            function ($data) { return new Record($data['id'], ['f1' => $data['f1'], 'f2' => $data['f2']]); },
88
            function () {}
89
        );
90
91
        $this->writer->writeRecord($meta, ['id' => 2, 'f1' => true, 'f2' => 'test2']);
92
93
        $this->assertEquals('test2', $this->connection->createQueryBuilder()->select('f2')->from('test_test')->where('identifier = 2')->execute()->fetchColumn(0));
94
        $this->assertCount(3, $this->connection->createQueryBuilder()->select('*')->from('test_test')->execute()->fetch());
95
    }
96
}
97