packages/sql-fixture/tests/Unit/Fixture/RowSpecTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Fixture;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Test;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use SqlFixture\Fixture\RowSpec;
12use SqlFixture\Fixture\TableOverrides;
13
14#[CoversClass(RowSpec::class)]
15#[UsesClass(TableOverrides::class)]
16#[CoversClass(\SqlFixture\Fixture\OverrideRows::class)]
17final class RowSpecTest extends TestCase
18{
19    #[Test]
20    public function testUnspecifiedAnUnspecifiedTableLeavesTheCountFree(): void
21    {
22        $spec = RowSpec::unspecified();
23
24        self::assertNull($spec->count);
25        self::assertSame([], $spec->overridesFor(0));
26    }
27
28    #[Test]
29    public function testFromAnIntegerIsACountWithNothingOverridden(): void
30    {
31        $spec = RowSpec::from('order_detail', 3);
32
33        self::assertSame(3, $spec->count);
34        self::assertSame([], $spec->overridesFor(1));
35    }
36
37    #[Test]
38    public function testAnEmptyArrayMeansNoRows(): void
39    {
40        self::assertSame(0, RowSpec::from('order_detail', [])->count);
41    }
42
43    #[Test]
44    public function testOverridesForOneSetOfValuesAppliesToEveryRow(): void
45    {
46        $spec = RowSpec::from('order_detail', ['quantity' => 2]);
47
48        self::assertNull($spec->count);
49        self::assertSame(['quantity' => 2], $spec->overridesFor(0));
50        self::assertSame(['quantity' => 2], $spec->overridesFor(7));
51    }
52
53    #[Test]
54    public function testAListGivesOneEntryPerRow(): void
55    {
56        $spec = RowSpec::from('order_detail', [['quantity' => 1], ['quantity' => 2]]);
57
58        self::assertSame(2, $spec->count);
59        self::assertSame(['quantity' => 1], $spec->overridesFor(0));
60        self::assertSame(['quantity' => 2], $spec->overridesFor(1));
61        self::assertSame([], $spec->overridesFor(2));
62    }
63
64    #[Test]
65    public function testTableOverridesApplyToEveryRow(): void
66    {
67        $spec = RowSpec::from('order', TableOverrides::of(['status' => 'paid']));
68
69        self::assertNull($spec->count);
70        self::assertSame(['status' => 'paid'], $spec->overridesFor(3));
71    }
72
73    #[Test]
74    public function testAListOfTableOverridesGivesOneEntryPerRow(): void
75    {
76        $spec = RowSpec::from('order_detail', [
77            TableOverrides::of(['quantity' => 1]),
78            TableOverrides::of(['quantity' => 2]),
79        ]);
80
81        self::assertSame(2, $spec->count);
82        self::assertSame(['quantity' => 2], $spec->overridesFor(1));
83    }
84
85    #[Test]
86    public function testAListOfScalarsIsReadAsColumnValuesNotRows(): void
87    {
88        $spec = RowSpec::from('order_detail', ['a', 'b']);
89
90        self::assertNull($spec->count);
91        self::assertSame(['a', 'b'], $spec->overridesFor(0));
92    }
93
94    #[Test]
95    public function testZeroIsAValidCount(): void
96    {
97        self::assertSame(0, RowSpec::from('order_detail', 0)->count);
98    }
99
100    #[Test]
101    public function testAKeyedArrayOfArraysIsStillOneSetOfValues(): void
102    {
103        $spec = RowSpec::from('order_detail', ['payload' => ['a' => 1]]);
104
105        self::assertNull($spec->count);
106        self::assertSame(['payload' => ['a' => 1]], $spec->overridesFor(0));
107    }
108}
109