Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT.php
1 <?php
2
3 namespace RedUNIT;
4 use RedBeanPHP\Facade as R;
5
6 /**
7  * RedUNIT
8  * Base class for RedUNIT, the micro unit test suite for RedBeanPHP
9  *
10  * @file               RedUNIT/RedUNIT.php
11  * @description        Provides the basic logic for any unit test in RedUNIT.
12  * @author             Gabor de Mooij
13  * @license            BSD
14  *
15  *
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.
19  */
20 abstract class RedUNIT
21 {
22         /**
23          * @var integer
24          */
25         protected $round;
26
27         /**
28          * @var string
29          */
30         protected $currentlyActiveDriverID = 'unknown';
31
32         /**
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).
37          *
38          * @return array
39          */
40         abstract public function getTargetDrivers();
41
42         /**
43          * Prepare test pack (mostly: nuke the entire database).
44          * This method prepares the test for a run.
45          */
46         public function prepare()
47         {
48                 R::freeze( FALSE );
49                 R::debug( FALSE );
50                 R::nuke();
51         }
52
53         /**
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.
59          *
60          * @return void
61          */
62         public function run()
63         {
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;
74                         $this->$call();
75                         try {
76                                 R::nuke();
77                         } catch( \PDOException $e ) {
78                                 // Some tests use a broken database on purpose, so an exception is ok
79                         }
80                 }
81         }
82
83         /**
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.
87          *
88          * @return void
89          */
90         public function cleanUp()
91         {
92         }
93
94         /**
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...
101          *
102          * @param string $driver the driver identifier
103          *
104          * @return void
105          */
106         public function setCurrentDriver( $driver )
107         {
108                 $this->currentlyActiveDriverID = $driver;
109         }
110
111         /**
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.
118          *
119          * @param integer $roundNumber round, the current round number
120          *
121          * @return void
122          */
123         public function setRound( $roundNumber )
124         {
125                 $this->round = (int) $roundNumber;
126         }
127
128         /**
129          * Returns the current round number.
130          * The current round number indicates how many times
131          * this test has been applied (to various drivers).
132          *
133          * @return integer
134          */
135         public function getRound()
136         {
137                 return $this->round;
138         }
139
140         /**
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.
146          *
147          * @return boolean
148          */
149         public function isFirstRound()
150         {
151                 return ( $this->round === 0 );
152         }
153 }