Skip to content

Commit 13c56fb

Browse files
committed
update
1 parent 8210c18 commit 13c56fb

8 files changed

Lines changed: 615 additions & 0 deletions

File tree

tests/AuthTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace MintyPHP\Tests;
4+
5+
use MintyPHP\Auth;
6+
7+
class AuthTest extends \PHPUnit\Framework\TestCase
8+
{
9+
public static DBTest $db;
10+
11+
public static function setUpBeforeClass(): void
12+
{
13+
DBTest::setUpBeforeClass();
14+
self::$db = new DBTest("AuthTest");
15+
self::$db->testDropUsersBefore();
16+
self::$db->testCreateUsers();
17+
set_error_handler(static function (int $errno, string $errstr): never {
18+
throw new \Exception($errstr, $errno);
19+
}, E_ALL);
20+
}
21+
22+
public function testRegister(): void
23+
{
24+
$registered = Auth::register('test', 'test');
25+
$this->assertNotFalse($registered, 'user not registered');
26+
}
27+
28+
public function testLogin(): void
29+
{
30+
try {
31+
Auth::login('test', 'test');
32+
$session_regenerated = false;
33+
} catch (\Exception $e) {
34+
$session_regenerated = explode(':', $e->getMessage())[0] == "session_regenerate_id()";
35+
}
36+
$this->assertTrue($session_regenerated, 'session not regenerated');
37+
}
38+
39+
public function testLogout(): void
40+
{
41+
$_SESSION['user'] = array('id' => 1, 'username' => 'test');
42+
try {
43+
Auth::logout();
44+
$session_regenerated = false;
45+
} catch (\Exception $e) {
46+
$session_regenerated = explode(':', $e->getMessage())[0] == "session_regenerate_id()";
47+
}
48+
$this->assertTrue($session_regenerated, 'session not regenerated');
49+
$this->assertFalse(isset($_SESSION['user']), 'user not unset');
50+
}
51+
52+
public static function tearDownAfterClass(): void
53+
{
54+
restore_error_handler();
55+
self::$db->testDropUsers();
56+
}
57+
}

