packages/requirements/tests/Unit/Report/ClaimsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Report;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use Requirements\Model\Item;
12use Requirements\Model\Source;
13use Requirements\Report\Claims;
14use Requirements\Report\SourceUnit;
15use Requirements\Source\Unit;
16
17#[CoversClass(Claims::class)]
18#[UsesClass(SourceUnit::class)]
19#[UsesClass(Item::class)]
20#[UsesClass(Source::class)]
21#[UsesClass(Unit::class)]
22#[Small]
23final class ClaimsTest extends TestCase
24{
25    public function testAssignRecordsTheEvidenceOfASpecification(): void
26    {
27        $source = new Source('manual', 'source.html', 'html', 'main p');
28        $item = new Item('SPEC-001', 'specification', 'The parser shall read names.', 'supported', $source, [], [], [], [], [], '', 'sourced', '', 'definition.yaml', []);
29        $units = (new Claims())->assign(['a' => new SourceUnit($source, new Unit('p:1', 'First.')), 'b' => new SourceUnit($source, new Unit('p:2', 'Second.'))], ['SPEC-001' => $item], ['SPEC-001' => ['a']], []);
30        self::assertSame(['SPEC-001' => $item], $units['a']->claims);
31        self::assertSame([], $units['b']->claims);
32    }
33
34    public function testAssignLetsNoRequirementClaimItsOwnEvidence(): void
35    {
36        $source = new Source('manual', 'source.html', 'html', 'main p');
37        $item = new Item('REQ-001', 'requirement', 'Names start with a letter.', 'supported', $source, [], [], [], [], [], '', 'sourced', '', 'definition.yaml', []);
38        $units = (new Claims())->assign(['a' => new SourceUnit($source, new Unit('p:1', 'First.'))], ['REQ-001' => $item], ['REQ-001' => ['a']], []);
39        self::assertSame([], $units['a']->claims);
40    }
41
42    public function testAssignLetsASpecificationClaimTheEvidenceOfTheRequirementsItRefines(): void
43    {
44        $source = new Source('manual', 'source.html', 'html', 'main p');
45        $requirement = new Item('REQ-001', 'requirement', 'Names start with a letter.', 'supported', $source, [], [], [], [], [], '', 'sourced', '', 'definition.yaml', []);
46        $specification = new Item('SPEC-001', 'specification', 'The parser shall read names.', 'supported', null, [], [], ['REQ-001'], [], [], '', 'sourced', '', 'other.yaml', []);
47        $units = (new Claims())->assign(['a' => new SourceUnit($source, new Unit('p:1', 'First.')), 'b' => new SourceUnit($source, new Unit('p:2', 'Second.'))], ['REQ-001' => $requirement, 'SPEC-001' => $specification], ['REQ-001' => ['b'], 'SPEC-001' => []], []);
48        self::assertSame([], $units['a']->claims);
49        self::assertSame(['SPEC-001' => $specification], $units['b']->claims);
50    }
51
52    public function testAssignSkipsASpecificationWithInvalidEvidence(): void
53    {
54        $source = new Source('manual', 'source.html', 'html', 'main p');
55        $item = new Item('SPEC-001', 'specification', 'The parser shall read names.', 'supported', $source, [], [], [], [], [], '', 'sourced', '', 'definition.yaml', []);
56        $units = (new Claims())->assign(['a' => new SourceUnit($source, new Unit('p:1', 'First.'))], ['SPEC-001' => $item], ['SPEC-001' => ['a']], ['SPEC-001' => true]);
57        self::assertSame([], $units['a']->claims);
58    }
59
60    public function testAssignSkipsASpecificationRefiningARequirementWithInvalidEvidence(): void
61    {
62        $source = new Source('manual', 'source.html', 'html', 'main p');
63        $requirement = new Item('REQ-001', 'requirement', 'Names start with a letter.', 'supported', $source, [], [], [], [], [], '', 'sourced', '', 'definition.yaml', []);
64        $specification = new Item('SPEC-001', 'specification', 'The parser shall read names.', 'supported', $source, [], [], ['REQ-001'], [], [], '', 'sourced', '', 'definition.yaml', []);
65        $units = (new Claims())->assign(['a' => new SourceUnit($source, new Unit('p:1', 'First.')), 'b' => new SourceUnit($source, new Unit('p:2', 'Second.'))], ['REQ-001' => $requirement, 'SPEC-001' => $specification], ['REQ-001' => ['b'], 'SPEC-001' => ['a']], ['REQ-001' => true]);
66        self::assertSame([], $units['a']->claims);
67        self::assertSame([], $units['b']->claims);
68    }
69
70    public function testAssignKeepsOtherSpecificationsWhenOneIsInvalid(): void
71    {
72        $source = new Source('manual', 'source.html', 'html', 'main p');
73        $invalid = new Item('SPEC-001', 'specification', 'The parser shall read names.', 'supported', $source, [], [], [], [], [], '', 'sourced', '', 'definition.yaml', []);
74        $valid = new Item('SPEC-002', 'specification', 'The parser shall read digits.', 'supported', $source, [], [], [], [], [], '', 'sourced', '', 'definition.yaml', []);
75        $units = (new Claims())->assign(['a' => new SourceUnit($source, new Unit('p:1', 'First.'))], ['SPEC-001' => $invalid, 'SPEC-002' => $valid], ['SPEC-001' => ['a'], 'SPEC-002' => ['a']], ['SPEC-001' => true]);
76        self::assertSame(['SPEC-002' => $valid], $units['a']->claims);
77    }
78
79    public function testAssignOrdersTheUnitsByKey(): void
80    {
81        $source = new Source('manual', 'source.html', 'html', 'main p');
82        $units = (new Claims())->assign(['c' => new SourceUnit($source, new Unit('p:3', 'Third.')), 'a' => new SourceUnit($source, new Unit('p:1', 'First.')), 'b' => new SourceUnit($source, new Unit('p:2', 'Second.'))], [], [], []);
83        self::assertSame(['a', 'b', 'c'], array_keys($units));
84    }
85}
86