packages/ztd-query-core/tests/Fake/CascadingShop.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Fake;
6
7use ZtdQuery\Schema\Key\ForeignKeyDefinition;
8use ZtdQuery\Schema\Key\ReferentialAction;
9use ZtdQuery\Schema\TableDefinition;
10use ZtdQuery\Schema\TableDefinitionRegistry;
11use ZtdQuery\Shadow\ShadowStore;
12
13/**
14 * Two tables, one of which takes its children with it.
15 *
16 * The smallest shape in which a statement's consequences can be seen reaching
17 * past the table it was written against, which is what several tests are
18 * about and none of them is about setting up.
19 */
20final class CascadingShop
21{
22    /**
23     * Answers the two tables and the key between them.
24     *
25     * @return TableDefinitionRegistry What describes them
26     */
27    public static function registry(): TableDefinitionRegistry
28    {
29        $registry = new TableDefinitionRegistry();
30        $registry->register('parents', new TableDefinition(['id'], [], ['id'], [], []));
31        $registry->register('children', new TableDefinition(['id', 'parent_id'], [], ['id'], [], [], foreignKeys: [
32            'fk' => new ForeignKeyDefinition(['parent_id'], 'parents', ['id'], ReferentialAction::Cascade),
33        ]));
34
35        return $registry;
36    }
37
38    /**
39     * Answers a shadow holding one parent and the one child referencing it.
40     *
41     * @return ShadowStore The shadow
42     */
43    public static function shadow(): ShadowStore
44    {
45        $store = new ShadowStore();
46        $store->set('parents', [['id' => 1]]);
47        $store->set('children', [['id' => 10, 'parent_id' => 1]]);
48
49        return $store;
50    }
51}
52