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 for Tests.
13 *
14 * Use TestDecorator as the base class for defining new
15 * test decorators. Test decorator subclasses can be introduced
16 * to add behaviour before or after a test is run.
17 */
18class PHPUnit_Extensions_TestDecorator extends PHPUnit_Framework_Assert implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
19{
20    /**
21     * The Test to be decorated.
22     *
23     * @var object
24     */
25    protected $test = null;
26
27    /**
28     * Constructor.
29     *
30     * @param PHPUnit_Framework_Test $test
31     */
32    public function __construct(PHPUnit_Framework_Test $test)
33    {
34        $this->test = $test;
35    }
36
37    /**
38     * Returns a string representation of the test.
39     *
40     * @return string
41     */
42    public function toString()
43    {
44        return $this->test->toString();
45    }
46
47    /**
48     * Runs the test and collects the
49     * result in a TestResult.
50     *
51     * @param PHPUnit_Framework_TestResult $result
52     */
53    public function basicRun(PHPUnit_Framework_TestResult $result)
54    {
55        $this->test->run($result);
56    }
57
58    /**
59     * Counts the number of test cases that
60     * will be run by this test.
61     *
62     * @return int
63     */
64    public function count()
65    {
66        return count($this->test);
67    }
68
69    /**
70     * Creates a default TestResult object.
71     *
72     * @return PHPUnit_Framework_TestResult
73     */
74    protected function createResult()
75    {
76        return new PHPUnit_Framework_TestResult;
77    }
78
79    /**
80     * Returns the test to be run.
81     *
82     * @return PHPUnit_Framework_Test
83     */
84    public function getTest()
85    {
86        return $this->test;
87    }
88
89    /**
90     * Runs the decorated test and collects the
91     * result in a TestResult.
92     *
93     * @param PHPUnit_Framework_TestResult $result
94     *
95     * @return PHPUnit_Framework_TestResult
96     */
97    public function run(PHPUnit_Framework_TestResult $result = null)
98    {
99        if ($result === null) {
100            $result = $this->createResult();
101        }
102
103        $this->basicRun($result);
104
105        return $result;
106    }
107}
108