Skip to content

Commit 71c6447

Browse files
committed
Added unit test for setting collection on Criteria
1 parent dcde10d commit 71c6447

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

test/Feature/Event/CriteriaTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,77 @@ function (CriteriaEvent $event): void {
9191
$data['artist']['edges'][0]['node']['performances']['edges'][0]['node']['venue'],
9292
);
9393
}
94+
95+
public function testEventFilterCollection(): void
96+
{
97+
$driver = new Driver($this->getEntityManager(), new Config(['group' => 'CriteriaEvent']));
98+
99+
$driver->get(EventDispatcher::class)->subscribeTo(
100+
Artist::class . '.performances.criteria',
101+
function (CriteriaEvent $event): void {
102+
$this->assertInstanceOf(Criteria::class, $event->getCriteria());
103+
104+
$event->setCollection($event->getCollection()->filter(
105+
static function ($performance) {
106+
return $performance->getVenue() === 'Delta Center';
107+
},
108+
));
109+
110+
$this->assertInstanceOf(Collection::class, $event->getCollection());
111+
$this->assertInstanceOf(Artist::class, $event->getObjectValue());
112+
$this->assertEquals('contextTest', $event->getContext());
113+
$this->assertIsArray($event->getArgs());
114+
$this->assertInstanceOf(ResolveInfo::class, $event->getInfo());
115+
},
116+
);
117+
118+
$schema = new Schema([
119+
'query' => new ObjectType([
120+
'name' => 'query',
121+
'fields' => [
122+
'artist' => [
123+
'type' => $driver->connection(Artist::class),
124+
'args' => [
125+
'filter' => $driver->filter(Artist::class),
126+
],
127+
'resolve' => $driver->resolve(Artist::class),
128+
],
129+
],
130+
]),
131+
]);
132+
133+
$query = '
134+
query ($id: String!) {
135+
artist (filter: { id: { eq: $id } } ) {
136+
edges {
137+
node {
138+
id
139+
name
140+
performances {
141+
edges {
142+
node {
143+
venue
144+
}
145+
}
146+
}
147+
}
148+
}
149+
}
150+
}';
151+
152+
$result = GraphQL::executeQuery(
153+
schema: $schema,
154+
source: $query,
155+
variableValues: ['id' => '1'],
156+
contextValue: 'contextTest',
157+
);
158+
$data = $result->toArray()['data'];
159+
160+
$this->assertEquals(1, count($data['artist']['edges']));
161+
$this->assertEquals(1, count($data['artist']['edges'][0]['node']['performances']));
162+
$this->assertEquals(
163+
'Delta Center',
164+
$data['artist']['edges'][0]['node']['performances']['edges'][0]['node']['venue'],
165+
);
166+
}
94167
}

0 commit comments

Comments
 (0)