WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/15/2015 04:43:37 AM (5 years ago)
Author:
wonderboymusic
Message:

Unit Tests: implement setUpBeforeClass() and tearDownAfterClass() on WP_UnitTestCase. Use late static binding (plus a gross fallback for PHP 5.2) to check if wpSetUpBeforeClass() or wpTearDownAfterClass() exist on the called class, and then call it and pass a static WP_UnitTest_Factory instance via Dependency Injection, if it exists.

This makes it way easier to add fixtures, and tear them down, without needing to instantiate WP_UnitTest_Factory in every class - removes the need to call commit_transaction() in each individual class.

See #30017, #33968.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions/getArchives.php

    r35163 r35186  
    1919    }
    2020
    21     public static function setUpBeforeClass() {
    22         $factory = new WP_UnitTest_Factory();
     21    public static function wpSetUpBeforeClass( $factory ) {
    2322        self::$post_ids = $factory->post->create_many( 8, array( 'post_type' => 'post', 'post_author' => '1' ) );
    24 
    25         self::commit_transaction();
    2623    }
    2724
    28     public static function tearDownAfterClass() {
     25    public static function wpTearDownAfterClass() {
    2926        foreach ( self::$post_ids as $post_id ) {
    3027            wp_delete_post( $post_id, true );
    3128        }
    32 
    33         self::commit_transaction();
    3429    }
    3530
     
    5348        $link5 = get_permalink( $ids[4] );
    5449
     50        $title1 = get_post( $ids[0] )->post_title;
     51        $title2 = get_post( $ids[1] )->post_title;
     52        $title3 = get_post( $ids[2] )->post_title;
     53        $title4 = get_post( $ids[3] )->post_title;
     54        $title5 = get_post( $ids[4] )->post_title;
     55
    5556        $expected['limit'] = <<<EOF
    56 <li><a href='$link1'>Post title 8</a></li>
    57     <li><a href='$link2'>Post title 7</a></li>
    58     <li><a href='$link3'>Post title 6</a></li>
    59     <li><a href='$link4'>Post title 5</a></li>
    60     <li><a href='$link5'>Post title 4</a></li>
     57<li><a href='$link1'>$title1</a></li>
     58    <li><a href='$link2'>$title2</a></li>
     59    <li><a href='$link3'>$title3</a></li>
     60    <li><a href='$link4'>$title4</a></li>
     61    <li><a href='$link5'>$title5</a></li>
    6162EOF;
    6263        $this->assertEquals( $expected['limit'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'postbypost', 'limit' => 5 ) ) ) );
Note: See TracChangeset for help on using the changeset viewer.