Skip to content

Commit 0431df8

Browse files
test(polylang): assert unchanged query vars correctly
Agent-Logs-Url: https://github.com/helsingborg-stad/Municipio/sessions/26b2264a-26a1-4a7d-91fa-b5aceaa535e8 Co-authored-by: sebastianthulin <797129+sebastianthulin@users.noreply.github.com>
1 parent 9fdcff0 commit 0431df8

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

library/Integrations/Polylang/ResolveFontAttachmentQueriesTest.php

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public function testAddHooksRegistersPreGetPostsAction(): void
3636
#[TestDox('makeFontAttachmentQueryLanguageAgnostic() updates font attachment queries with array mime types')]
3737
public function testMakeFontAttachmentQueryLanguageAgnosticUpdatesArrayMimeTypeQuery(): void
3838
{
39-
$query = new WP_Query();
40-
$query->query_vars = [];
39+
$query = $this->createQuery();
4140
$query->set('post_type', 'attachment');
4241
$query->set('post_mime_type', ['application/font-woff', 'font/woff2']);
4342

@@ -54,8 +53,7 @@ public function testMakeFontAttachmentQueryLanguageAgnosticUpdatesArrayMimeTypeQ
5453
#[TestDox('makeFontAttachmentQueryLanguageAgnostic() updates font attachment queries with string mime types')]
5554
public function testMakeFontAttachmentQueryLanguageAgnosticUpdatesStringMimeTypeQuery(): void
5655
{
57-
$query = new WP_Query();
58-
$query->query_vars = [];
56+
$query = $this->createQuery();
5957
$query->set('post_type', 'attachment');
6058
$query->set('post_mime_type', 'application/font-woff');
6159

@@ -72,8 +70,7 @@ public function testMakeFontAttachmentQueryLanguageAgnosticUpdatesStringMimeType
7270
#[TestDox('makeFontAttachmentQueryLanguageAgnostic() does not update non-font attachment queries')]
7371
public function testMakeFontAttachmentQueryLanguageAgnosticDoesNotUpdateNonFontQueries(): void
7472
{
75-
$query = new WP_Query();
76-
$query->query_vars = [];
73+
$query = $this->createQuery();
7774
$query->set('post_type', 'attachment');
7875
$query->set('post_mime_type', 'image/jpeg');
7976

@@ -83,15 +80,14 @@ public function testMakeFontAttachmentQueryLanguageAgnosticDoesNotUpdateNonFontQ
8380

8481
$sut->makeFontAttachmentQueryLanguageAgnostic($query);
8582

86-
static::assertNull($query->get('lang'));
87-
static::assertNull($query->get('suppress_filters'));
83+
static::assertArrayNotHasKey('lang', $query->query_vars);
84+
static::assertArrayNotHasKey('suppress_filters', $query->query_vars);
8885
}
8986

9087
#[TestDox('makeFontAttachmentQueryLanguageAgnostic() does not update queries when Polylang is unavailable')]
9188
public function testMakeFontAttachmentQueryLanguageAgnosticDoesNotUpdateQueriesWhenPolylangIsUnavailable(): void
9289
{
93-
$query = new WP_Query();
94-
$query->query_vars = [];
90+
$query = $this->createQuery();
9591
$query->set('post_type', 'attachment');
9692
$query->set('post_mime_type', 'application/font-woff');
9793

@@ -101,8 +97,8 @@ public function testMakeFontAttachmentQueryLanguageAgnosticDoesNotUpdateQueriesW
10197

10298
$sut->makeFontAttachmentQueryLanguageAgnostic($query);
10399

104-
static::assertNull($query->get('lang'));
105-
static::assertNull($query->get('suppress_filters'));
100+
static::assertArrayNotHasKey('lang', $query->query_vars);
101+
static::assertArrayNotHasKey('suppress_filters', $query->query_vars);
106102
}
107103

108104
/**
@@ -121,4 +117,47 @@ private function getSut(?Closure $polylangIsActiveResolver = null): ResolveFontA
121117
$polylangIsActiveResolver
122118
);
123119
}
120+
121+
/**
122+
* Create a query double with working get/set methods.
123+
*
124+
* @return WP_Query The query double.
125+
*/
126+
private function createQuery(): WP_Query
127+
{
128+
return new class extends WP_Query {
129+
/**
130+
* Query vars storage.
131+
*
132+
* @var array<string, mixed>
133+
*/
134+
public $query_vars = [];
135+
136+
/**
137+
* Set a query var.
138+
*
139+
* @param string $queryVar The query var name.
140+
* @param mixed $value The query var value.
141+
*
142+
* @return void
143+
*/
144+
public function set($queryVar, $value = ''): void
145+
{
146+
$this->query_vars[$queryVar] = $value;
147+
}
148+
149+
/**
150+
* Get a query var.
151+
*
152+
* @param string $queryVar The query var name.
153+
* @param mixed $defaultValue The default value.
154+
*
155+
* @return mixed The query var value.
156+
*/
157+
public function get($queryVar, $defaultValue = '')
158+
{
159+
return $this->query_vars[$queryVar] ?? $defaultValue;
160+
}
161+
};
162+
}
124163
}

0 commit comments

Comments
 (0)