From 0c59589d478b11fbc9ae71391e7754c00ae2427b Mon Sep 17 00:00:00 2001 From: Christoph Burschka Date: Mon, 30 Dec 2013 13:13:33 +0100 Subject: Rewrote Unit tests to explain successes and failures more clearly. --- tests/test | 11 ------- tests/test.php | 38 +++++++++------------- tests/test.successful | Bin 32 -> 0 bytes tests/unit_test.php | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 34 deletions(-) delete mode 100755 tests/test mode change 100644 => 100755 tests/test.php delete mode 100644 tests/test.successful create mode 100644 tests/unit_test.php diff --git a/tests/test b/tests/test deleted file mode 100755 index 01662c6..0000000 --- a/tests/test +++ /dev/null @@ -1,11 +0,0 @@ -p=`dirname $0` -php $p/test.php > $p/test.in -php $p/../main.php <$p/test.in>$p/test.out -if [ -z "$(diff $p/test.out $p/test.successful)" ] -then - echo "Test successful." - rm $p/test.in $p/test.out -else - echo "Test unsuccessful. Check activity logs." -fi - diff --git a/tests/test.php b/tests/test.php old mode 100644 new mode 100755 index ec6d35b..91b2dbb --- a/tests/test.php +++ b/tests/test.php @@ -1,26 +1,18 @@ +#!/usr/bin/env php +'); } + +$valid_account = [ + 'user' => $_SERVER['argv'][1], + 'password' => $_SERVER['argv'][2], +]; + +require_once __DIR__ . '/unit_test.php'; + +(new UnitTest($valid_account))->run(); diff --git a/tests/test.successful b/tests/test.successful deleted file mode 100644 index 128aea6..0000000 Binary files a/tests/test.successful and /dev/null differ diff --git a/tests/unit_test.php b/tests/unit_test.php new file mode 100644 index 0000000..86fff61 --- /dev/null +++ b/tests/unit_test.php @@ -0,0 +1,85 @@ + ['pipe', 'r'], // stdin is a pipe that the child will read from + 1 => ['pipe', 'w'], // stdout is a pipe that the child will write to + 2 => STDERR, // forward stderr directly + ]; + $cmd = __DIR__ . '/../main.php'; + $this->_process = proc_open($cmd, $spec, $this->pipes); + $this->_input = $this->pipes[0]; + $this->_output = $this->pipes[1]; + $this->valid = $valid_account; + $this->successes = 0; + $this->failures = 0; + $this->cases = 0; + } + + function run() { + foreach (get_class_methods($this) as $method) { + if (strpos($method, 'Test') === 0) { + $this->$method(); + } + } + printf("%d tests, %d passed, %d failed\n", $this->cases, $this->successes, $this->failures); + } + + function _send_request($request) { + $request = implode(':', $request); + fwrite($this->_input, pack('n', strlen($request))); + fwrite($this->_input, $request); + $result = unpack('n2x', fread($this->_output, 4)); + return [$result['x1'], $result['x2']]; + } + + function assert($request, $success, $comment) { + $result = $this->_send_request($request); + if ($result[0] == 2 and $result[1] == $success) { + $this->successes++; + printf("\033[0;32mPASS #%d: %s\033[0m\n", 1+$this->cases, $comment); + } + else { + $this->failures++; + printf("\033[0;31mFAIL #%d: %s\033[0m\n", 1+$this->cases, $comment); + } + $this->cases++; + } + + function TestUserGood() { + $this->assert(['isuser', $this->valid['user'], 'localhost'], TRUE, 'isuser with valid username'); + } + + function TestUserBad() { + $this->assert(['isuser', '123456789', 'localhost'], FALSE, 'isuser with bad username'); + } + + function TestAuthGood() { + $this->assert(['auth', $this->valid['user'], 'localhost', $this->valid['password']], TRUE, 'auth with valid password'); + } + + function TestAuthBadUser() { + $this->assert(['auth', '123456789', 'localhost', '123456789'], FALSE, 'auth with bad username'); + } + + function TestAuthBadPass() { + $this->assert(['auth', $this->valid['user'], 'localhost', '123456789'], FALSE, 'auth with bad password'); + } + + function TestSetPass() { + $this->assert(['setpass', '123456789', 'localhost', '123456789'], FALSE, 'attempt to set password (fail)'); + } + + function TestRegister() { + $this->assert(['tryregister', '123456789', 'localhost', '123456789'], FALSE, 'attempt to create account (fail)'); + } + + function TestRemove() { + $this->assert(['removeuser', '123456789', 'localhost', '123456789'], FALSE, 'attempt to delete account (fail)'); + } + + function TestRemove3() { + $this->assert(['removeuser3', '123456789', 'localhost', '123456789'], FALSE, 'attempt to login and delete account (fail)'); + } +} -- cgit v1.1