Passed
Push — master ( 192c4c...e2011a )
by Francis
01:14
created

BlogTest::setUpBeforeClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
use PHPUnit\Framework\TestCase;
4
5
final class BlogTest extends TestCase {
6
7
  /**
8
   * Code Igniter Instance.
9
   * @var object
10
   */
11
  private static $ci;
12
13
  /**
14
   * Prerquisites for the Unit Tests.
15
   */
16
  public static function setUpBeforeClass(): void {
17
    self::$ci =& get_instance();
18
    self::$ci->load->database('mysqli://root@localhost/test_db');
19
    $queries = [
20
      "CREATE TABLE IF NOT EXISTS admins (id INT(7) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL, password TEXT NOT NULL) Engine=InnoDB;",
21
      "INSERT INTO admins (id, name, password) VALUES (1, \"Dev\", \"does_not_matter_for_this_test\");"
22
    ];
23
    foreach ($queries as $query) {
24
      self::assertTrue(self::$ci->db->query($query), "$query, Ran sucessfully.");
25
    }
26
    self::$ci->load->splint("francis94c/blog", "+Blogger", null, "blogger");
27
  }
28
  /**
29
   * Test all functions relating to the installation of a blog. this is just the
30
   * creation of tables under the hood.
31
   */
32
  public function testInstallBlog(): void {
33
    $this->assertTrue(self::$ci->blogger->install("test_blog"), "Blog Installed Successfuly without admin ID constraint.");
34
    $this->assertTrue(self::$ci->db->table_exists(Blogger::TABLE_PREFIX . "_test_blog"));
35
    $fields = self::$ci->db->list_fields(Blogger::TABLE_PREFIX . "_test_blog");
36
    $this->assertContains("id", $fields);
37
    $this->assertContains("title", $fields);
38
    $this->assertContains("content", $fields);
39
    $this->assertContains("slug", $fields);
40
    $this->assertContains("date_created", $fields);
41
    $this->assertContains("date_published", $fields);
42
    $this->assertTrue(self::$ci->blogger->install("test_blog"), "Verify CREATE IF NOT EXISTS clause");
43
    $this->assertTrue(self::$ci->blogger->install("admin_test_blog", "admins", "id", 7), "Create Blog with existent admin constarint");
44
    $fields = self::$ci->db->list_fields(Blogger::TABLE_PREFIX . "_admin_test_blog");
45
    $this->assertContains("id", $fields);
46
    $this->assertContains("title", $fields);
47
    $this->assertContains("content", $fields);
48
    $this->assertContains("slug", $fields);
49
    $this->assertContains("date_created", $fields);
50
    $this->assertContains("date_published", $fields);
51
    $this->assertContains("poster_id", $fields);
52
  }
53
  /**
54
   * Test UI functions. This just out pust  HTML for manual inspection. The optimal
55
   * inspection for this part is to use the Code Igniter Unit Testing system that
56
   * outputs to a browser. See https://splint.cynobit/wiki
57
   *
58
   * @depends testInstallBlog
59
   */
60
  public function testUI(): void {
61
    $this->assertTrue(self::$ci->blogger->loadEditor("callback"), "Load Editor");
62
    self::$ci->blogger->setBlog("test_blog");
63
    $this->assertTrue(self::$ci->blogger->renderPostItems(null, null, null, 1, 0), "Test load empty posts set");
64
  }
65
  /**
66
   * Test the blog post saving functionality of the library.
67
   * Create, Save, Publish, Create and Publish
68
   * @depends testInstallBlog
69
   */
70
  public function testBlogSave(): void {
71
    // No Admin.
72
    self::$ci->blogger->setBlog("test_blog");
73
    $_POST["action"] = "save";
74
    $_POST["title"] = "Hello Title";
75
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog.";
76
    $this->assertEquals(self::$ci->blogger->savePost(), Blogger::CREATE);
77
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog. Again.";
78
    $_POST["id"] = 1;
79
    $this->assertEquals(self::$ci->blogger->savePost(), Blogger::EDIT);
80
    $this->assertTrue(self::$ci->blogger->renderPost("Hello-Title", null));
81
    $post = self::$ci->blogger->getPost("Hello-Title", false);
82
    $this->assertTrue(is_array($post));
83
    $this->assertArrayHasKey("id", $post);
84
    $this->assertArrayHasKey("title", $post);
85
    $this->assertArrayHasKey("content", $post);
86
    $this->assertArrayHasKey("published", $post);
87
    $this->assertArrayHasKey("date_published", $post);
88
    $this->assertArrayHasKey("slug", $post);
89
    $this->assertEquals(1, $post["id"], "Assert Post ID");
90
    $this->assertEquals("Hello Title", $post["title"], "Assert Post Title");
91
    $this->assertEquals("The Quick Brown Fox Jumped over the Lazy Dog. Again.", $post["content"]);
92
    $this->assertEquals("Hello-Title", $post["slug"]);
93
    $this->assertEquals(0, $post["published"]);
94
    $this->assertEquals(null, $post["date_published"]);
95
    $_POST["action"] = "publish";
96
    $this->assertEquals(self::$ci->blogger->savePost(), Blogger::PUBLISH);
97
    $post = self::$ci->blogger->getPost("Hello-Title", false);
98
    $this->assertTrue(is_array($post));
99
    $this->assertEquals(1, $post["published"]);
100
    $this->assertNotEquals(null, $post["date_published"]);
101
    $_POST["action"] = "createAndPublish";
102
    $_POST["title"] = "Hello Title 2";
103
    $_POST["editor"] = "Create and Published Post.";
104
    unset($_POST["id"]);
105
    $this->assertEquals(Blogger::CREATE_AND_PUBLISH, self::$ci->blogger->savePost());
106
    $post = self::$ci->blogger->getPost("Hello-Title-2", false);
107
    $this->assertTrue(is_array($post));
108
    $this->assertArrayHasKey("id", $post);
109
    $this->assertArrayHasKey("title", $post);
110
    $this->assertArrayHasKey("content", $post);
111
    $this->assertArrayHasKey("published", $post);
112
    $this->assertArrayHasKey("date_published", $post);
113
    $this->assertArrayHasKey("slug", $post);
114
    $this->assertEquals(2, $post["id"], "Assert Post ID");
115
    $this->assertEquals("Hello Title 2", $post["title"], "Assert Post Title");
116
    $this->assertEquals("Create and Published Post.", $post["content"]);
117
    $this->assertEquals("Hello-Title-2", $post["slug"]);
118
    $this->assertEquals(1, $post["published"]);
119
    $this->assertNotEquals(null, $post["date_published"]);
120
    $this->assertEquals(Blogger::ABORT, self::$ci->blogger->savePost(), "No 2 blog posts can have the same title.");
121
    // TODO: With Admin.
122
    self::$ci->blogger->setBlog("admin_test_blog");
123
    $_POST["action"] = "save";
124
    $_POST["title"] = "Admin Hello Title";
125
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog.";
126
    unset($_POST["id"]);
127
    $this->assertEquals(self::$ci->blogger->savePost(1), Blogger::CREATE);
128
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog. Again.";
129
    $_POST["id"] = 1;
130
    $this->assertEquals(self::$ci->blogger->savePost(1), Blogger::EDIT);
131
    $this->assertTrue(self::$ci->blogger->renderPost("Admin-Hello-Title", null));
132
    $post = self::$ci->blogger->getPost("Admin-Hello-Title", false);
133
    $this->assertTrue(is_array($post));
134
    $this->assertArrayHasKey("id", $post);
135
    $this->assertArrayHasKey("title", $post);
136
    $this->assertArrayHasKey("content", $post);
137
    $this->assertArrayHasKey("published", $post);
138
    $this->assertArrayHasKey("date_published", $post);
139
    $this->assertArrayHasKey("slug", $post);
140
    $this->assertEquals(1, $post["id"], "Assert Post ID");
141
    $this->assertEquals("Admin Hello Title", $post["title"], "Assert Post Title");
142
    $this->assertEquals("The Quick Brown Fox Jumped over the Lazy Dog. Again.", $post["content"]);
143
    $this->assertEquals("Admin-Hello-Title", $post["slug"]);
144
    $this->assertEquals(0, $post["published"]);
145
    $this->assertEquals(null, $post["date_published"]);
146
    $_POST["action"] = "publish";
147
    $this->assertEquals(self::$ci->blogger->savePost(1), Blogger::PUBLISH);
148
    $post = self::$ci->blogger->getPost("Admin-Hello-Title", false);
149
    $this->assertTrue(is_array($post));
150
    $this->assertEquals(1, $post["published"]);
151
    $this->assertNotEquals(null, $post["date_published"]);
152
    $_POST["action"] = "createAndPublish";
153
    $_POST["title"] = "Admin Hello Title 2";
154
    $_POST["editor"] = "Create and Published Post.";
155
    unset($_POST["id"]);
156
    $this->assertEquals(Blogger::CREATE_AND_PUBLISH, self::$ci->blogger->savePost(1));
157
    $post = self::$ci->blogger->getPost("Admin-Hello-Title-2", false);
158
    $this->assertTrue(is_array($post));
159
    $this->assertArrayHasKey("id", $post);
160
    $this->assertArrayHasKey("title", $post);
161
    $this->assertArrayHasKey("content", $post);
162
    $this->assertArrayHasKey("published", $post);
163
    $this->assertArrayHasKey("date_published", $post);
164
    $this->assertArrayHasKey("slug", $post);
165
    $this->assertArrayHasKey("poster_id", $post);
166
    $this->assertEquals(2, $post["id"], "Assert Post ID");
167
    $this->assertEquals("Admin Hello Title 2", $post["title"], "Assert Post Title");
168
    $this->assertEquals("Create and Published Post.", $post["content"]);
169
    $this->assertEquals("Admin-Hello-Title-2", $post["slug"]);
170
    $this->assertEquals(1, $post["published"]);
171
    $this->assertNotEquals(null, $post["date_published"]);
172
    $this->assertEquals(Blogger::ABORT, self::$ci->blogger->savePost(1), "No 2 blog posts can have the same title.");
173
  }
174
  /**
175
   * Test Setters and Getters.
176
   */
177
  public function testDynamicFunctions(): void {
178
    self::$ci->blogger->setBlog("rocket_blog");
179
    $this->assertEquals(Blogger::TABLE_PREFIX . "_rocket_blog", self::$ci->blogger->getName(), "Blogger setBlog works.");
180
  }
181
  /**
182
   * Clear and Free up persistent used resources for this test class.
183
   */
184
  public static function tearDownAfterClass(): void {
185
    self::$ci->db->empty_table("blogger_posts_test_blog");
186
    self::$ci->db->empty_table("blogger_posts_admin_test_blog");
187
    self::$ci->db->empty_table("admins");
188
    self::$ci->load->dbforge();
189
    self::$ci->dbforge->drop_table("blogger_posts_test_blog");
190
    self::$ci->dbforge->drop_table("blogger_posts_admin_test_blog");
191
    self::$ci->dbforge->drop_table("admins");
192
    self::$ci->db->close();
193
  }
194
}
195