Last active
September 16, 2016 02:58
-
-
Save aaroneaton/9701eef8c3b18d3f9c9bd9191f47f012 to your computer and use it in GitHub Desktop.
Revisions
-
J. Aaron Eaton revised this gist
Sep 16, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ # Check out the full article on [channeleaton.com](http://channeleaton.com/2016/09/using-wp_mock-phpspec/) -
J. Aaron Eaton revised this gist
Sep 16, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()->shouldReturn(true); } } -
J. Aaron Eaton revised this gist
Sep 16, 2016 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()`. * 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, -
J. Aaron Eaton revised this gist
Sep 16, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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' => $this->title, 'post_status' => 'publish'], true); } } -
J. Aaron Eaton revised this gist
Sep 16, 2016 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ */ class TodoTest extends TestCase { public function testCanCompleteTodo() { $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); -
J. Aaron Eaton created this gist
Sep 16, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ # Check out the full article on [channeleaton.com](http://channeleaton.com) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } }