Passed
Push — master ( 543cc3...82c6b4 )
by Gabriel
02:50 queued 12s
created

RecordsTraitTest::testExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
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
16
    public function testExists()
17
    {
18
        $manager = $this->getManagerWithUnique();
19
        $manager->shouldReceive('findOneByParams')
20
            ->andReturnUsing(function ($argument) {
21
                return $argument;
22
            });
23
24
25
        $item = new Book();
26
27
        self::assertSame(
28
            ['where' => ["`id_event` = '' AND `id_volunteer` = '' OR `hash` = ''"]],
29
            $manager->exists($item)
30
        );
31
    }
32
33
    public function testGenerateExistsParams()
34
    {
35
        $manager = $this->getManagerWithUnique();
36
        $item = new Book();
37
38
        $params = $manager->generateExistsParams($item);
39
        self::assertSame(
40
            [
41
                'where' => [
42
                    'id_event_id_volunteer-UNQ' => "`id_event` = '' AND `id_volunteer` = ''",
43
                    'hash-UNQ' => "`hash` = ''"
44
                ],
45
            ],
46
            $params
47
        );
48
    }
49
50
    public function testGetUniqueFields()
51
    {
52
        $manager = $this->getManagerWithUnique();
53
54
        $unique = $manager->getUniqueFields();
55
        self::assertSame(
56
            [
57
                'id_event_id_volunteer' => [
58
                    0 => 'id_event',
59
                    1 => 'id_volunteer'
60
                ],
61
                'hash' => [
62
                    0 => 'hash'
63
                ]
64
            ],
65
            $unique
66
        );
67
    }
68
69
    /**
70
     * @return Books|\Mockery\MockInterface
71
     */
72
    protected function getManagerWithUnique()
73
    {
74
        $manager = \Mockery::mock(Books::class)->makePartial();
75
76
        $structure = require TEST_FIXTURE_PATH
77
            . DIRECTORY_SEPARATOR . 'database_structure' . DIRECTORY_SEPARATOR . 'table_with_unique.php';
78
        $manager->setTableStructure($structure);
79
        return $manager;
80
    }
81
}
82