Skip to content

Commit 3a6c562

Browse files
committed
query for multiple objects in single request
1 parent d9176d0 commit 3a6c562

17 files changed

+627
-135
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace App\Factory\Model;
4+
5+
use App\Model\GitHubIssue;
6+
use App\Model\GitHubUrlData;
7+
use Zenstruck\Foundry\ModelFactory;
8+
use Zenstruck\Foundry\Proxy;
9+
10+
/**
11+
* @extends ModelFactory<GitHubIssue>
12+
*
13+
* @method GitHubIssue|Proxy create(array|callable $attributes = [])
14+
* @method static GitHubIssue|Proxy createOne(array $attributes = [])
15+
* @method static GitHubIssue|Proxy find(object|array|mixed $criteria)
16+
* @method static GitHubIssue|Proxy findOrCreate(array $attributes)
17+
* @method static GitHubIssue|Proxy first(string $sortedField = 'id')
18+
* @method static GitHubIssue|Proxy last(string $sortedField = 'id')
19+
* @method static GitHubIssue|Proxy random(array $attributes = [])
20+
* @method static GitHubIssue|Proxy randomOrCreate(array $attributes = [])
21+
* @method static GitHubIssue[]|Proxy[] all()
22+
* @method static GitHubIssue[]|Proxy[] createMany(int $number, array|callable $attributes = [])
23+
* @method static GitHubIssue[]|Proxy[] createSequence(iterable|callable $sequence)
24+
* @method static GitHubIssue[]|Proxy[] findBy(array $attributes)
25+
* @method static GitHubIssue[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
26+
* @method static GitHubIssue[]|Proxy[] randomSet(int $number, array $attributes = [])
27+
*
28+
* @psalm-method GitHubIssue&Proxy create(array|callable $attributes = [])
29+
* @psalm-method static GitHubIssue&Proxy createOne(array $attributes = [])
30+
* @psalm-method static GitHubIssue&Proxy find(object|array|mixed $criteria)
31+
* @psalm-method static GitHubIssue&Proxy findOrCreate(array $attributes)
32+
* @psalm-method static GitHubIssue&Proxy first(string $sortedField = 'id')
33+
* @psalm-method static GitHubIssue&Proxy last(string $sortedField = 'id')
34+
* @psalm-method static GitHubIssue&Proxy random(array $attributes = [])
35+
* @psalm-method static GitHubIssue&Proxy randomOrCreate(array $attributes = [])
36+
* @psalm-method static GitHubIssue[]&Proxy[] all()
37+
* @psalm-method static GitHubIssue[]&Proxy[] createMany(int $number, array|callable $attributes = [])
38+
* @psalm-method static GitHubIssue[]&Proxy[] createSequence(iterable|callable $sequence)
39+
* @psalm-method static GitHubIssue[]&Proxy[] findBy(array $attributes)
40+
* @psalm-method static GitHubIssue[]&Proxy[] randomRange(int $min, int $max, array $attributes = [])
41+
* @psalm-method static GitHubIssue[]&Proxy[] randomSet(int $number, array $attributes = [])
42+
*/
43+
class GitHubIssueFactory extends ModelFactory
44+
{
45+
#[\Override]
46+
protected function getDefaults(): array
47+
{
48+
return [
49+
'owner' => $owner = self::faker()->slug(),
50+
'repo' => $repository = self::faker()->slug(),
51+
'number' => $id = self::faker()->randomNumber(),
52+
'uri' => sprintf('https://github.com/%s/%s/issues/%s',
53+
$owner,
54+
$repository,
55+
$id
56+
),
57+
'title' => self::faker()->sentence(),
58+
];
59+
}
60+
61+
public function fromUrlData(GitHubUrlData $urlData): self
62+
{
63+
return $this->addState([
64+
'owner' => $urlData->owner,
65+
'repo' => $urlData->repository,
66+
'number' => $urlData->identifier,
67+
'uri' => $urlData->uri,
68+
'title' => 'some sort of issue',
69+
]);
70+
}
71+
72+
#[\Override]
73+
protected function initialize(): self
74+
{
75+
return $this
76+
->withoutPersisting()
77+
;
78+
}
79+
80+
#[\Override]
81+
protected static function getClass(): string
82+
{
83+
return GitHubIssue::class;
84+
}
85+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace App\Factory\Model;
4+
5+
use App\Model\GitHubPullRequest;
6+
use App\Model\GitHubUrlData;
7+
use Zenstruck\Foundry\ModelFactory;
8+
use Zenstruck\Foundry\Proxy;
9+
10+
/**
11+
* @extends ModelFactory<GitHubPullRequest>
12+
*
13+
* @method GitHubPullRequest|Proxy create(array|callable $attributes = [])
14+
* @method static GitHubPullRequest|Proxy createOne(array $attributes = [])
15+
* @method static GitHubPullRequest|Proxy find(object|array|mixed $criteria)
16+
* @method static GitHubPullRequest|Proxy findOrCreate(array $attributes)
17+
* @method static GitHubPullRequest|Proxy first(string $sortedField = 'id')
18+
* @method static GitHubPullRequest|Proxy last(string $sortedField = 'id')
19+
* @method static GitHubPullRequest|Proxy random(array $attributes = [])
20+
* @method static GitHubPullRequest|Proxy randomOrCreate(array $attributes = [])
21+
* @method static GitHubPullRequest[]|Proxy[] all()
22+
* @method static GitHubPullRequest[]|Proxy[] createMany(int $number, array|callable $attributes = [])
23+
* @method static GitHubPullRequest[]|Proxy[] createSequence(iterable|callable $sequence)
24+
* @method static GitHubPullRequest[]|Proxy[] findBy(array $attributes)
25+
* @method static GitHubPullRequest[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
26+
* @method static GitHubPullRequest[]|Proxy[] randomSet(int $number, array $attributes = [])
27+
*
28+
* @psalm-method GitHubPullRequest&Proxy create(array|callable $attributes = [])
29+
* @psalm-method static GitHubPullRequest&Proxy createOne(array $attributes = [])
30+
* @psalm-method static GitHubPullRequest&Proxy find(object|array|mixed $criteria)
31+
* @psalm-method static GitHubPullRequest&Proxy findOrCreate(array $attributes)
32+
* @psalm-method static GitHubPullRequest&Proxy first(string $sortedField = 'id')
33+
* @psalm-method static GitHubPullRequest&Proxy last(string $sortedField = 'id')
34+
* @psalm-method static GitHubPullRequest&Proxy random(array $attributes = [])
35+
* @psalm-method static GitHubPullRequest&Proxy randomOrCreate(array $attributes = [])
36+
* @psalm-method static GitHubPullRequest[]&Proxy[] all()
37+
* @psalm-method static GitHubPullRequest[]&Proxy[] createMany(int $number, array|callable $attributes = [])
38+
* @psalm-method static GitHubPullRequest[]&Proxy[] createSequence(iterable|callable $sequence)
39+
* @psalm-method static GitHubPullRequest[]&Proxy[] findBy(array $attributes)
40+
* @psalm-method static GitHubPullRequest[]&Proxy[] randomRange(int $min, int $max, array $attributes = [])
41+
* @psalm-method static GitHubPullRequest[]&Proxy[] randomSet(int $number, array $attributes = [])
42+
*/
43+
class GitHubPullRequestFactory extends ModelFactory
44+
{
45+
#[\Override]
46+
protected function getDefaults(): array
47+
{
48+
return [
49+
'owner' => $owner = self::faker()->slug(),
50+
'repo' => $repository = self::faker()->slug(),
51+
'number' => $id = self::faker()->randomNumber(),
52+
'uri' => sprintf('https://github.com/%s/%s/pulls/%s',
53+
$owner,
54+
$repository,
55+
$id
56+
),
57+
'title' => self::faker()->sentence(),
58+
];
59+
}
60+
61+
public function fromUrlData(GitHubUrlData|Proxy $urlData, string $title = '[ci] run sa tools'): self
62+
{
63+
return $this->addState([
64+
'owner' => $urlData->owner,
65+
'repo' => $urlData->repository,
66+
'number' => $urlData->identifier,
67+
'uri' => $urlData->uri,
68+
'title' => $title,
69+
]);
70+
}
71+
72+
#[\Override]
73+
protected function initialize(): self
74+
{
75+
return $this
76+
->withoutPersisting()
77+
;
78+
}
79+
80+
#[\Override]
81+
protected static function getClass(): string
82+
{
83+
return GitHubPullRequest::class;
84+
}
85+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace App\Factory\Model;
4+
5+
use App\Model\GitHubUrlData;
6+
use App\Util\TypeEnum;
7+
use Zenstruck\Foundry\ModelFactory;
8+
use Zenstruck\Foundry\Proxy;
9+
10+
/**
11+
* @extends ModelFactory<GitHubUrlData>
12+
*
13+
* @method GitHubUrlData|Proxy create(array|callable $attributes = [])
14+
* @method static GitHubUrlData|Proxy createOne(array $attributes = [])
15+
* @method static GitHubUrlData|Proxy find(object|array|mixed $criteria)
16+
* @method static GitHubUrlData|Proxy findOrCreate(array $attributes)
17+
* @method static GitHubUrlData|Proxy first(string $sortedField = 'id')
18+
* @method static GitHubUrlData|Proxy last(string $sortedField = 'id')
19+
* @method static GitHubUrlData|Proxy random(array $attributes = [])
20+
* @method static GitHubUrlData|Proxy randomOrCreate(array $attributes = [])
21+
* @method static GitHubUrlData[]|Proxy[] all()
22+
* @method static GitHubUrlData[]|Proxy[] createMany(int $number, array|callable $attributes = [])
23+
* @method static GitHubUrlData[]|Proxy[] createSequence(iterable|callable $sequence)
24+
* @method static GitHubUrlData[]|Proxy[] findBy(array $attributes)
25+
* @method static GitHubUrlData[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
26+
* @method static GitHubUrlData[]|Proxy[] randomSet(int $number, array $attributes = [])
27+
*
28+
* @psalm-method GitHubUrlData&Proxy create(array|callable $attributes = [])
29+
* @psalm-method static GitHubUrlData&Proxy createOne(array $attributes = [])
30+
* @psalm-method static GitHubUrlData&Proxy find(object|array|mixed $criteria)
31+
* @psalm-method static GitHubUrlData&Proxy findOrCreate(array $attributes)
32+
* @psalm-method static GitHubUrlData&Proxy first(string $sortedField = 'id')
33+
* @psalm-method static GitHubUrlData&Proxy last(string $sortedField = 'id')
34+
* @psalm-method static GitHubUrlData&Proxy random(array $attributes = [])
35+
* @psalm-method static GitHubUrlData&Proxy randomOrCreate(array $attributes = [])
36+
* @psalm-method static GitHubUrlData[]&Proxy[] all()
37+
* @psalm-method static GitHubUrlData[]&Proxy[] createMany(int $number, array|callable $attributes = [])
38+
* @psalm-method static GitHubUrlData[]&Proxy[] createSequence(iterable|callable $sequence)
39+
* @psalm-method static GitHubUrlData[]&Proxy[] findBy(array $attributes)
40+
* @psalm-method static GitHubUrlData[]&Proxy[] randomRange(int $min, int $max, array $attributes = [])
41+
* @psalm-method static GitHubUrlData[]&Proxy[] randomSet(int $number, array $attributes = [])
42+
*/
43+
class GitHubUrlDataFactory extends ModelFactory
44+
{
45+
#[\Override]
46+
protected function getDefaults(): array
47+
{
48+
return [
49+
'owner' => $owner = self::faker()->slug(),
50+
'repository' => $repository = self::faker()->slug(),
51+
'type' => $type = TypeEnum::PULL_REQUEST,
52+
'identifier' => $id = self::faker()->randomNumber(),
53+
'uri' => sprintf('https://github.com/%s/%s/%s/%s',
54+
$owner,
55+
$repository,
56+
TypeEnum::PULL_REQUEST === $type ? 'pulls' : 'issues',
57+
$id
58+
),
59+
];
60+
}
61+
62+
public function asIssue(): self
63+
{
64+
$uri = $this->getDefaults()['uri'];
65+
66+
return $this->addState([
67+
'type' => TypeEnum::ISSUE,
68+
'uri' => str_replace(TypeEnum::PULL_REQUEST->value, TypeEnum::ISSUE->value, (string) $uri),
69+
]);
70+
}
71+
72+
public function forResponse(): self
73+
{
74+
return $this->addState([
75+
'owner' => 'rushlow-development',
76+
'repository' => 'big-desk',
77+
'type' => TypeEnum::PULL_REQUEST,
78+
'identifier' => 100,
79+
'uri' => 'https://github.com/rushlow-development/big-desk/pulls/100',
80+
]);
81+
}
82+
83+
#[\Override]
84+
protected function initialize(): self
85+
{
86+
return $this
87+
->withoutPersisting()
88+
;
89+
}
90+
91+
#[\Override]
92+
protected static function getClass(): string
93+
{
94+
return GitHubUrlData::class;
95+
}
96+
}

src/Factory/TaskFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Factory;
44

55
use App\Entity\Task;
6+
use App\Entity\TodoList;
67
use App\Repository\TaskRepository;
78
use Doctrine\Common\Collections\ArrayCollection;
89
use Zenstruck\Foundry\ModelFactory;
@@ -56,6 +57,16 @@ protected function getDefaults(): array
5657
];
5758
}
5859

60+
public function withName(string $name): self
61+
{
62+
return $this->addState(['name' => $name]);
63+
}
64+
65+
public function forTodoList(TodoList|Proxy $todoList): self
66+
{
67+
return $this->addState(['todoList' => $todoList]);
68+
}
69+
5970
#[\Override]
6071
protected static function getClass(): string
6172
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fragment issue on Issue {
2+
title
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
issueZ<?php echo $number; ?>: issue(number: <?php echo $number; ?>) {
2+
...issue
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fragment pr on PullRequest {
2+
title
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pullZ<?php echo $number; ?>: pullRequest(number: <?php echo $number; ?>) {
2+
...pr
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query($repository_owner: String!, $repository_name: String!) {
2+
repository (owner: $repository_owner, name: $repository_name) {
3+
<?php echo $objects; ?>
4+
}
5+
}
6+
7+
<?php echo $fragments; ?>

0 commit comments

Comments
 (0)