|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace LastDragon_ru\LaraASP\GraphQL\Helpers; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Exception; |
| 7 | +use Illuminate\Database\Eloquent\Builder; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 10 | +use Illuminate\Database\Eloquent\Relations\HasOne; |
| 11 | +use LastDragon_ru\LaraASP\GraphQL\Testing\TestCase; |
| 12 | +use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; |
| 13 | +use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; |
| 14 | +use LastDragon_ru\LaraASP\Testing\Providers\Unknown; |
| 15 | +use LogicException; |
| 16 | +use stdClass; |
| 17 | + |
| 18 | +use function sprintf; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + * @coversDefaultClass \LastDragon_ru\LaraASP\GraphQL\Helpers\ModelHelper |
| 23 | + */ |
| 24 | +class ModelHelperTest extends TestCase { |
| 25 | + // <editor-fold desc="Tests"> |
| 26 | + // ========================================================================= |
| 27 | + /** |
| 28 | + * @covers ::getRelation |
| 29 | + * |
| 30 | + * @dataProvider dataProviderGetRelation |
| 31 | + */ |
| 32 | + public function testGetRelation(Exception|string $expected, Closure $model, string $name): void { |
| 33 | + if ($expected instanceof Exception) { |
| 34 | + $this->expectExceptionObject($expected); |
| 35 | + } |
| 36 | + |
| 37 | + $this->assertInstanceOf($expected, (new ModelHelper($model()))->getRelation($name)); |
| 38 | + } |
| 39 | + // </editor-fold> |
| 40 | + |
| 41 | + // <editor-fold desc="DataProviders"> |
| 42 | + // ========================================================================= |
| 43 | + /** |
| 44 | + * @return array<mixed> |
| 45 | + */ |
| 46 | + public function dataProviderGetRelation(): array { |
| 47 | + return (new CompositeDataProvider( |
| 48 | + new ArrayDataProvider([ |
| 49 | + 'model' => [ |
| 50 | + new Unknown(), |
| 51 | + static function (): Model { |
| 52 | + return new ModelHelperTest__Model(); |
| 53 | + }, |
| 54 | + ], |
| 55 | + 'builder' => [ |
| 56 | + new Unknown(), |
| 57 | + static function (): Builder { |
| 58 | + return (new ModelHelperTest__Model())->query(); |
| 59 | + }, |
| 60 | + ], |
| 61 | + ]), |
| 62 | + new ArrayDataProvider([ |
| 63 | + 'noTypeHint' => [ |
| 64 | + new LogicException(sprintf( |
| 65 | + 'Property `%s` is not a relation.', |
| 66 | + 'noTypeHint', |
| 67 | + )), |
| 68 | + 'noTypeHint', |
| 69 | + ], |
| 70 | + 'notRelation' => [ |
| 71 | + new LogicException(sprintf( |
| 72 | + 'Property `%s` is not a relation.', |
| 73 | + 'notRelation', |
| 74 | + )), |
| 75 | + 'notRelation', |
| 76 | + ], |
| 77 | + 'union' => [ |
| 78 | + new LogicException(sprintf( |
| 79 | + 'Property `%s` is not a relation.', |
| 80 | + 'union', |
| 81 | + )), |
| 82 | + 'union', |
| 83 | + ], |
| 84 | + 'ok' => [ |
| 85 | + BelongsTo::class, |
| 86 | + 'ok', |
| 87 | + ], |
| 88 | + ]), |
| 89 | + ))->getData(); |
| 90 | + } |
| 91 | + // </editor-fold> |
| 92 | +} |
| 93 | + |
| 94 | +// @phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses |
| 95 | +// @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps |
| 96 | + |
| 97 | +/** |
| 98 | + * @internal |
| 99 | + * @noinspection PhpMultipleClassesDeclarationsInOneFile |
| 100 | + */ |
| 101 | +class ModelHelperTest__Model extends Model { |
| 102 | + /** |
| 103 | + * @noinspection PhpMissingReturnTypeInspection |
| 104 | + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint |
| 105 | + */ |
| 106 | + public function noTypeHint() { |
| 107 | + return $this->belongsTo(self::class); |
| 108 | + } |
| 109 | + |
| 110 | + public function notRelation(): stdClass { |
| 111 | + return new stdClass(); |
| 112 | + } |
| 113 | + |
| 114 | + public function union(): BelongsTo|HasOne { |
| 115 | + return $this->belongsTo(self::class); |
| 116 | + } |
| 117 | + |
| 118 | + public function ok(): BelongsTo { |
| 119 | + return $this->belongsTo(self::class); |
| 120 | + } |
| 121 | +} |
0 commit comments