Skip to content

Commit fe72603

Browse files
committed
Share fixtures across a number of query-related test classes.
This shaves 10-20 seconds off the running time for the suite. See #30017. git-svn-id: https://develop.svn.wordpress.org/trunk@30276 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a4ecf4b commit fe72603

File tree

3 files changed

+114
-50
lines changed

3 files changed

+114
-50
lines changed

tests/phpunit/includes/testcase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ function start_transaction() {
167167
add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
168168
}
169169

170+
/**
171+
* Commit the queries in a transaction.
172+
*
173+
* @since 4.1.0
174+
*/
175+
public static function commit_transaction() {
176+
global $wpdb;
177+
$wpdb->query( 'COMMIT;' );
178+
}
179+
170180
function _create_temporary_tables( $query ) {
171181
if ( 'CREATE TABLE' === substr( trim( $query ), 0, 12 ) )
172182
return substr_replace( trim( $query ), 'CREATE TEMPORARY TABLE', 0, 12 );

tests/phpunit/tests/query/date.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class Tests_Query_Date extends WP_UnitTestCase {
1010

1111
public $q;
1212

13-
public function setUp() {
14-
parent::setUp();
13+
static $post_ids = array();
1514

15+
public static function setUpBeforeClass() {
1616
// Be careful modifying this. Tests are coded to expect this exact sample data.
1717
$post_dates = array(
1818
'1972-05-24 14:53:45',
@@ -40,10 +40,25 @@ public function setUp() {
4040
'2025-05-20 10:13:01',
4141
);
4242

43+
$factory = new WP_UnitTest_Factory;
44+
4345
foreach ( $post_dates as $post_date ) {
44-
$this->factory->post->create( array( 'post_date' => $post_date ) );
46+
self::$post_ids[] = $factory->post->create( array( 'post_date' => $post_date ) );
4547
}
4648

49+
self::commit_transaction();
50+
}
51+
52+
public static function tearDownAfterClass() {
53+
foreach ( self::$post_ids as $post_id ) {
54+
wp_delete_post( $post_id, true );
55+
}
56+
57+
self::commit_transaction();
58+
}
59+
60+
public function setUp() {
61+
parent::setUp();
4762
unset( $this->q );
4863
$this->q = new WP_Query();
4964
}

tests/phpunit/tests/query/results.php

Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,86 @@
1010
class Tests_Query_Results extends WP_UnitTestCase {
1111
protected $q;
1212

13+
static $cat_ids = array();
14+
static $tag_ids = array();
15+
static $post_ids = array();
16+
17+
static $parent_one;
18+
static $parent_two;
19+
static $parent_three;
20+
static $child_one;
21+
static $child_two;
22+
static $child_three;
23+
static $child_four;
24+
25+
public static function setUpBeforeClass() {
26+
$factory = new WP_UnitTest_Factory;
27+
28+
self::$cat_ids[] = $cat_a = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
29+
self::$cat_ids[] = $cat_b = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
30+
self::$cat_ids[] = $cat_c = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-c' ) );
31+
32+
self::$tag_ids[] = $tag_a = $factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'tag-a' ) );
33+
self::$tag_ids[] = $tag_b = $factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'tag-b' ) );
34+
self::$tag_ids[] = $tag_c = $factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'tag-c' ) );
35+
self::$tag_ids[] = $tag_nun = $factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'tag-נ' ) );
36+
37+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'tag-נ', 'tags_input' => array( 'tag-נ' ), 'post_date' => '2008-11-01 00:00:00' ) );
38+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'cats-a-b-c', 'post_date' => '2008-12-01 00:00:00', 'post_category' => array( $cat_a, $cat_b, $cat_c ) ) );
39+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'cats-a-and-b', 'post_date' => '2009-01-01 00:00:00', 'post_category' => array( $cat_a, $cat_b ) ) );
40+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'cats-b-and-c', 'post_date' => '2009-02-01 00:00:00', 'post_category' => array( $cat_b, $cat_c ) ) );
41+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'cats-a-and-c', 'post_date' => '2009-03-01 00:00:00', 'post_category' => array( $cat_a, $cat_c ) ) );
42+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'cat-a', 'post_date' => '2009-04-01 00:00:00', 'post_category' => array( $cat_a ) ) );
43+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'cat-b', 'post_date' => '2009-05-01 00:00:00', 'post_category' => array( $cat_b ) ) );
44+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'cat-c', 'post_date' => '2009-06-01 00:00:00', 'post_category' => array( $cat_c ) ) );
45+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'lorem-ipsum', 'post_date' => '2009-07-01 00:00:00' ) );
46+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'comment-test', 'post_date' => '2009-08-01 00:00:00' ) );
47+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'one-trackback', 'post_date' => '2009-09-01 00:00:00' ) );
48+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'many-trackbacks', 'post_date' => '2009-10-01 00:00:00' ) );
49+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'no-comments', 'post_date' => '2009-10-01 00:00:00' ) );
50+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'one-comment', 'post_date' => '2009-11-01 00:00:00' ) );
51+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'contributor-post-approved', 'post_date' => '2009-12-01 00:00:00' ) );
52+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'embedded-video', 'post_date' => '2010-01-01 00:00:00' ) );
53+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'simple-markup-test', 'post_date' => '2010-02-01 00:00:00' ) );
54+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'raw-html-code', 'post_date' => '2010-03-01 00:00:00' ) );
55+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'tags-a-b-c', 'tags_input' => array( 'tag-a', 'tag-b', 'tag-c' ), 'post_date' => '2010-04-01 00:00:00' ) );
56+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'tag-a', 'tags_input' => array( 'tag-a' ), 'post_date' => '2010-05-01 00:00:00' ) );
57+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'tag-b', 'tags_input' => array( 'tag-b' ), 'post_date' => '2010-06-01 00:00:00' ) );
58+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'tag-c', 'tags_input' => array( 'tag-c' ), 'post_date' => '2010-07-01 00:00:00' ) );
59+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'tags-a-and-b', 'tags_input' => array( 'tag-a', 'tag-b' ), 'post_date' => '2010-08-01 00:00:00' ) );
60+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'tags-b-and-c', 'tags_input' => array( 'tag-b', 'tag-c' ), 'post_date' => '2010-09-01 00:00:00' ) );
61+
self::$post_ids[] = $factory->post->create( array( 'post_title' => 'tags-a-and-c', 'tags_input' => array( 'tag-a', 'tag-c' ), 'post_date' => '2010-10-01 00:00:00' ) );
62+
63+
self::$post_ids[] = self::$parent_one = $factory->post->create( array( 'post_title' => 'parent-one', 'post_date' => '2007-01-01 00:00:00' ) );
64+
self::$post_ids[] = self::$parent_two = $factory->post->create( array( 'post_title' => 'parent-two', 'post_date' => '2007-01-01 00:00:00' ) );
65+
self::$post_ids[] = self::$parent_three = $factory->post->create( array( 'post_title' => 'parent-three', 'post_date' => '2007-01-01 00:00:00' ) );
66+
self::$post_ids[] = self::$child_one = $factory->post->create( array( 'post_title' => 'child-one', 'post_parent' => self::$parent_one, 'post_date' => '2007-01-01 00:00:01' ) );
67+
self::$post_ids[] = self::$child_two = $factory->post->create( array( 'post_title' => 'child-two', 'post_parent' => self::$parent_one, 'post_date' => '2007-01-01 00:00:02' ) );
68+
self::$post_ids[] = self::$child_three = $factory->post->create( array( 'post_title' => 'child-three', 'post_parent' => self::$parent_two, 'post_date' => '2007-01-01 00:00:03' ) );
69+
self::$post_ids[] = self::$child_four = $factory->post->create( array( 'post_title' => 'child-four', 'post_parent' => self::$parent_two, 'post_date' => '2007-01-01 00:00:04' ) );
70+
71+
self::commit_transaction();
72+
}
73+
74+
public static function tearDownAfterClass() {
75+
foreach ( self::$cat_ids as $cat_id ) {
76+
wp_delete_term( $cat_id, 'category' );
77+
}
78+
79+
foreach ( self::$tag_ids as $tag_id ) {
80+
wp_delete_term( $tag_id, 'post_tag' );
81+
}
82+
83+
foreach ( self::$post_ids as $post_id ) {
84+
wp_delete_post( $post_id, true );
85+
}
86+
87+
self::commit_transaction();
88+
}
89+
1390
function setUp() {
1491
parent::setUp();
1592

16-
$cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
17-
$cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
18-
$cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-c' ) );
19-
20-
$this->factory->post->create( array( 'post_title' => 'tag-נ', 'tags_input' => array( 'tag-נ' ), 'post_date' => '2008-11-01 00:00:00' ) );
21-
$this->factory->post->create( array( 'post_title' => 'cats-a-b-c', 'post_date' => '2008-12-01 00:00:00', 'post_category' => array( $cat_a, $cat_b, $cat_c ) ) );
22-
$this->factory->post->create( array( 'post_title' => 'cats-a-and-b', 'post_date' => '2009-01-01 00:00:00', 'post_category' => array( $cat_a, $cat_b ) ) );
23-
$this->factory->post->create( array( 'post_title' => 'cats-b-and-c', 'post_date' => '2009-02-01 00:00:00', 'post_category' => array( $cat_b, $cat_c ) ) );
24-
$this->factory->post->create( array( 'post_title' => 'cats-a-and-c', 'post_date' => '2009-03-01 00:00:00', 'post_category' => array( $cat_a, $cat_c ) ) );
25-
$this->factory->post->create( array( 'post_title' => 'cat-a', 'post_date' => '2009-04-01 00:00:00', 'post_category' => array( $cat_a ) ) );
26-
$this->factory->post->create( array( 'post_title' => 'cat-b', 'post_date' => '2009-05-01 00:00:00', 'post_category' => array( $cat_b ) ) );
27-
$this->factory->post->create( array( 'post_title' => 'cat-c', 'post_date' => '2009-06-01 00:00:00', 'post_category' => array( $cat_c ) ) );
28-
$this->factory->post->create( array( 'post_title' => 'lorem-ipsum', 'post_date' => '2009-07-01 00:00:00' ) );
29-
$this->factory->post->create( array( 'post_title' => 'comment-test', 'post_date' => '2009-08-01 00:00:00' ) );
30-
$this->factory->post->create( array( 'post_title' => 'one-trackback', 'post_date' => '2009-09-01 00:00:00' ) );
31-
$this->factory->post->create( array( 'post_title' => 'many-trackbacks', 'post_date' => '2009-10-01 00:00:00' ) );
32-
$this->factory->post->create( array( 'post_title' => 'no-comments', 'post_date' => '2009-10-01 00:00:00' ) );
33-
$this->factory->post->create( array( 'post_title' => 'one-comment', 'post_date' => '2009-11-01 00:00:00' ) );
34-
$this->factory->post->create( array( 'post_title' => 'contributor-post-approved', 'post_date' => '2009-12-01 00:00:00' ) );
35-
$this->factory->post->create( array( 'post_title' => 'embedded-video', 'post_date' => '2010-01-01 00:00:00' ) );
36-
$this->factory->post->create( array( 'post_title' => 'simple-markup-test', 'post_date' => '2010-02-01 00:00:00' ) );
37-
$this->factory->post->create( array( 'post_title' => 'raw-html-code', 'post_date' => '2010-03-01 00:00:00' ) );
38-
$this->factory->post->create( array( 'post_title' => 'tags-a-b-c', 'tags_input' => array( 'tag-a', 'tag-b', 'tag-c' ), 'post_date' => '2010-04-01 00:00:00' ) );
39-
$this->factory->post->create( array( 'post_title' => 'tag-a', 'tags_input' => array( 'tag-a' ), 'post_date' => '2010-05-01 00:00:00' ) );
40-
$this->factory->post->create( array( 'post_title' => 'tag-b', 'tags_input' => array( 'tag-b' ), 'post_date' => '2010-06-01 00:00:00' ) );
41-
$this->factory->post->create( array( 'post_title' => 'tag-c', 'tags_input' => array( 'tag-c' ), 'post_date' => '2010-07-01 00:00:00' ) );
42-
$this->factory->post->create( array( 'post_title' => 'tags-a-and-b', 'tags_input' => array( 'tag-a', 'tag-b' ), 'post_date' => '2010-08-01 00:00:00' ) );
43-
$this->factory->post->create( array( 'post_title' => 'tags-b-and-c', 'tags_input' => array( 'tag-b', 'tag-c' ), 'post_date' => '2010-09-01 00:00:00' ) );
44-
$this->factory->post->create( array( 'post_title' => 'tags-a-and-c', 'tags_input' => array( 'tag-a', 'tag-c' ), 'post_date' => '2010-10-01 00:00:00' ) );
45-
46-
$this->parent_one = $this->factory->post->create( array( 'post_title' => 'parent-one', 'post_date' => '2007-01-01 00:00:00' ) );
47-
$this->parent_two = $this->factory->post->create( array( 'post_title' => 'parent-two', 'post_date' => '2007-01-01 00:00:00' ) );
48-
$this->parent_three = $this->factory->post->create( array( 'post_title' => 'parent-three', 'post_date' => '2007-01-01 00:00:00' ) );
49-
$this->child_one = $this->factory->post->create( array( 'post_title' => 'child-one', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:01' ) );
50-
$this->child_two = $this->factory->post->create( array( 'post_title' => 'child-two', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:02' ) );
51-
$this->child_three = $this->factory->post->create( array( 'post_title' => 'child-three', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:03' ) );
52-
$this->child_four = $this->factory->post->create( array( 'post_title' => 'child-four', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:04' ) );
53-
5493
unset( $this->q );
5594
$this->q = new WP_Query();
5695
}
@@ -308,7 +347,7 @@ function test_query_offset_and_paged() {
308347
function test_query_post_parent__in() {
309348
// Query for first parent's children
310349
$posts = $this->q->query( array(
311-
'post_parent__in' => array( $this->parent_one ),
350+
'post_parent__in' => array( self::$parent_one ),
312351
'orderby' => 'date',
313352
'order' => 'asc',
314353
) );
@@ -320,7 +359,7 @@ function test_query_post_parent__in() {
320359

321360
// Second parent's children
322361
$posts = $this->q->query( array(
323-
'post_parent__in' => array( $this->parent_two ),
362+
'post_parent__in' => array( self::$parent_two ),
324363
'orderby' => 'date',
325364
'order' => 'asc',
326365
) );
@@ -332,7 +371,7 @@ function test_query_post_parent__in() {
332371

333372
// Both first and second parent's children
334373
$posts = $this->q->query( array(
335-
'post_parent__in' => array( $this->parent_one, $this->parent_two ),
374+
'post_parent__in' => array( self::$parent_one, self::$parent_two ),
336375
'orderby' => 'date',
337376
'order' => 'asc',
338377
) );
@@ -346,7 +385,7 @@ function test_query_post_parent__in() {
346385

347386
// Third parent's children
348387
$posts = $this->q->query( array(
349-
'post_parent__in' => array( $this->parent_three ),
388+
'post_parent__in' => array( self::$parent_three ),
350389
) );
351390

352391
$this->assertEquals( array(), wp_list_pluck( $posts, 'post_title' ) );
@@ -357,7 +396,7 @@ function test_query_post_parent__in() {
357396
*/
358397
function test_query_orderby_post_parent__in() {
359398
$posts = $this->q->query( array(
360-
'post_parent__in' => array( $this->parent_two, $this->parent_one ),
399+
'post_parent__in' => array( self::$parent_two, self::$parent_one ),
361400
'orderby' => 'post_parent__in',
362401
'order' => 'asc',
363402
) );
@@ -376,8 +415,8 @@ function test_query_orderby_post_parent__in() {
376415
function test_query_fields_integers() {
377416

378417
$parents = array(
379-
(int) $this->parent_one,
380-
(int) $this->parent_two
418+
(int) self::$parent_one,
419+
(int) self::$parent_two
381420
);
382421
$posts1 = $this->q->query( array(
383422
'post__in' => $parents,
@@ -388,8 +427,8 @@ function test_query_fields_integers() {
388427
$this->assertSame( $parents, $posts1 );
389428

390429
$children = array(
391-
(int) $this->child_one => (int) $this->parent_one,
392-
(int) $this->child_two => (int) $this->parent_one
430+
(int) self::$child_one => (int) self::$parent_one,
431+
(int) self::$child_two => (int) self::$parent_one
393432
);
394433

395434
$posts2 = $this->q->query( array(

0 commit comments

Comments
 (0)