Permalink
Browse files

initial work on `fu` test runner script

  • Loading branch information...
1 parent 1f8ccdf commit d65256e71bd5ddfcf49126ea028cbd3858c018de @funkatron committed Mar 6, 2014
Showing with 303 additions and 0 deletions.
  1. +109 −0 bin/fu
  2. +118 −0 composer.lock
  3. +35 −0 examples/example_tests_1.php
  4. +41 −0 examples/example_tests_2.php
View
109 bin/fu
@@ -0,0 +1,109 @@
+#!/usr/bin/env php
+<?php
+use \Colors\Color;
+use \Commando\Command;
+use \FUnit as fu;
+
+/**
+ * try to load up classes
+ */
+$req_paths = array(
+ __DIR__ . '/../vendor/autoload.php',
+ __DIR__ . '/../../../autoload.php',
+ __DIR__ . '/../autoload.php',
+);
+
+foreach ($req_paths as $req_path) {
+ if (is_file($req_path)) {
+ require_once($req_path);
+ break;
+ }
+}
+
+if (!class_exists('FUnit')) {
+ echo 'Could not load class FUnit' . PHP_EOL;
+ exit(1);
+}
+
+/**
+ * build CLI options with commando
+ */
+$tokens = array('fu');
+$cmd = new Command();
+$cmd
+ ->setHelp('fu is the FUnit test runner. Tell it to execute a single file, or pass it no args and it will scan file files with (tests?) in them in the current directory')
+ ->argument()->referToAs('file to run')->describeAs('tell fu to execute a single file. OPTIONAL')
+ ->option('f')
+ ->aka('format')
+ ->referToAs('report format')
+ ->must(function ($format) {
+ $formats = array('text', 'xunit');
+ return in_array($format, $formats);
+ })
+ ->describeAs('the report format. can be `text` or `xunit`. Default is `text`')
+ ->flag('d')->boolean()->description("Enable debugging");
+
+/**
+ * start looking for test files
+ */
+function getTestFiles($testfile = null)
+{
+ $clr = new Color();
+ $testfiles = array();
+ if (!empty($testfile)) {
+
+ if (!file_exists($testfile)) {
+ echo $clr("{$testfile} does not exist; exiting")->red . PHP_EOL;
+ exit(1);
+ }
+
+ $testfile = realpath($testfile);
+ if (is_dir($testfile)) {
+ $testfiles = scanDirectoryForTestFiles($testfile);
+ } else if (!is_file($testfile)) {
+ echo $clr("{$testfile} is not a file; exiting")->red . PHP_EOL;
+ exit(1);
+ } else {
+ $testfiles[] = $testfile;
+ }
+ }
+ return $testfiles;
+}
+
+function scanDirectoryForTestFiles($dirpath) {
+ $di = new \DirectoryIterator($dirpath);
+ $testfiles = array();
+ foreach ($di as $fileInfo) {
+ if (preg_match("#(tests?)#", $fileInfo->getBasename())) {
+ $testfiles[] = $fileInfo->getPathname();
+ }
+ }
+ return $testfiles;
+}
+
+/**
+ * execute the tests within the test files
+ * @param array $testfiles array of paths to test files
+ */
+function executeTests($testfiles = array())
+{
+ $clr = new Color();
+ if (count($testfiles) < 1) {
+ echo $clr("No test files found/specified")->red . PHP_EOL;
+ exit(1);
+ }
+ fu::set_disable_reporting(true);
+ foreach ($testfiles as $testfile) {
+ require($testfile);
+ }
+ fu::set_disable_reporting(false);
+}
+
+$args = $cmd->getArgumentValues();
+$flags = $cmd->getFlags();
+$testfiles = getTestFiles($args[0]);
+executeTests($testfiles);
+
+fu::report();
+
+exit(fu::exit_code());
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,35 @@
+<?php
+use \FUnit as fu;
+
+fu::setup(function() {
+ // set a fixture to use in tests
+ fu::fixture('foobar', array('foo'=>'bar'));
+});
+
+fu::teardown(function() {
+ // this resets the fu::$fixtures array. May not provide clean shutdown
+ fu::reset_fixtures();
+});
+
+fu::test("this is a test", function() {
+ fu::ok(1, "the integer '1' is okay");
+ fu::ok(0, "the integer '0' is not okay"); // this will fail!
+});
+
+fu::test("another test", function() {
+ fu::equal(true, 1, "the integer '1' is truthy");
+ fu::not_strict_equal(true, 1, "the integer '1' is NOT true");
+ // access a fixture
+ $foobar = fu::fixture('foobar');
+ fu::equal($foobar['foo'], 'bar', "the fixture 'foobar' should have a key 'foo' equal to 'baz'");
+
+ $fooarr = array('blam'=>'blaz');
+ fu::has('blam', $fooarr, "\$fooarr has a key named 'blam'");
+
+
+ $fooobj = new \StdClass;
+ $fooobj->blam = 'blaz';
+ fu::has('blam', $fooobj, "\$fooobj has a property named 'blam'");
+});
+
+fu::run();
@@ -0,0 +1,41 @@
+<?php
+use \FUnit as fu;
+
+fu::setup(function() {
+ // set a fixture to use in tests
+ fu::fixture('foobar', array('foo'=>'bar'));
+});
+
+fu::teardown(function() {
+ // this resets the fu::$fixtures array. May not provide clean shutdown
+ fu::reset_fixtures();
+});
+
+fu::test('Checking for exceptions', function() {
+ $callback = function() { throw new RuntimeException(); };
+ fu::throws($callback, 'RuntimeException', 'Correct exception');
+
+ $callback = function($foo) { throw new RuntimeException($foo); };
+ fu::throws($callback, array('bar'), 'LogicException', 'Not the correct exception');
+});
+
+fu::test('Forced failure', function() {
+ fu::fail('This is a forced fail');
+});
+
+
+fu::test('Expected failure', function() {
+ fu::expect_fail('This is a good place to describe a missing test');
+});
+
+
+fu::test('Forced Errors/Exception', function() {
+
+ trigger_error('This was triggered inside a test', E_USER_ERROR);
+
+ trigger_error('This was triggered inside a test', E_USER_NOTICE);
+
+ throw new Exception('This was thrown inside a test');
+});
+
+fu::run();

0 comments on commit d65256e

Please sign in to comment.