iterators.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * @ticket 22435
  4. */
  5. class Test_WP_Post_IDs_Iterator extends WP_UnitTestCase {
  6. function setUp() {
  7. if ( ! class_exists( 'WP_Post_IDs_Iterator' ) ) {
  8. $this->markTestSkipped( "WP_Post_IDs_Iterator class doesn't exist" );
  9. }
  10. parent::setUp();
  11. }
  12. function test_create() {
  13. new WP_Post_IDs_Iterator( array( 1, 2, 3 ) );
  14. }
  15. function test_no_posts() {
  16. $this->assertIteratorReturnsSamePostIDs( array() );
  17. }
  18. function test_less_ids_than_limit() {
  19. $post_id_0 = $this->factory->post->create();
  20. $post_id_1 = $this->factory->post->create();
  21. $this->assertIteratorReturnsSamePostIDs( array( $post_id_0, $post_id_1 ), 10 );
  22. }
  23. function test_ids_exactly_as_limit() {
  24. $post_id_0 = $this->factory->post->create();
  25. $post_id_1 = $this->factory->post->create();
  26. $this->assertIteratorReturnsSamePostIDs( array( $post_id_0, $post_id_1 ), 2 );
  27. }
  28. function test_more_ids_than_limit() {
  29. $post_id_0 = $this->factory->post->create();
  30. $post_id_1 = $this->factory->post->create();
  31. $post_id_2 = $this->factory->post->create();
  32. $this->assertIteratorReturnsSamePostIDs( array( $post_id_0, $post_id_1, $post_id_2 ), 2 );
  33. }
  34. function test_ids_exactly_twice_more_than_limit() {
  35. $post_id_0 = $this->factory->post->create();
  36. $post_id_1 = $this->factory->post->create();
  37. $post_id_2 = $this->factory->post->create();
  38. $post_id_3 = $this->factory->post->create();
  39. $this->assertIteratorReturnsSamePostIDs( array( $post_id_0, $post_id_1, $post_id_2, $post_id_3 ), 2 );
  40. }
  41. private function assertIteratorReturnsSamePostIDs( $post_ids, $limit = 2 ) {
  42. $this->assertEquals( $post_ids, wp_list_pluck( iterator_to_array( new WP_Post_IDs_Iterator( $post_ids, $limit ) ), 'ID' ) );
  43. }
  44. }