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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Fixture;
6
7use Faker\Factory;
8use PHPUnit\Framework\Attributes\CoversClass;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use SqlFixture\Fixture\RowGenerator;
12use SqlFixture\Fixture\Validation\OverrideValidator;
13use SqlFixture\Hydrator\HydratorInterface;
14use SqlFixture\Schema\ColumnDefinition;
15use SqlFixture\Schema\TableSchema;
16use SqlFixture\TypeMapper\TypeMapperInterface;
17use stdClass;
18
19#[CoversClass(RowGenerator::class)]
20#[UsesClass(OverrideValidator::class)]
21#[UsesClass(ColumnDefinition::class)]
22#[UsesClass(TableSchema::class)]
23final class RowGeneratorTest extends TestCase
24{
25    public function testGenerateUsesInjectedMappingAndPreservesOverrides(): void
26    {
27        $faker = Factory::create();
28        $value = new ColumnDefinition('value', 'custom');
29        $mapper = $this->createMock(TypeMapperInterface::class);
30        $mapper->expects(self::once())->method('generate')->with($faker, $value)->willReturn('mapped');
31        $hydrator = $this->createMock(HydratorInterface::class);
32        $hydrator->expects(self::never())->method('hydrate');
33        $schema = new TableSchema('items', [
34            'id' => new ColumnDefinition('id', 'custom', autoIncrement: true),
35            'value' => $value,
36            'label' => new ColumnDefinition('label', 'custom'),
37            'computed' => new ColumnDefinition('computed', 'custom', generated: true),
38        ]);
39
40        self::assertSame(['value' => 'mapped', 'label' => 'chosen'], (new RowGenerator($faker, $mapper, $hydrator))->generate($schema, ['label' => 'chosen']));
41    }
42
43    public function testGenerateDelegatesObjectHydration(): void
44    {
45        $faker = Factory::create();
46        $mapper = $this->createMock(TypeMapperInterface::class);
47        $mapper->expects(self::never())->method('generate');
48        $object = new stdClass();
49        $object->id = 7;
50        $hydrator = $this->createMock(HydratorInterface::class);
51        $hydrator->expects(self::once())->method('hydrate')->with(['id' => 7], stdClass::class)->willReturn($object);
52        $schema = new TableSchema('items', ['id' => new ColumnDefinition('id', 'custom')]);
53
54        self::assertSame($object, (new RowGenerator($faker, $mapper, $hydrator))->generate($schema, ['id' => 7], stdClass::class));
55    }
56}
57