Skip to content

Commit 7096bf4

Browse files
authored
Merge pull request #152 from ryanwelcher/fix/unit-tests
Comprehensive unit test improvements and expansion
2 parents 98a7c12 + 57dc098 commit 7096bf4

12 files changed

+2500
-30
lines changed

phpunit.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
bootstrap="tests/unit/bootstrap.php"
55
cacheResultFile=".phpunit.cache/test-results"
66
executionOrder="depends,defects"
7-
forceCoversAnnotation="true"
8-
beStrictAboutCoversAnnotation="true"
97
beStrictAboutOutputDuringTests="true"
108
beStrictAboutTodoAnnotatedTests="true"
119
convertDeprecationsToExceptions="true"
@@ -18,7 +16,7 @@
1816

1917
<filter>
2018
<whitelist processUncoveredFilesFromWhitelist="true">
21-
<directory suffix=".php">src</directory>
19+
<directory suffix=".php">includes</directory>
2220
</whitelist>
2321
</filter>
2422

tests/unit/Date_Query_Tests.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function data_range_and_relationship_are_discreet() {
2525
],
2626
],
2727
[
28+
'is_aql' => true,
2829
'date_query' => [
2930
'before' => [
3031
'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ),
@@ -37,7 +38,6 @@ public function data_range_and_relationship_are_discreet() {
3738
'day' => gmdate( 'd', strtotime( 'first day of -1 months' ) ),
3839
],
3940
],
40-
'is_aql' => true,
4141
],
4242
],
4343
[
@@ -50,14 +50,14 @@ public function data_range_and_relationship_are_discreet() {
5050
],
5151
],
5252
[
53+
'is_aql' => true,
5354
'date_query' => [
5455
'before' => [
5556
'year' => '2024',
5657
'month' => '06',
5758
'day' => '05',
5859
],
5960
],
60-
'is_aql' => true,
6161
],
6262
],
6363
[
@@ -70,33 +70,31 @@ public function data_range_and_relationship_are_discreet() {
7070
],
7171
],
7272
[
73+
'is_aql' => true,
7374
'date_query' => [
7475
'before' => [
7576
'year' => '2024',
7677
'month' => '06',
7778
'day' => '05',
7879
],
7980
],
80-
'is_aql' => true,
8181
],
8282
],
8383
];
8484
}
8585

8686
/**
87-
* Ranges and Relationships can't co-exist in they query
87+
* Ranges and Relationships can't co-exist in the query
8888
*
8989
* @param array $custom_data The data from the block.
9090
* @param array $expected_results The expected results to test against.
9191
*
9292
* @dataProvider data_range_and_relationship_are_discreet
93-
* @skip
9493
*/
9594
public function test_range_and_relationship_are_discreet( $custom_data, $expected_results ) {
9695
$qpg = new Query_Params_Generator( [], $custom_data );
9796
$qpg->process_all();
98-
$this->markTestSkipped( 'must be revisited.' );
99-
// Empty arrays return empty.
97+
10098
$this->assertSame( $expected_results, $qpg->get_query_args() );
10199
}
102100

