wordpress-develop/tests/phpunit/tests/comment/dateQuery.php
Sergey Biryukov ddb409edca Build/Test Tools: Implement use of the void solution.
> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches.
>
> The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used.
>
> When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`.
>
> The snake_case methods will automatically be called by PHPUnit.
>
> > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`.

Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases

This commit renames all declared fixture methods, and calls to parent versions of those fixture methods, from camelCase to snake_case.

Follow-up to [51559-51567].

Props jrf, hellofromTonya, johnbillion, netweb, dd32, pputzer, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51568 602fd350-edb4-49c9-b593-d223f7449a82
2021-08-07 10:29:41 +00:00

83 lines
1.9 KiB
PHP

<?php
/**
* Tests to make sure querying posts based on various date parameters
* using "date_query" works as expected.
*
* No need to do a full repeat of all of the post tests again since
* the query SQL is the same for both just with a different column.
*
* @ticket 18694
*
* @group comment
* @group date
* @group datequery
*/
class Tests_Comment_DateQuery extends WP_UnitTestCase {
public $posts = array();
public function set_up() {
parent::set_up();
// Just some dummy posts to use as parents for comments.
for ( $i = 1; $i <= 2; $i++ ) {
$this->posts[ $i ] = self::factory()->post->create();
}
// Be careful modifying this. Tests are coded to expect this exact sample data.
// Format is 'datetime' => 'post number (not ID)'.
$comment_dates = array(
'2007-01-22 03:49:21' => 1,
'2007-05-16 17:32:22' => 1,
'2007-09-24 07:17:23' => 1,
'2008-03-29 09:04:25' => 1,
'2008-07-15 11:32:26' => 2, // This one should never be in the results.
'2008-12-10 13:06:27' => 1,
'2009-06-11 21:30:28' => 1,
'2009-12-18 10:42:29' => 1,
);
foreach ( $comment_dates as $comment_date => $comment_parent ) {
$result = self::factory()->comment->create(
array(
'comment_date' => $comment_date,
'comment_post_ID' => $this->posts[ $comment_parent ],
)
);
}
}
public function _get_query_result( $args = array() ) {
$args = wp_parse_args(
$args,
array(
'post_id' => $this->posts[1],
'orderby' => 'comment_ID', // Same order they were created.
'order' => 'ASC',
)
);
return get_comments( $args );
}
public function test_year() {
$comments = $this->_get_query_result(
array(
'date_query' => array(
array(
'year' => 2008,
),
),
)
);
$expected_dates = array(
'2008-03-29 09:04:25',
'2008-12-10 13:06:27',
);
$this->assertSame( $expected_dates, wp_list_pluck( $comments, 'comment_date' ) );
}
}