Skip to content

Instantly share code, notes, and snippets.

@aaroneaton
Last active September 16, 2016 02:58
Show Gist options
  • Select an option

  • Save aaroneaton/9701eef8c3b18d3f9c9bd9191f47f012 to your computer and use it in GitHub Desktop.

Select an option

Save aaroneaton/9701eef8c3b18d3f9c9bd9191f47f012 to your computer and use it in GitHub Desktop.

Revisions

  1. J. Aaron Eaton revised this gist Sep 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    # Check out the full article on [channeleaton.com](http://channeleaton.com)
    # Check out the full article on [channeleaton.com](http://channeleaton.com/2016/09/using-wp_mock-phpspec/)
  2. J. Aaron Eaton revised this gist Sep 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion testing-comparison.php
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,6 @@ public function let() {

    public function it_can_complete_todo() {
    $this->markCompleted();
    $this->isComplete()->shouldBe(true);
    $this->isComplete()->shouldReturn(true);
    }
    }
  3. J. Aaron Eaton revised this gist Sep 16, 2016. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion todo-spec.php
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,11 @@ public function letGo() {
    }

    public function it_can_create_todo() {
    //We are expecting that the `save()` method will call `wp_insert_post`
    /**
    * We are expecting that the `save()` method will call `wp_insert_post()`.
    * If `wp_insert_post()` wasn't actually called an error would be thrown
    * during the test.
    */
    \WP_Mock::wpFunction( 'wp_insert_post', array(
    'args' => [['post_title' => 'Write a blog post', 'post_status' => 'publish'], true],
    'times' => 1,
  4. J. Aaron Eaton revised this gist Sep 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion todo-spec.php
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ public function __construct( $title ) {
    }

    public function save() {
    return wp_insert_post(['post_title' => 'Write a blog post', 'post_status' => 'publish'], true);
    return wp_insert_post(['post_title' => $this->title, 'post_status' => 'publish'], true);
    }
    }

  5. J. Aaron Eaton revised this gist Sep 16, 2016. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion testing-comparison.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    */
    class TodoTest extends TestCase {
    public function testCanCompleteTodo() {
    $todo = new Todo();
    $todo = new Todo('Write a blog post');
    $todo->markCompleted();
    $this->assertTrue($todo->isComplete());
    }
    @@ -15,6 +15,10 @@ public function testCanCompleteTodo() {
    * The same test in PhpSpec
    */
    class TodoSpec extends ObjectBehavior {
    public function let() {
    $this->beConstructedWith('Write a blog post');
    }

    public function it_can_complete_todo() {
    $this->markCompleted();
    $this->isComplete()->shouldBe(true);
  6. J. Aaron Eaton created this gist Sep 16, 2016.
    11 changes: 11 additions & 0 deletions phpspec-setup.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?php

    class TodoSpec extends ObjectBehavior {
    public function let() {
    \WP_Mock::setUp();
    }

    public function letGo() {
    \WP_Mock::tearDown();
    }
    }
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    # Check out the full article on [channeleaton.com](http://channeleaton.com)
    22 changes: 22 additions & 0 deletions testing-comparison.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php

    /**
    * A test written with PHPUnit
    */
    class TodoTest extends TestCase {
    public function testCanCompleteTodo() {
    $todo = new Todo();
    $todo->markCompleted();
    $this->assertTrue($todo->isComplete());
    }
    }

    /**
    * The same test in PhpSpec
    */
    class TodoSpec extends ObjectBehavior {
    public function it_can_complete_todo() {
    $this->markCompleted();
    $this->isComplete()->shouldBe(true);
    }
    }
    35 changes: 35 additions & 0 deletions todo-spec.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php

    class Todo {
    private $title;

    public function __construct( $title ) {
    $this->title = $title;
    }

    public function save() {
    return wp_insert_post(['post_title' => 'Write a blog post', 'post_status' => 'publish'], true);
    }
    }

    class TodoSpec extends ObjectBehavior {
    public function let() {
    \WP_Mock::setUp();
    }

    public function letGo() {
    \WP_Mock::tearDown();
    }

    public function it_can_create_todo() {
    //We are expecting that the `save()` method will call `wp_insert_post`
    \WP_Mock::wpFunction( 'wp_insert_post', array(
    'args' => [['post_title' => 'Write a blog post', 'post_status' => 'publish'], true],
    'times' => 1,
    'return' => 42
    ) );

    // Now run Todo::save() and make sure it returns the post (todo) ID
    $this->save()->shouldReturn(42);
    }
    }