-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordPressTest.php
110 lines (95 loc) · 2.92 KB
/
WordPressTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Idearia\WpTests;
use Idearia\WpTests\WordPressTestCase;
/**
* Test the creation and deletion of a WordPress post.
*/
class WordPressTest extends WordPressTestCase
{
/**
* You can share variables between tests! This is the name
* of the post that will be created.
*/
protected static ?string $postTitle = 'Test post';
/**
* This is the postmeta that will be attached to the created post.
*/
protected static ?string $postMetaKey = 'test-meta';
protected static ?string $postMetaValue = 'Test post';
/**
* Your class variables can be initiated at run-time. This variable
* will contain the post that will be created.
*/
protected static ?\WP_Post $createdPost = null;
/**
* Create a post
*/
public function testCreatePost(): int
{
$postId = wp_insert_post([
'post_title' => self::$postTitle,
'post_status' => 'publish',
'meta_input' => [
self::$postMetaKey => self::$postMetaValue,
],
]);
$this->assertIsInt($postId);
$this->assertGreaterThan(0, $postId);
return $postId;
}
/**
* @depends testCreatePost
*
* Fetch the created post to make sure it exists in the
* database.
*
* The 'depends' line above ensures two things:
* 1. that this test is run after testCreatePost, and
* 2. that this test receives the output of testCreatePost
* as a paramter.
*/
public function testGetPost(int $postId): void
{
self::$createdPost = get_post($postId);
$this->assertInstanceOf(\WP_Post::Class, self::$createdPost);
$this->assertEquals(self::$postTitle, self::$createdPost->post_title);
}
/**
* @depends testCreatePost
*
* Make sure the post meta was created succesfully
*/
public function testGetPostmeta(int $postId)
{
$postMeta = get_post_meta($postId, self::$postMetaKey, true);
$this->assertEquals(self::$postMetaValue, $postMeta);
}
/**
* @depends testGetPost
* @depends testGetPostmeta
*
* Delete the created post
*/
public function testDeletePost()
{
$deletedPost = wp_delete_post(self::$createdPost->ID, true);
$this->assertInstanceOf(\WP_Post::class, $deletedPost);
}
/**
* Clean up any mess we could have made with the tests.
*
* In particular, we delete the post in the event it was created
* but it could not be deleted.
*
* This function is called regardless of whether the tests
* succeeded or failed; more details here:
* - https://phpunit.readthedocs.io/en/latest/fixtures.html
*/
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
if (self::$createdPost instanceof \WP_Post) {
wp_delete_post(self::$createdPost->ID, true);
}
}
}