Passed
Push — codex/explain-codebase-structu... ( a4c6f1...5ea8d4 )
by Michael
08:21
created

StubTreeRepository   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
dl 0
loc 18
rs 10
eloc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A persistFromData() 0 8 1
A get() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
use PHPUnit\Framework\TestCase;
6
use XoopsModules\Pedigree\Helper;
7
use XoopsModules\Pedigree\Tree;
8
9
final class TreeTest extends TestCase
10
{
11
    /** @var StubTreeRepository */
12
    private $repository;
13
14
    protected function setUp(): void
15
    {
16
        $this->repository = new StubTreeRepository();
17
        $helper = new class extends Helper {
18
            /** @var array<string,mixed> */
19
            private $handlers = [];
20
21
            public function setHandler(string $name, $handler): void
22
            {
23
                $this->handlers[strtolower($name)] = $handler;
24
            }
25
26
            public function getHandler($name)
27
            {
28
                $key = strtolower($name);
29
                if (isset($this->handlers[$key])) {
30
                    return $this->handlers[$key];
31
                }
32
33
                return parent::getHandler($name);
34
            }
35
        };
36
        $helper->setHandler('Tree', $this->repository);
37
        Helper::setInstance($helper);
38
    }
39
40
    protected function tearDown(): void
41
    {
42
        Helper::setInstance(null);
43
    }
44
45
    public function testGetParentsReturnsPopulatedParentInformation(): void
46
    {
47
        $mother = $this->repository->persistFromData(['id' => 101, 'pname' => 'Matriarch']);
48
        $father = $this->repository->persistFromData(['id' => 202, 'pname' => 'Patriarch']);
49
50
        $subject = new Tree();
51
        $subject->setVar('mother', $mother->getVar('id'));
52
        $subject->setVar('father', $father->getVar('id'));
53
54
        $parents = $subject->getParents();
55
56
        self::assertSame(['id' => 101, 'name' => 'Matriarch'], $parents['mother']);
57
        self::assertSame(['id' => 202, 'name' => 'Patriarch'], $parents['father']);
58
    }
59
60
    public function testGetParentsReturnsDefaultWhenParentMissing(): void
61
    {
62
        $subject = new Tree();
63
        $subject->setVar('mother', 0);
64
        $subject->setVar('father', 0);
65
66
        $parents = $subject->getParents();
67
68
        self::assertSame(['id' => 0, 'name' => ''], $parents['mother']);
69
        self::assertSame(['id' => 0, 'name' => ''], $parents['father']);
70
    }
71
}
72
73
final class StubTreeRepository
74
{
75
    /** @var array<int,Tree> */
76
    private $records = [];
77
78
    public function persistFromData(array $data): Tree
79
    {
80
        $tree = new Tree();
81
        $tree->assignVars($data);
82
        $tree->unsetNew();
83
        $this->records[(int)$tree->getVar('id')] = $tree;
84
85
        return $tree;
86
    }
87
88
    public function get(int $id): ?Tree
89
    {
90
        return $this->records[$id] ?? null;
91
    }
92
}
93