1

(I'm trying to solve style problems of code samples)

I'm developing a theme but I have some classes which developed for this theme. I want to run unit tests on my theme.

I've followed this steps:

  1. Checked out test repo to my root web folder:

    svn co http://develop.svn.wordpress.org/trunk/ wordpress-develop
    
  2. Copied my theme folder to wordpress-develop/src/wp-content/themes

  3. Created an empty database and set up wp-tests-config.php file. Also added this line to wp-tests-config.php

    define( 'WP_DEFAULT_THEME', 'THEMENAME' );
    
  4. Created wordpress-develop/tests/phpunit/tests/konser.php file with this content:

    <?php
    class Test_Konser extends WP_UnitTestCase {
    
    public function test_firstTest(){
         // Arrange
            $id = $this->factory->post->create(array(
                    'post_type' => 'konser',
                ));
            add_post_meta($id, "konser_tarih", time());
         $ids = get_posts(array(
                'post_type' => 'konser',
                )
            );
         foreach ($ids as $id) {
         $this->assertInstanceOf('WP_Post',$id);
        $ss = get_post_meta($id->ID, "konser_tarih", true);
        $this->assertTrue($ss!='');
        }
    }
    }
    

    enter image description here

  5. But I want to use my Konser and PostType classes.

    function setUp() {
    
       $this->post_id = new Konser(410);
       parent::setUp();
    }
    function test_ID() {
        $this->assertInternalType("int", $this->post_id);
    }
    
  6. Added this 2 lines to /wordpress-develop/tests/phpunit/includes/functions.php

      require_once('src/wp-content/themes/THEMENAME/include/PostType.php');
      require_once('src/wp-content/themes/THEMENAME/include/Konser.php');
    
  7. But I'm getting Call to undefined function get_post() in konsertv-develop/src/wp-content/themes/THEMENAME/include/PostType.php

Can you tell me what I'm missing?

CONTENT OF PostType.php

  <?php
  class PostType
  {
public $post;
public $ID;

function PostType($ID) {
    $this->post = get_post($ID);
    $this->ID = $ID;
}
function getPost()
{
    return $this->post;
}
function getTarih()
{
    return get_post_meta($this->ID, "konser_tarih", true);
}
function getID()
{
    return $this->ID;
}

}

1 Answer 1

0

6 . Added this 2 lines to /wordpress-develop/tests/phpunit/includes/functions.php

require_once('src/wp-content/themes/THEMENAME/include/PostType.php'); require_once('src/wp-content/themes/THEMENAME/include/Konser.php');

That is the problem. The get_post() function doesn't exist, becuase the phpunit/includes/functions.php file is included before WordPress is loaded. That is actually part of the reason that it exists as a separate file: so it can be included in your own phpunit bootstrap (if you need one, and need to use those functions). For an example of how this is done for plugins, you can see a tutorial I've written.

However, in your case, you are testing a theme, and your test suite is not complex enough that it warrants its own bootstrap. Actually, your third step, adding define( 'WP_DEFAULT_THEME', 'THEMENAME' ); to the wp-tests-config.php should be sufficient. The two files you're requireing will automatically be loaded as part of your theme. (Yes, the theme is loaded during the unit tests.)

So your mistake was step 6. It isn't necessary.

1
  • Now I got it @J.D. Thank you for your detailed response.
    – Eray
    Jun 7, 2014 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.