Skip to content

Commit 5ec05b1

Browse files
committed
Refactoring order parsing and seed evaluation
1 parent 6c29d4c commit 5ec05b1

File tree

2 files changed

+57
-71
lines changed

2 files changed

+57
-71
lines changed

src/PHPUnitRandomizer/Command.php

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,75 +17,51 @@ public function __construct()
1717
$this->longOptions['seed='] = 'seedHandler';
1818
$this->longOptions['order='] = 'orderHandler';
1919
$this->seed = rand(0, 9999);
20+
$this->order = 'defined';
2021
}
2122

2223
public static function main($exit = TRUE)
2324
{
2425
return parent::main($exit);
2526
}
2627

27-
protected function orderHandler($order_parameter)
28+
private function setSeedToPrinter($seed)
2829
{
29-
if (!is_string($order_parameter)) {
30-
$this->showError(
31-
sprintf('Could not use "%s" as order.', $order_parameter)
32-
);
33-
}
34-
35-
$order_parts = explode(':', $order_parameter, 2);
36-
if (count($order_parts) < 2) {
37-
$this->order = $order_parts[0];
38-
$this->arguments['order'] = $this->order;
39-
if ($this->order !== 'defined') {
40-
$this->arguments['seed'] = $this->seed;
41-
if (isset($this->arguments['printer']) && $this->arguments['printer'] instanceof ResultPrinter )
42-
{
43-
$this->arguments['printer']->setSeed($this->seed);
44-
}
45-
}
46-
47-
return;
48-
}
49-
50-
list($order, $seed) = $order_parts;
51-
if ($order !== 'defined' && is_int($seed)) {
52-
$this->showError(
53-
sprintf('Could not use "%s" as order.', $order_parameter)
54-
);
55-
}
56-
57-
$this->order = $order;
58-
$this->arguments['order'] = $this->order;
59-
$this->seed = $seed;
60-
$this->arguments['seed'] = $this->seed;
61-
6230
if (isset($this->arguments['printer']) && $this->arguments['printer'] instanceof ResultPrinter )
6331
{
64-
$this->arguments['printer']->setSeed($this->seed);
32+
$this->arguments['printer']->setSeed($seed);
6533
}
6634
}
6735

68-
protected function seedHandler($seed)
36+
/**
37+
* Parses arguments to know if random order is desired, and if seed was chosen.
38+
*
39+
* @param string $order String from command line parameter.
40+
* @return array
41+
*/
42+
private function getOrderAndSeed($order)
6943
{
70-
if (!is_numeric($seed)) {
71-
$this->showError(
72-
sprintf('Could not use "%s" as seed.', $seed)
73-
);
44+
@list($order, $seed) = explode(':', $order, 2);
45+
46+
if (empty($seed)) {
47+
$seed = $this->seed;
7448
}
7549

76-
if (isset($this->arguments['order'])) {
77-
$this->showError(
78-
sprintf('You can\'t use the \'order\' and \'seed\' arguments together')
79-
);
50+
if (!is_numeric($seed)) {
51+
$this->showError("Could not use '$seed' as seed.");
8052
}
8153

82-
$this->seed = intval($seed);
83-
$this->arguments['seed'] = $this->seed;
54+
return array($order, $seed);
55+
}
8456

85-
if (isset($this->arguments['printer']) && $this->arguments['printer'] instanceof ResultPrinter )
86-
{
87-
$this->arguments['printer']->setSeed($this->seed);
88-
}
57+
protected function orderHandler($order_parameter)
58+
{
59+
list($order, $seed) = $this->getOrderAndSeed($order_parameter);
60+
$this->order = $order;
61+
$this->arguments['order'] = $order;
62+
$this->seed = $seed;
63+
$this->arguments['seed'] = $seed;
64+
$this->setSeedToPrinter($this->seed);
8965
}
9066

9167
protected function createRunner()
@@ -99,7 +75,7 @@ public function showHelp()
9975

10076
print <<<EOT
10177
102-
--seed <seed> Seed the randomizer with a specific seed.
78+
--order <rand[:seed]> Randomize the order of the tests. Optionally you can pass a seed to run a specific order.
10379
10480
EOT;
10581
}

src/PHPUnitRandomizer/TestRunner.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,24 @@ public function __construct(PHPUnit_Runner_TestSuiteLoader $loader = NULL, PHP_C
1919
public function doRun(\PHPUnit_Framework_Test $suite, array $arguments = array())
2020
{
2121
$this->handleConfiguration($arguments);
22-
if
23-
(
24-
(isset($arguments['order']) && $arguments['order'] !== 'defined')
25-
|| (isset($arguments['seed']))
26-
)
22+
if (isset($arguments['order']))
2723
{
2824
$suite = $this->getRandomTestSuite($suite, $arguments);
2925
}
3026

3127
return parent::doRun($suite, $arguments);
3228
}
3329

30+
/**
31+
* Creates a TestSuite with random tests.
32+
*
33+
* @param TestSuite $suite The suite to randomize.
34+
* @param array $arguments Arguments to use.
35+
* @return TestSuite
36+
*/
3437
private function getRandomTestSuite($suite, $arguments)
3538
{
36-
if ($this->printer === NULL) {
37-
if (isset($arguments['printer']) &&
38-
$arguments['printer'] instanceof \PHPUnit_Util_Printer) {
39-
$this->printer = $arguments['printer'];
40-
} else {
41-
$this->printer = new ResultPrinter(
42-
NULL,
43-
$arguments['verbose'],
44-
$arguments['colors'],
45-
$arguments['debug']
46-
);
47-
$this->printer->setSeed($this->seed);
48-
}
49-
}
39+
$this->addPrinter($arguments);
5040

5141
$random_suite = new Decorator(
5242
$suite,
@@ -57,9 +47,29 @@ private function getRandomTestSuite($suite, $arguments)
5747
$arguments['processIsolation']
5848
);
5949

60-
$suite = new \PHPUnit_Framework_TestSuite();
50+
$suite = new \PHPUnit_Framework_TestSuite();
6151
$suite->addTest($random_suite, $arguments['groups']);
6252

6353
return $suite;
6454
}
55+
56+
/**
57+
* Sets printer to show the seed used to randomize the TestSuite.
58+
* @param array $arguments Arguments to use.
59+
*/
60+
private function addPrinter($arguments)
61+
{
62+
$this->printer = new ResultPrinter(
63+
NULL,
64+
$arguments['verbose'],
65+
$arguments['colors'],
66+
$arguments['debug']
67+
);
68+
$this->printer->setSeed($this->seed);
69+
70+
if (isset($arguments['printer']) &&
71+
$arguments['printer'] instanceof \PHPUnit_Util_Printer) {
72+
$this->printer = $arguments['printer'];
73+
}
74+
}
6575
}

0 commit comments

Comments
 (0)