tests/DBTest.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
namespace MintyPHP\Tests;
4+
5+
use MintyPHP\DB;
6+
7+
class DBTest extends \PHPUnit\Framework\TestCase
8+
{
9+
public static function setUpBeforeClass(): void
10+
{
11+
DB::$username = 'mintyphp_test';
12+
DB::$password = 'mintyphp_test';
13+
DB::$database = 'mintyphp_test';
14+
}
15+
16+
public function testDropPostsBefore(): void
17+
{
18+
$result = DB::query('DROP TABLE IF EXISTS `posts`;');
19+
$this->assertNotFalse($result, 'drop posts failed');
20+
}
21+
22+
public function testDropUsersBefore(): void
23+
{
24+
$result = DB::query('DROP TABLE IF EXISTS `users`;');
25+
$this->assertNotFalse($result, 'drop users failed');
26+
}
27+
28+
public function testCreateUsers(): void
29+
{
30+
$result = DB::query('CREATE TABLE `users` (
31+
`id` int(11) NOT NULL AUTO_INCREMENT,
32+
`username` varchar(255) COLLATE utf8_bin NOT NULL,
33+
`password` varchar(255) COLLATE utf8_bin NOT NULL,
34+
`created` datetime NOT NULL,
35+
PRIMARY KEY (`id`),
36+
UNIQUE KEY `username` (`username`)
37+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;');
38+
$this->assertNotFalse($result, 'create users failed');
39+
}
40+
41+
public function testCreatePosts(): void
42+
{
43+
$result = DB::query('CREATE TABLE `posts` (
44+
`id` int(11) NOT NULL AUTO_INCREMENT,
45+
`slug` varchar(255) COLLATE utf8_bin NOT NULL,
46+
`tags` varchar(255) COLLATE utf8_bin NOT NULL,
47+
`title` text COLLATE utf8_bin NOT NULL,
48+
`content` mediumtext COLLATE utf8_bin NOT NULL,
49+
`created` datetime NOT NULL,
50+
`published` datetime DEFAULT NULL,
51+
`user_id` int(11) NOT NULL,
52+
PRIMARY KEY (`id`),
53+
UNIQUE KEY `slug` (`slug`),
54+
KEY `user_id` (`user_id`),
55+
CONSTRAINT `posts_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
56+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;');
57+
$this->assertNotFalse($result, 'create posts failed');
58+
}
59+
60+
public function testInsertUsers(): void
61+
{
62+
$result = DB::insert("INSERT INTO `users` (`id`, `username`, `password`, `created`) VALUES (NULL, 'test1', 'c32ac6310706acdadea74c901c3f08fe06c44c61', '2014-05-28 22:58:22');");
63+
$this->assertNotFalse($result, 'insert user failed 1');
64+
$this->assertEquals(1, $result);
65+
$result = DB::insert("INSERT INTO `users` (`id`, `username`, `password`, `created`) VALUES (NULL, 'test2', 'c32ac6310706acdadea74c901c3f08fe06c44c61', '2014-05-28 22:58:22');");
66+
$this->assertNotFalse($result, 'insert user failed 2');
67+
$this->assertEquals(2, $result);
68+
}
69+
70+
public function testInsertPosts(): void
71+
{
72+
$result = DB::insert("INSERT INTO `posts` (`id`, `slug`, `tags`, `title`, `content`, `created`, `published`, `user_id`) VALUES (NULL, '2014-08-test1', '', 'test', 'test', '2014-05-28 22:58:22', NULL, 1);");
73+
$this->assertNotFalse($result, 'insert post failed 1');
74+
$this->assertEquals(1, $result);
75+
$result = DB::insert("INSERT INTO `posts` (`id`, `slug`, `tags`, `title`, `content`, `created`, `published`, `user_id`) VALUES (NULL, '2014-08-test2', '', 'test', 'test', '2014-05-28 22:58:22', NULL, 1);");
76+
$this->assertNotFalse($result, 'insert post failed 2');
77+
$this->assertEquals(2, $result);
78+
}
79+
80+
public function testUpdatePosts(): void
81+
{
82+
$result = DB::update("UPDATE `posts` SET `created`='2014-05-28 22:58:20' WHERE `id`=? OR `id` IN (???) OR `id`=? OR `id` IN (???) OR `id`=? OR `id` IN (???);", 1, [1, 2], 1, [1], 1, []);
83+
$this->assertNotFalse($result, 'update posts failed');
84+
$this->assertEquals(2, $result);
85+
}
86+
87+
public function testSelectPosts(): void
88+
{
89+
$result = DB::select("SELECT * FROM `posts`;");
90+
$this->assertEquals(2, count($result));
91+
$this->assertEquals('posts', array_keys($result[0])[0]);
92+
$this->assertEquals('id', array_keys($result[0]['posts'])[0]);
93+
$result = DB::select("SELECT * FROM `posts`, `users` WHERE posts.user_id = users.id and users.username = 'test1';");
94+
$this->assertEquals(2, count($result));
95+
$this->assertEquals(array('posts', 'users'), array_keys($result[0]));
96+
$this->assertEquals('id', array_keys($result[0]['posts'])[0]);
97+
$this->assertEquals('test1', $result[0]['users']['username']);
98+
$this->expectException('MintyPHP\DBError');
99+
$result = DB::select("some bogus query;");
100+
}
101+
102+
public function testSelectOne(): void
103+
{
104+
$result = DB::selectOne("SELECT * FROM `posts` limit 1;");
105+
$this->assertEquals('posts', array_keys($result)[0]);
106+
$this->assertEquals('id', array_keys($result['posts'])[0]);
107+
$result = DB::selectOne("SELECT * FROM `posts` WHERE slug like 'm%' limit 1;");
108+
$this->assertEquals([], $result);
109+
$this->expectException('MintyPHP\DBError');
110+
$result = DB::selectOne("some bogus query;");
111+
}
112+
113+
public function testSelectValues(): void
114+
{
115+
$result = DB::selectValues("SELECT username FROM `users`;");
116+
$this->assertEquals(array('test1', 'test2'), $result);
117+
$result = DB::selectValues("SELECT username FROM `users` WHERE username like 'm%' limit 1;");
118+
$this->assertEquals([], $result);
119+
$this->expectException('MintyPHP\DBError');
120+
$result = DB::selectValues("some bogus query;");
121+
}
122+
123+
public function testSelectValue(): void
124+
{
125+
$result = DB::selectValue("SELECT username FROM `users` limit 1;");
126+
$this->assertEquals('test1', $result);
127+
$result = DB::selectValue("SELECT username FROM `users` WHERE username like 'm%' limit 1;");
128+
$this->assertEquals(false, $result);
129+
$this->expectException('MintyPHP\DBError');
130+
$result = DB::selectValue("some bogus query;");
131+
}
132+
133+
public function testQuery(): void
134+
{
135+
$result = DB::query("SELECT * FROM `posts` limit 1;");
136+
$this->assertNotEquals(false, $result);
137+
$this->expectException('MintyPHP\DBError');
138+
$result = DB::query("some bogus query;");
139+
}
140+
141+
public function testDeletePosts(): void
142+
{
143+
$result = DB::delete('DELETE FROM `posts`;');
144+
$this->assertNotFalse($result, 'delete posts failed');
145+
$this->assertEquals(2, $result);
146+
}
147+
148+
public function testDeleteUsers(): void
149+
{
150+
$result = DB::delete('DELETE FROM `users`;');
151+
$this->assertNotFalse($result, 'delete users failed');
152+
$this->assertEquals(2, $result);
153+
}
154+
155+
public function testDropPosts(): void
156+
{
157+
$result = DB::query('DROP TABLE `posts`;');
158+
$this->assertNotFalse($result, 'drop posts failed');
159+
}
160+
161+
public function testDropUsers(): void
162+
{
163+
$result = DB::query('DROP TABLE `users`;');
164+
$this->assertNotFalse($result, 'drop users failed');
165+
}
166+
}

0 commit comments

Comments
 (0)