Passed
Push — master ( c32df4...4b10bf )
by Francis
01:12
created

BlogTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 89
c 9
b 0
f 0
dl 0
loc 132
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 11 2
A testUI() 0 4 1
A testBlogSave() 0 51 1
A testDynamicFunctions() 0 3 1
A tearDownAfterClass() 0 8 1
A testInstallBlog() 0 19 1
1
<?php
2
use PHPUnit\Framework\TestCase;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
3
4
final class BlogTest extends TestCase {
5
6
  /**
7
   * [private description]
8
   * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
9
   */
10
  private static $ci;
11
12
  /**
13
   * [setUp description]
14
   */
15
  public static function setUpBeforeClass(): void {
16
    self::$ci =& get_instance();
1 ignored issue
show
Bug introduced by
The function get_instance was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
    self::$ci =& /** @scrutinizer ignore-call */ get_instance();
Loading history...
17
    self::$ci->load->database('mysqli://root@localhost/test_db');
18
    $queries = [
19
      "CREATE TABLE IF NOT EXISTS admins (id INT(7) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL, password TEXT NOT NULL) Engine=InnoDB;",
20
      "INSERT INTO admins (id, name, password) VALUES (1, \"Dev\", \"does_not_matter_for_this_test\");"
21
    ];
22
    foreach ($queries as $query) {
23
      self::assertTrue(self::$ci->db->query($query), "$query, Ran sucessfully.");
24
    }
25
    self::$ci->load->splint("francis94c/blog", "+Blogger", null, "blogger");
26
  }
27
  /**
28
   * [testInstallBlog description]
29
   * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
30
   */
31
  public function testInstallBlog() {
32
    $this->assertTrue(self::$ci->blogger->install("test_blog"), "Blog Installed Successfuly without admin ID constraint.");
33
    $this->assertTrue(self::$ci->db->table_exists(Blogger::TABLE_PREFIX . "_test_blog"));
34
    $fields = self::$ci->db->list_fields(Blogger::TABLE_PREFIX . "_test_blog");
35
    $this->assertContains("id", $fields);
36
    $this->assertContains("title", $fields);
37
    $this->assertContains("content", $fields);
38
    $this->assertContains("slug", $fields);
39
    $this->assertContains("date_created", $fields);
40
    $this->assertContains("date_published", $fields);
41
    $this->assertTrue(self::$ci->blogger->install("test_blog"), "Verify CREATE IF NOT EXISTS clause");
42
    $this->assertTrue(self::$ci->blogger->install("admin_test_blog", "admins", "id", 7), "Create Blog with existent admin constarint");
43
    $fields = self::$ci->db->list_fields(Blogger::TABLE_PREFIX . "_admin_test_blog");
44
    $this->assertContains("id", $fields);
45
    $this->assertContains("title", $fields);
46
    $this->assertContains("content", $fields);
47
    $this->assertContains("slug", $fields);
48
    $this->assertContains("date_created", $fields);
49
    $this->assertContains("date_published", $fields);
50
  }
51
  /**
52
   * [testUI description]
53
   * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
54
   * @depends testInstallBlog
55
   */
56
  public function testUI() {
57
    $this->assertTrue(self::$ci->blogger->loadEditor("callback"), "Load Editor");
58
    self::$ci->blogger->setBlog("test_blog");
59
    $this->assertTrue(self::$ci->blogger->renderPostItems(null, null, null, 1, 0), "Test load empty posts set");
60
  }
61
  /**
62
   * [testBlogSave description]
63
   * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
64
   * @depends testInstallBlog
65
   */
66
  public function testBlogSave() {
67
    // No Admin.
68
    self::$ci->blogger->setBlog("test_blog");
69
    $_POST["action"] = "save";
70
    $_POST["title"] = "Hello Title";
71
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog.";
72
    $this->assertEquals(self::$ci->blogger->savePost(), Blogger::CREATE);
73
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog. Again.";
74
    $_POST["id"] = 1;
75
    $this->assertEquals(self::$ci->blogger->savePost(), Blogger::EDIT);
76
    $this->assertTrue(self::$ci->blogger->renderPost("Hello-Title", null));
77
    $post = self::$ci->blogger->getPost("Hello-Title", false);
78
    $this->assertTrue(is_array($post));
79
    $this->assertArrayHasKey("id", $post);
80
    $this->assertArrayHasKey("title", $post);
81
    $this->assertArrayHasKey("content", $post);
82
    $this->assertArrayHasKey("published", $post);
83
    $this->assertArrayHasKey("date_published", $post);
84
    $this->assertArrayHasKey("slug", $post);
85
    $this->assertEquals(1, $post["id"], "Assert Post ID");
86
    $this->assertEquals("Hello Title", $post["title"], "Assert Post Title");
87
    $this->assertEquals("The Quick Brown Fox Jumped over the Lazy Dog. Again.", $post["content"]);
88
    $this->assertEquals("Hello-Title", $post["slug"]);
89
    $this->assertEquals(0, $post["published"]);
90
    $this->assertEquals(null, $post["date_published"]);
91
    $_POST["action"] = "publish";
92
    $this->assertEquals(self::$ci->blogger->savePost(), Blogger::PUBLISH);
93
    $post = self::$ci->blogger->getPost("Hello-Title", false);
94
    $this->assertTrue(is_array($post));
95
    $this->assertEquals(1, $post["published"]);
96
    $this->assertNotEquals(null, $post["date_published"]);
97
    $_POST["action"] = "createAndPublish";
98
    $_POST["title"] = "Hello Title 2";
99
    $_POST["editor"] = "Create and Published Post.";
100
    unset($_POST["id"]);
101
    $this->assertEquals(Blogger::CREATE_AND_PUBLISH, self::$ci->blogger->savePost());
102
    $post = self::$ci->blogger->getPost("Hello-Title-2", false);
103
    $this->assertTrue(is_array($post));
104
    $this->assertArrayHasKey("id", $post);
105
    $this->assertArrayHasKey("title", $post);
106
    $this->assertArrayHasKey("content", $post);
107
    $this->assertArrayHasKey("published", $post);
108
    $this->assertArrayHasKey("date_published", $post);
109
    $this->assertArrayHasKey("slug", $post);
110
    $this->assertEquals(2, $post["id"], "Assert Post ID");
111
    $this->assertEquals("Hello Title 2", $post["title"], "Assert Post Title");
112
    $this->assertEquals("Create and Published Post.", $post["content"]);
113
    $this->assertEquals("Hello-Title-2", $post["slug"]);
114
    $this->assertEquals(1, $post["published"]);
115
    $this->assertNotEquals(null, $post["date_published"]);
116
    $this->assertEquals(Blogger::ABORT, self::$ci->blogger->savePost(), "No 2 blog posts can have the same title.");
117
  }
118
  /**
119
   * [testDynamicFunctions description]
120
   */
121
  public function testDynamicFunctions(): void {
122
    self::$ci->blogger->setBlog("rocket_blog");
123
    $this->assertEquals(Blogger::TABLE_PREFIX . "_rocket_blog", self::$ci->blogger->getName(), "Blogger setBlog works.");
124
  }
125
  /**
126
   * [tearDownAfterClass description]
127
   */
128
  public static function tearDownAfterClass(): void {
129
    self::$ci->db->empty_table("admins");
130
    self::$ci->db->empty_table("blogger_posts_test_blog");
131
    self::$ci->db->empty_table("blogger_posts_admin_test_blog");
132
    self::$ci->load->dbforge();
133
    self::$ci->dbforge->drop_table("admins");
134
    self::$ci->dbforge->drop_table("blogger_posts_test_blog");
135
    self::$ci->dbforge->drop_table("blogger_posts_admin_test_blog");
136
  }
137
}
138