@@ -113,6 +111,7 @@ public function data_all_ranges_return_expected() {
113111
],
114112
],
115113
[
114+
'is_aql' => true,
116115
'date_query' => [
117116
'before' => [
118117
'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ),
@@ -125,7 +124,6 @@ public function data_all_ranges_return_expected() {
125124
'day' => gmdate( 'd', strtotime( 'first day of -1 months' ) ),
126125
],
127126
],
128-
'is_aql' => true,
129127
],
130128
],
131129
[
@@ -135,6 +133,7 @@ public function data_all_ranges_return_expected() {
135133
],
136134
],
137135
[
136+
'is_aql' => true,
138137
'date_query' => [
139138
'before' => [
140139
'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ),
@@ -147,7 +146,6 @@ public function data_all_ranges_return_expected() {
147146
'day' => gmdate( 'd', strtotime( 'first day of -3 months' ) ),
148147
],
149148
],
150-
'is_aql' => true,
151149
],
152150
],
153151
[
@@ -157,6 +155,7 @@ public function data_all_ranges_return_expected() {
157155
],
158156
],
159157
[
158+
'is_aql' => true,
160159
'date_query' => [
161160
'before' => [
162161
'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ),
@@ -169,7 +168,6 @@ public function data_all_ranges_return_expected() {
169168
'day' => gmdate( 'd', strtotime( 'first day of -6 months' ) ),
170169
],
171170
],
172-
'is_aql' => true,
173171
],
174172
],
175173
[
@@ -179,6 +177,7 @@ public function data_all_ranges_return_expected() {
179177
],
180178
],
181179
[
180+
'is_aql' => true,
182181
'date_query' => [
183182
'before' => [
184183
'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ),
@@ -191,14 +190,13 @@ public function data_all_ranges_return_expected() {
191190
'day' => gmdate( 'd', strtotime( 'first day of -12 months' ) ),
192191
],
193192
],
194-
'is_aql' => true,
195193
],
196194
],
197195
];
198196
}
199197

