Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (10 years ago)
Author:
wonderboymusic
Message:

Unit Tests: one $factory to rule them all, and it shall be static.

Using more than one instance of WP_UnitTest_Factory causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use setUpBeforeClass, we were creating ad hoc instances. To avoid that, we were injecting one static instance via Dependency Injection in wpSetUpBeforeClass. All tests should really use the static instance, so we will remove the instance prop $factory.

Replace $this->factory with self::$factory over 2000 times.
Rewrite all of the tests that were hard-coding dynamic values.

#YOLOFriday

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/template.php

    r34950 r35225  
    99        $contents = array( 'One', 'Two', 'Three' );
    1010        $content = join( '<!--nextpage-->', $contents );
    11         $post_id = $this->factory->post->create( array( 'post_content' => $content ) );
     11        $post_id = self::$factory->post->create( array( 'post_content' => $content ) );
    1212
    1313        $this->go_to( '?p=' . $post_id );
     
    8282
    8383        $bump = '&nbsp;&nbsp;&nbsp;';
    84         $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
    85         $child_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id ) );
    86         $grandchild_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $child_id ) );
     84        $page_id = self::$factory->post->create( array( 'post_type' => 'page' ) );
     85        $child_id = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id ) );
     86        $grandchild_id = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $child_id ) );
     87
     88        $title1 = get_post( $page_id )->post_title;
     89        $title2 = get_post( $child_id )->post_title;
     90        $title3 = get_post( $grandchild_id )->post_title;
    8791
    8892        $lineage =<<<LINEAGE
    8993<select name='page_id' id='page_id'>
    90     <option class="level-0" value="$page_id">Post title 1</option>
    91     <option class="level-1" value="$child_id">{$bump}Post title 2</option>
    92     <option class="level-2" value="$grandchild_id">{$bump}{$bump}Post title 3</option>
     94    <option class="level-0" value="$page_id">$title1</option>
     95    <option class="level-1" value="$child_id">{$bump}$title2</option>
     96    <option class="level-2" value="$grandchild_id">{$bump}{$bump}$title3</option>
    9397</select>
    9498
     
    100104        $depth =<<<DEPTH
    101105<select name='page_id' id='page_id'>
    102     <option class="level-0" value="$page_id">Post title 1</option>
     106    <option class="level-0" value="$page_id">$title1</option>
    103107</select>
    104108
     
    111115<select name='page_id' id='page_id'>
    112116    <option value="Woo">Hoo</option>
    113     <option class="level-0" value="$page_id">Post title 1</option>
     117    <option class="level-0" value="$page_id">$title1</option>
    114118</select>
    115119
     
    125129    <option value="-1">Burrito</option>
    126130    <option value="Woo">Hoo</option>
    127     <option class="level-0" value="$page_id">Post title 1</option>
     131    <option class="level-0" value="$page_id">$title1</option>
    128132</select>
    129133
     
    140144     */
    141145    public function test_wp_dropdown_pages_value_field_should_default_to_ID() {
    142         $p = $this->factory->post->create( array(
     146        $p = self::$factory->post->create( array(
    143147            'post_type' => 'page',
    144148        ) );
     
    156160     */
    157161    public function test_wp_dropdown_pages_value_field_ID() {
    158         $p = $this->factory->post->create( array(
     162        $p = self::$factory->post->create( array(
    159163            'post_type' => 'page',
    160164        ) );
     
    172176     */
    173177    public function test_wp_dropdown_pages_value_field_post_name() {
    174         $p = $this->factory->post->create( array(
     178        $p = self::$factory->post->create( array(
    175179            'post_type' => 'page',
    176180            'post_name' => 'foo',
     
    189193     */
    190194    public function test_wp_dropdown_pages_value_field_should_fall_back_on_ID_when_an_invalid_value_is_provided() {
    191         $p = $this->factory->post->create( array(
     195        $p = self::$factory->post->create( array(
    192196            'post_type' => 'page',
    193197            'post_name' => 'foo',
     
    206210     */
    207211    public function test_wp_dropdown_pages_should_not_contain_class_attribute_when_no_class_is_passed() {
    208         $p = $this->factory->post->create( array(
     212        $p = self::$factory->post->create( array(
    209213            'post_type' => 'page',
    210214            'post_name' => 'foo',
     
    222226     */
    223227    public function test_wp_dropdown_pages_should_obey_class_parameter() {
    224         $p = $this->factory->post->create( array(
     228        $p = self::$factory->post->create( array(
    225229            'post_type' => 'page',
    226230            'post_name' => 'foo',
     
    239243     */
    240244    public function test_get_page_template_slug_by_id() {
    241         $page_id = $this->factory->post->create( array(
     245        $page_id = self::$factory->post->create( array(
    242246            'post_type' => 'page',
    243247        ) );
     
    256260     */
    257261    public function test_get_page_template_slug_from_loop() {
    258         $page_id = $this->factory->post->create( array(
     262        $page_id = self::$factory->post->create( array(
    259263            'post_type' => 'page',
    260264        ) );
     
    270274     */
    271275    public function test_get_page_template_slug_non_page() {
    272         $post_id = $this->factory->post->create( array(
     276        $post_id = self::$factory->post->create( array(
    273277            'post_type' => 'post',
    274278        ) );
     
    285289     */
    286290    public function test_wp_page_menu_wp_nav_menu_fallback() {
    287         $pages = $this->factory->post->create_many( 3, array( 'post_type' => 'page' ) );
     291        $pages = self::$factory->post->create_many( 3, array( 'post_type' => 'page' ) );
    288292
    289293        // No menus + wp_nav_menu() falls back to wp_page_menu().
Note: See TracChangeset for help on using the changeset viewer.