RecordsTraitTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testExists() 0 14 1
A testGetUniqueFields() 0 16 1
A getManagerWithUnique() 0 8 1
A testGenerateExistsParams() 0 14 1
1
<?php
2
3
namespace Nip\Records\Tests\Traits\Unique;
4
5
use Nip\Records\Tests\AbstractTest;
6
use Nip\Records\Tests\Fixtures\Records\Books\Book;
7
use Nip\Records\Tests\Fixtures\Records\Books\Books;
8
9
/**
10
 * Class RecordsTraitTest
11
 * @package Nip\Records\Tests\Traits\Unique
12
 */
13
class RecordsTraitTest extends AbstractTest
14
{
15
    public function testExists()
16
    {
17
        $manager = $this->getManagerWithUnique();
18
        $manager->shouldReceive('findOneByParams')
19
            ->andReturnUsing(function ($argument) {
20
                return $argument;
21
            });
22
23
24
        $item = new Book();
25
26
        self::assertSame(
27
            ['where' => ["`id_event` = '' AND `id_volunteer` = '' OR `hash` = ''"]],
28
            $manager->exists($item)
29
        );
30
    }
31
32
    public function testGenerateExistsParams()
33
    {
34
        $manager = $this->getManagerWithUnique();
35
        $item = new Book();
36
37
        $params = $manager->generateExistsParams($item);
38
        self::assertSame(
39
            [
40
                'where' => [
41
                    'id_event_id_volunteer-UNQ' => "`id_event` = '' AND `id_volunteer` = ''",
42
                    'hash-UNQ' => "`hash` = ''"
43
                ],
44
            ],
45
            $params
46
        );
47
    }
48
49
    public function testGetUniqueFields()
50
    {
51
        $manager = $this->getManagerWithUnique();
52
53
        $unique = $manager->getUniqueFields();
54
        self::assertSame(
55
            [
56
                'id_event_id_volunteer' => [
57
                    0 => 'id_event',
58
                    1 => 'id_volunteer'
59
                ],
60
                'hash' => [
61
                    0 => 'hash'
62
                ]
63
            ],
64
            $unique
65
        );
66
    }
67
68
    /**
69
     * @return Books|\Mockery\MockInterface
70
     */
71
    protected function getManagerWithUnique()
72
    {
73
        $manager = \Mockery::mock(Books::class)->makePartial();
74
75
        $structure = require TEST_FIXTURE_PATH
76
            . DIRECTORY_SEPARATOR . 'database_structure' . DIRECTORY_SEPARATOR . 'table_with_unique.php';
77
        $manager->setTableStructure($structure);
78
        return $manager;
79
    }
80
}
81