From d884f5a3df280a83242c952cc81243e3bdfbcf33 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Thu, 2 Oct 2014 20:54:12 -0400 Subject: [PATCH] Finished up tests --- tests/DatabaseTest.php | 87 ++++++++++++++++++++++++++++++++++++++++++ tests/ProfilerTest.php | 3 ++ tests/ResourceTest.php | 43 +++++++++++++++++++++ tests/schema.sql | 2 + 4 files changed, 135 insertions(+) diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index c298078..dd49291 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -435,5 +435,92 @@ class DatabaseTest extends PHPUnit_Framework_TestCase Pickles\Database::getInstance(); } + + public function testProfilerWithoutParameters() + { + // Clears out the Config for ease of testing + Pickles\Object::$instances = []; + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['SERVER_NAME'] = '127.0.0.1'; + + file_put_contents('/tmp/pickles.php', ' [ + "local" => "127.0.0.1", + "production" => "123.456.789.0", + ], + "pickles" => [ + "datasource" => "mysql", + "profiler" => true, + ], + "datasources" => [ + "mysql" => [ + "type" => "mysql", + "driver" => "pdo_mysql", + "database" => "test", + "hostname" => "localhost", + "username" => "root", + "password" => "", + "database" => "test", + ], + ], + ]; + '); + + Pickles\Config::getInstance('/tmp/pickles.php'); + + $db = Pickles\Database::getInstance(); + $db->execute('SELECT * FROM `users`'); + + $report = Pickles\Profiler::report(); + $this->assertEquals(7, count($report)); + $this->assertEquals(2, count($report['logs'])); + $this->assertTrue(isset($report['logs'][1]['details']['explain'])); + } + + public function testProfilerWithParameters() + { + // Clears out the Config for ease of testing + Pickles\Object::$instances = []; + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['SERVER_NAME'] = '127.0.0.1'; + + file_put_contents('/tmp/pickles.php', ' [ + "local" => "127.0.0.1", + "production" => "123.456.789.0", + ], + "pickles" => [ + "datasource" => "mysql", + "profiler" => true, + ], + "datasources" => [ + "mysql" => [ + "type" => "mysql", + "driver" => "pdo_mysql", + "database" => "test", + "hostname" => "localhost", + "username" => "root", + "password" => "", + "database" => "test", + ], + ], + ]; + '); + + Pickles\Config::getInstance('/tmp/pickles.php'); + + $db = Pickles\Database::getInstance(); + $db->execute('SELECT * FROM `users` WHERE id = ?', [1000000]); + + $report = Pickles\Profiler::report(); + $this->assertEquals(7, count($report)); + $this->assertEquals(3, count($report['logs'])); + $this->assertEquals([1000000], $report['logs'][2]['details']['parameters']); + $this->assertTrue(isset($report['logs'][2]['details']['explain'])); + } } diff --git a/tests/ProfilerTest.php b/tests/ProfilerTest.php index 140d240..8df03d4 100644 --- a/tests/ProfilerTest.php +++ b/tests/ProfilerTest.php @@ -4,6 +4,9 @@ class ProfilerTest extends PHPUnit_Framework_TestCase { public function testProfiler() { + // Clears out any previous logging + Pickles\Profiler::report(); + $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['SERVER_NAME'] = '127.0.0.1'; diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index eeb7d24..78cbfed 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -204,6 +204,49 @@ namespace new Pickles\Router(); } + public function testBasicAuth() + { + Pickles\Object::$instances = []; + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['SERVER_NAME'] = '127.0.0.1'; + + file_put_contents('/tmp/pickles.php', ' [ + "local" => "127.0.0.1", + "production" => "123.456.789.0", + ], + "pickles" => [ + "namespace" => "", + "datasource" => "mysql", + "auth" => "basic", + ], + "datasources" => [ + "mysql" => [ + "driver" => "pdo_mysql", + ], + ], + ]; + '); + + Pickles\Config::getInstance('/tmp/pickles.php'); + + $response = json_encode([ + 'meta' => [ + 'status' => 405, + 'message' => 'Method not allowed.', + ], + ]); + + $this->expectOutputString($response); + + $_SERVER['REQUEST_METHOD'] = 'DELETE'; + $_REQUEST['request'] = 'v1/resource/1'; + + new Pickles\Router(); + } + public function testMethodNotAllowed() { $response = json_encode([ diff --git a/tests/schema.sql b/tests/schema.sql index 5d0a89a..52ee31b 100644 --- a/tests/schema.sql +++ b/tests/schema.sql @@ -53,3 +53,5 @@ CREATE TABLE `users` ( `is_deleted` tinyint(1) unsigned DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + +INSERT INTO users (id, username, created_at) VALUES (1000000, 'test', NOW());