1<?php
2/*
3 * This file is part of PHPUnit.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11/**
12 * A Decorator that runs a test repeatedly.
13 */
14class PHPUnit_Extensions_RepeatedTest extends PHPUnit_Extensions_TestDecorator
15{
16    /**
17     * @var bool
18     */
19    protected $processIsolation = false;
20
21    /**
22     * @var int
23     */
24    protected $timesRepeat = 1;
25
26    /**
27     * @param PHPUnit_Framework_Test $test
28     * @param int                    $timesRepeat
29     * @param bool                   $processIsolation
30     *
31     * @throws PHPUnit_Framework_Exception
32     */
33    public function __construct(PHPUnit_Framework_Test $test, $timesRepeat = 1, $processIsolation = false)
34    {
35        parent::__construct($test);
36
37        if (is_int($timesRepeat) &&
38            $timesRepeat >= 0) {
39            $this->timesRepeat = $timesRepeat;
40        } else {
41            throw PHPUnit_Util_InvalidArgumentHelper::factory(
42                2,
43                'positive integer'
44            );
45        }
46
47        $this->processIsolation = $processIsolation;
48    }
49
50    /**
51     * Counts the number of test cases that
52     * will be run by this test.
53     *
54     * @return int
55     */
56    public function count()
57    {
58        return $this->timesRepeat * count($this->test);
59    }
60
61    /**
62     * Runs the decorated test and collects the
63     * result in a TestResult.
64     *
65     * @param PHPUnit_Framework_TestResult $result
66     *
67     * @return PHPUnit_Framework_TestResult
68     *
69     * @throws PHPUnit_Framework_Exception
70     */
71    public function run(PHPUnit_Framework_TestResult $result = null)
72    {
73        if ($result === null) {
74            $result = $this->createResult();
75        }
76
77        //@codingStandardsIgnoreStart
78        for ($i = 0; $i < $this->timesRepeat && !$result->shouldStop(); $i++) {
79            //@codingStandardsIgnoreEnd
80            if ($this->test instanceof PHPUnit_Framework_TestSuite) {
81                $this->test->setRunTestInSeparateProcess($this->processIsolation);
82            }
83            $this->test->run($result);
84        }
85
86        return $result;
87    }
88}
89