4 use RedBeanPHP\Facade as R;
8 * Base class for RedUNIT, the micro unit test suite for RedBeanPHP
10 * @file RedUNIT/RedUNIT.php
11 * @description Provides the basic logic for any unit test in RedUNIT.
12 * @author Gabor de Mooij
16 * (c) G.J.G.T. (Gabor) de Mooij
17 * This source file is subject to the BSD/GPLv2 License that is bundled
18 * with this source code in the file license.txt.
20 abstract class RedUNIT
30 protected $currentlyActiveDriverID = 'unknown';
33 * What drivers should be loaded for this test pack ?
34 * This method should be implemented by the test to tell the
35 * test controller system what drivers are supposed to be tested.
36 * Each driver will be fed to the test in a 'round' (explained below).
40 abstract public function getTargetDrivers();
43 * Prepare test pack (mostly: nuke the entire database).
44 * This method prepares the test for a run.
46 public function prepare()
54 * Runs the test. This method will run the tests implemented
55 * by the RedUNIT instance. The run method scans its class for
56 * all public instance methods except:
57 * run (to avoid recursion), getTargetDrivers, onEvent
58 * and prepare. -- added cleanUp/prepare just in case they get overridden.
64 $class = new \ReflectionClass( $this );
65 $skip = array( 'run', 'getTargetDrivers', 'onEvent', 'cleanUp', 'prepare' );
66 // Call all methods except run automatically
67 foreach ( $class->getMethods( \ReflectionMethod::IS_PUBLIC ) as $method ) {
68 // Skip methods inherited from parent class
69 if ( $method->class != $class->getName() ) continue;
70 if ( in_array( $method->name, $skip ) ) continue;
71 $classname = str_replace( $class->getParentClass()->getName().'_', '', $method->class );
72 printtext( "\n\t" . $classname."->".$method->name." [".$method->class."->".$method->name."]" . " \n\t" );
73 $call = $method->name;
77 } catch( \PDOException $e ) {
78 // Some tests use a broken database on purpose, so an exception is ok
84 * Clean-up method, to be invoked after running the test.
85 * This is an empty implementation, it does nothing. However this method
86 * should be overridden by tests that require clean-up.
90 public function cleanUp()
95 * Sets the current driver.
96 * This method is called by a test controller, runner or manager
97 * to activate the mode associated with the specified driver
98 * identifier. This mechanism allows a test to run slightly different
99 * in the context of another driver, for instance SQLite might not need
100 * some tests, or MySQL might need some additional tests etc...
102 * @param string $driver the driver identifier
106 public function setCurrentDriver( $driver )
108 $this->currentlyActiveDriverID = $driver;
112 * Sets the round number.
113 * Each test can have a varying number of flavors.
114 * A test flavor is 'that same test' but for a different driver.
115 * Each 'run on a specific driver' is called a round.
116 * Some tests might want to know what the current round is.
117 * This method can be used to set the current round number.
119 * @param integer $roundNumber round, the current round number
123 public function setRound( $roundNumber )
125 $this->round = (int) $roundNumber;
129 * Returns the current round number.
130 * The current round number indicates how many times
131 * this test has been applied (to various drivers).
135 public function getRound()
141 * Detects whether the current round is the first one.
142 * If the current round is indeed the first round, this method
143 * will return boolean TRUE, otherwise it will return FALSE. Note that
144 * the rounds are 0-based, so - if the current round equals 0, this
145 * method will return TRUE.
149 public function isFirstRound()
151 return ( $this->round === 0 );