Skip to content

Commit fad4b1e

Browse files
committed
[graphql] ModelHelper moved into new namaspace + tests.
1 parent 244ed6f commit fad4b1e

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

src/ModelHelper.php renamed to src/Helpers/ModelHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types = 1);
22

3-
namespace LastDragon_ru\LaraASP\GraphQL;
3+
namespace LastDragon_ru\LaraASP\GraphQL\Helpers;
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Database\Eloquent\Model;

src/Helpers/ModelHelperTest.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

src/SearchBy/Operators/Complex/Relation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
66
use Illuminate\Database\Query\Builder as QueryBuilder;
7-
use LastDragon_ru\LaraASP\GraphQL\ModelHelper;
7+
use LastDragon_ru\LaraASP\GraphQL\Helpers\ModelHelper;
88
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\OperatorNegationable;
99
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator;
1010
use LastDragon_ru\LaraASP\GraphQL\SearchBy\SearchBuilder;

src/SortBy/SortBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Database\Eloquent\Relations\HasOne;
88
use Illuminate\Database\Eloquent\Relations\Relation;
99
use Illuminate\Database\Query\Builder as QueryBuilder;
10-
use LastDragon_ru\LaraASP\GraphQL\ModelHelper;
10+
use LastDragon_ru\LaraASP\GraphQL\Helpers\ModelHelper;
1111
use LogicException;
1212

1313
use function array_keys;

0 commit comments

Comments
 (0)