Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 145 lines (133 sloc) 3.776 kB
e7f3c31 @gwoo going lithium
gwoo authored
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
4f1a9c0 @nateabele Updating copyright year.
nateabele authored
5 * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
e7f3c31 @gwoo going lithium
gwoo authored
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
8
9 namespace lithium\test;
10
c88bb6c @nateabele Adding error handling for test case loading.
nateabele authored
11 use Exception;
12 use lithium\test\Unit;
13 use lithium\core\Libraries;
14 use lithium\util\Collection;
e7f3c31 @gwoo going lithium
gwoo authored
15
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
16 /**
e2c871c @jperras Add class and method doc blocks to `test\Group`.
jperras authored
17 * A `Collection` of tests that represents a test group.
18 *
19 * Tests are added to this group either on `construct` by passing a fully-namespaced test class
20 * or namespace string-based path, e.g.
21 *
22 * {{{
23 * $group = new Group(array('data' => array(
24 * 'data\ModelTest',
25 * new \lithium\tests\cases\core\ObjectTest()
26 * )));
27 * }}}
28 *
29 * Or they can be added programmatically:
30 *
31 * {{{
32 * $group->add('data\ModelTest');
33 * }}}
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
34 */
e7f3c31 @gwoo going lithium
gwoo authored
35 class Group extends \lithium\util\Collection {
36
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
37 /**
38 * auto init for setting up items passed into constructor
39 *
40 * @return void
41 */
e7f3c31 @gwoo going lithium
gwoo authored
42 protected function _init() {
43 parent::_init();
244646c @nateabele Adding test for `\data\Source`. Refactoring classes in `\data` into "…
nateabele authored
44 $data = $this->_data;
45 $this->_data = array();
46
47 foreach ($data as $item) {
e7f3c31 @gwoo going lithium
gwoo authored
48 $this->add($item);
49 }
50 }
51
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
52 /**
8bfcfe4 @gwoo updating test suite handling. Re-Adding `\test\Controller` for browse…
gwoo authored
53 * Get all test cases. By default, does not include function or integration tests.
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
54 *
97c5e7c @Howard3 Doing a general QA and Docblock
Howard3 authored
55 * @param array $options
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
56 * @return array
57 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
58 public static function all(array $options = array()) {
8bfcfe4 @gwoo updating test suite handling. Re-Adding `\test\Controller` for browse…
gwoo authored
59 $defaults = array(
60 'filter' => '/cases/',
61 'exclude' => '/mock/',
26c2192 @davidpersson QA: Removing trailing comma in arrays, lowercasing some keywords.
davidpersson authored
62 'recursive' => true
8bfcfe4 @gwoo updating test suite handling. Re-Adding `\test\Controller` for browse…
gwoo authored
63 );
7f43930 @davidpersson Simplifying `Group::all()`.
davidpersson authored
64 return Libraries::locate('tests', null, $options + $defaults);
e7f3c31 @gwoo going lithium
gwoo authored
65 }
66
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
67 /**
e2c871c @jperras Add class and method doc blocks to `test\Group`.
jperras authored
68 * Add a tests to the group.
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
69 *
e2c871c @jperras Add class and method doc blocks to `test\Group`.
jperras authored
70 * @param string $test The test to be added.
97c5e7c @Howard3 Doing a general QA and Docblock
Howard3 authored
71 * @param array $options Method options. Currently not used in this method.
e2c871c @jperras Add class and method doc blocks to `test\Group`.
jperras authored
72 * @return array Updated list of tests contained within this collection.
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
73 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
74 public function add($test = null, array $options = array()) {
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
75 $resolve = function($self, $test) {
76 switch (true) {
77 case !$test:
78 return array();
c88bb6c @nateabele Adding error handling for test case loading.
nateabele authored
79 case is_object($test) && $test instanceof Unit:
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
80 return array(get_class($test));
81 case is_string($test) && !file_exists(Libraries::path($test)):
2228161 @gwoo fixes #252 : cannot run tests in app as a group
gwoo authored
82 return $self->invokeMethod('_resolve', array($test));
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
83 default:
84 return (array) $test;
e7f3c31 @gwoo going lithium
gwoo authored
85 }
86 };
87 if (is_array($test)) {
88 foreach ($test as $t) {
244646c @nateabele Adding test for `\data\Source`. Refactoring classes in `\data` into "…
nateabele authored
89 $this->_data = array_filter(array_merge($this->_data, $resolve($this, $t)));
e7f3c31 @gwoo going lithium
gwoo authored
90 }
244646c @nateabele Adding test for `\data\Source`. Refactoring classes in `\data` into "…
nateabele authored
91 return $this->_data;
e7f3c31 @gwoo going lithium
gwoo authored
92 }
244646c @nateabele Adding test for `\data\Source`. Refactoring classes in `\data` into "…
nateabele authored
93 return $this->_data = array_merge($this->_data, $resolve($this, $test));
e7f3c31 @gwoo going lithium
gwoo authored
94 }
95
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
96 /**
97 * Get the collection of tests
98 *
97c5e7c @Howard3 Doing a general QA and Docblock
Howard3 authored
99 * @param string|array $params
100 * @param array $options
c34904e @gwoo updating test menu some more. refactoring \test\Group
gwoo authored
101 * @return lithium\util\Collection
102 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
103 public function tests($params = array(), array $options = array()) {
e7f3c31 @gwoo going lithium
gwoo authored
104 $tests = new Collection();
c88bb6c @nateabele Adding error handling for test case loading.
nateabele authored
105
106 foreach ($this->_data as $test) {
107 if (!class_exists($test)) {
583059b @davidpersson Adding and replacing ticks/quotes with backticks in error and excepti…
davidpersson authored
108 throw new Exception("Test case `{$test}` not found.");
c88bb6c @nateabele Adding error handling for test case loading.
nateabele authored
109 }
110 $tests[] = new $test;
111 }
e7f3c31 @gwoo going lithium
gwoo authored
112 return $tests;
113 }
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
114
115 /**
2228161 @gwoo fixes #252 : cannot run tests in app as a group
gwoo authored
116 * Resolves a unit test class (or classes) from a class or namespace path string.
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
117 *
118 * @param string $test The path string in which to find the test case(s). This may be a
fd09a84 @gwoo requiring namespaced path to resolve test cases in Group. no longer r…
gwoo authored
119 * library, a namespace, or a fully-namespaced class reference.
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
120 * @return array Returns an array containing one or more fully-namespaced class references to
2228161 @gwoo fixes #252 : cannot run tests in app as a group
gwoo authored
121 * unit tests.
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
122 */
2228161 @gwoo fixes #252 : cannot run tests in app as a group
gwoo authored
123 protected function _resolve($test) {
124 if (strpos($test, '\\') === false && Libraries::get($test)) {
125 return (array) Libraries::find($test, array(
26c2192 @davidpersson QA: Removing trailing comma in arrays, lowercasing some keywords.
davidpersson authored
126 'recursive' => true, 'filter' => '/cases|integration|functional/'
2228161 @gwoo fixes #252 : cannot run tests in app as a group
gwoo authored
127 ));
128 }
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
129 if (preg_match("/Test/", $test)) {
130 return array($test);
131 }
8b48d05 @nateabele Refactoring test lookup fix for `\test\Group::_unitClass()`, referenc…
nateabele authored
132 if (!$test = trim($test, '\\')) {
dfe612a @nateabele Adding fix for adding empty tests in `\test\Group`.
nateabele authored
133 return array();
134 }
8b48d05 @nateabele Refactoring test lookup fix for `\test\Group::_unitClass()`, referenc…
nateabele authored
135 list($library, $path) = explode('\\', $test, 2) + array($test, null);
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
136
137 return (array) Libraries::find($library, array(
138 'recursive' => true,
139 'path' => '/' . str_replace('\\', '/', $path),
26c2192 @davidpersson QA: Removing trailing comma in arrays, lowercasing some keywords.
davidpersson authored
140 'filter' => '/cases|integration|functional/'
08481b3 @nateabele Refactoring `\test\Group::add()`, fixing support for running plugin a…
nateabele authored
141 ));
142 }
e7f3c31 @gwoo going lithium
gwoo authored
143 }
144
145 ?>
Something went wrong with that request. Please try again.