200198
/**
201-
* Ensure range return the expected array
199+
* Ensure ranges return the expected array
202200
*
203201
* @param array $custom_data The data from the block.
204202
* @param array $expected_results The expected results to test against.
@@ -208,8 +206,7 @@ public function data_all_ranges_return_expected() {
208206
public function test_all_ranges_return_expected( $custom_data, $expected_results ) {
209207
$qpg = new Query_Params_Generator( [], $custom_data );
210208
$qpg->process_all();
211-
$this->markTestSkipped( 'must be revisited.' );
212-
// Empty arrays return empty.
209+
213210
$this->assertSame( $expected_results, $qpg->get_query_args() );
214211
}
215212
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<?php
2+
/**
3+
* Tests for the Disable_Pagination Trait
4+
*/
5+
6+
namespace AdvancedQueryLoop\UnitTests;
7+
8+
use AdvancedQueryLoop\Query_Params_Generator;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* Test the Disable_Pagination trait
13+
*/
14+
class Disable_Pagination_Tests extends TestCase {
15+
16+
/**
17+
* Data provider for tests that should not add no_found_rows
18+
*
19+
* @return array
20+
*/
21+
public function data_no_pagination_setting() {
22+
return array(
23+
'no disable_pagination param' => array(
24+
// Custom data.
25+
array(),
26+
// Expected.
27+
array( 'is_aql' => true ),
28+
),
29+
'empty string disable_pagination' => array(
30+
// Custom data.
31+
array(
32+
'disable_pagination' => '',
33+
),
34+
// Expected - empty string is falsy, not processed.
35+
array( 'is_aql' => true ),
36+
),
37+
'null disable_pagination' => array(
38+
// Custom data.
39+
array(
40+
'disable_pagination' => null,
41+
),
42+
// Expected - null is falsy, not processed.
43+
array( 'is_aql' => true ),
44+
),
45+
'false disable_pagination' => array(
46+
// Custom data.
47+
array(
48+
'disable_pagination' => false,
49+
),
50+
// Expected - false is falsy, not processed.
51+
array( 'is_aql' => true ),
52+
),
53+
);
54+
}
55+
56+
/**
57+
* Test that empty/null/false pagination values are not added
58+
*
59+
* @param array $custom_data The params coming from AQL.
60+
* @param array $expected The expected results.
61+
*
62+
* @dataProvider data_no_pagination_setting
63+
*/
64+
public function test_disable_pagination_empty_handling( $custom_data, $expected ) {
65+
$qpg = new Query_Params_Generator( array(), $custom_data );
66+
$qpg->process_all();
67+
68+
$this->assertEquals( $expected, $qpg->get_query_args() );
69+
}
70+
71+
/**
72+
* Data provider for truthy disable_pagination values
73+
*
74+
* @return array
75+
*/
76+
public function data_disable_pagination_enabled() {
77+
return array(
78+
'boolean true' => array(
79+
// Custom data.
80+
array(
81+
'disable_pagination' => true,
82+
),
83+
// Expected.
84+
array(
85+
'is_aql' => true,
86+
'no_found_rows' => true,
87+
),
88+
),
89+
'integer 1' => array(
90+
// Custom data.
91+
array(
92+
'disable_pagination' => 1,
93+
),
94+
// Expected.
95+
array(
96+
'is_aql' => true,
97+
'no_found_rows' => 1,
98+
),
99+
),
100+
'string "true"' => array(
101+
// Custom data.
102+
array(
103+
'disable_pagination' => 'true',
104+
),
105+
// Expected.
106+
array(
107+
'is_aql' => true,
108+
'no_found_rows' => 'true',
109+
),
110+
),
111+
'string "1"' => array(
112+
// Custom data.
113+
array(
114+
'disable_pagination' => '1',
115+
),
116+
// Expected.
117+
array(
118+
'is_aql' => true,
119+
'no_found_rows' => '1',
120+
),
121+
),
122+
);
123+
}
124+
125+
/**
126+
* Test that truthy disable_pagination values are passed through
127+
*
128+
* @param array $custom_data The params coming from AQL.
129+
* @param array $expected The expected results.
130+
*
131+
* @dataProvider data_disable_pagination_enabled
132+
*/
133+
public function test_disable_pagination_enabled( $custom_data, $expected ) {
134+
$qpg = new Query_Params_Generator( array(), $custom_data );
135+
$qpg->process_all();
136+
137+
$this->assertEquals( $expected, $qpg->get_query_args() );
138+
}
139+
140+
/**
141+
* Test that any non-falsy value is passed through (trait doesn't validate)
142+
*/
143+
public function test_disable_pagination_passes_through_any_value() {
144+
$custom_data = array(
145+
'disable_pagination' => 'some-random-value',
146+
);
147+
148+
$qpg = new Query_Params_Generator( array(), $custom_data );
149+
$qpg->process_all();
150+
151+
$result = $qpg->get_query_args();
152+
153+
// The trait simply passes through the value
154+
$this->assertEquals( 'some-random-value', $result['no_found_rows'] );
155+
}
156+
157+
/**
158+
* Test disable_pagination works with other query params
159+
*/
160+
public function test_disable_pagination_with_other_params() {
161+
$custom_data = array(
162+
'disable_pagination' => true,
163+
'exclude_current' => 10,
164+
);
165+
166+
$qpg = new Query_Params_Generator( array(), $custom_data );
167+
$qpg->process_all();
168+
169+
$result = $qpg->get_query_args();
170+
171+
// Should have both no_found_rows and post__not_in
172+
$this->assertArrayHasKey( 'no_found_rows', $result );
173+
$this->assertArrayHasKey( 'post__not_in', $result );
174+
$this->assertTrue( $result['no_found_rows'] );
175+
$this->assertEquals( array( 10 ), $result['post__not_in'] );
176+
}
177+
178+
/**
179+
* Test no_found_rows improves performance (documentation test)
180+
*/
181+
public function test_no_found_rows_purpose() {
182+
// This test documents the purpose of no_found_rows
183+
// In WordPress, no_found_rows=true skips the SQL_CALC_FOUND_ROWS
184+
// which improves query performance when pagination info isn't needed
185+
186+
$custom_data = array(
187+
'disable_pagination' => true,
188+
);
189+
190+
$qpg = new Query_Params_Generator( array(), $custom_data );
191+
$qpg->process_all();
192+
193+
$result = $qpg->get_query_args();
194+
195+
// Verify the parameter is set correctly for WP_Query
196+
$this->assertArrayHasKey( 'no_found_rows', $result );
197+
$this->assertTrue( $result['no_found_rows'] );
198+
}
199+
}

0 commit comments

Comments
 (0)