Ticket #437: NativePhpunitTask.php

File NativePhpunitTask.php, 6.3 KB (added by kontakt@…, 2 years ago)
Line 
1<?php
2/**
3 * Native PHPUnit Task
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to kontakt@beberlei.de so I can send you a copy immediately.
12 */
13
14require_once 'PHPUnit/Framework.php';
15
16/**
17 * A more flexible and powerful PHPUnit Task than the native Phing one.
18 *
19 * Plus forward compability for PHPUnit 3.5 and later is ensured by using the PHPUnit Test Runner instead of implementing one.
20 *
21 * @author Benjamin Eberlei <kontakt@beberlei.de>
22 */
23class NativePhpunitTask extends Task
24{
25    private $test;
26    private $testfile;
27    private $testdirectory;
28    private $configuration = null;
29    private $coverageClover = null;
30    private $junitlogfile = null;
31    private $haltonfailure = true;
32    private $haltonerror = true;
33
34    public function setTestdirectory($directory) {
35        $this->testdirectory = $directory;
36    }
37
38    public function setTest($test) {
39        $this->test = $test;
40    }
41
42    public function setTestfile($testfile) {
43        $this->testfile = $testfile;
44    }
45
46    public function setJunitlogfile($junitlogfile) {
47        $this->junitlogfile = $junitlogfile;
48    }
49
50    public function setConfiguration($configuration) {
51        $this->configuration = $configuration;
52    }
53
54    public function setCoverageClover($coverageClover) {
55        $this->coverageClover = $coverageClover;
56    }
57
58    public function setHaltonfailure($haltonfailures) {
59        $this->haltonfailure = $haltonfailures;
60    }
61
62    public function setHaltonerror($haltonerrors) {
63        $this->haltonerror = $haltonerrors;
64    }
65
66    public function init()
67    {
68        require_once "PHPUnit/Runner/Version.php";
69        $version = PHPUnit_Runner_Version::id();
70
71        if (version_compare($version, '3.4.0') < 0)
72        {
73            throw new BuildException("NativePHPUnitTask requires PHPUnit version >= 3.2.0", $this->getLocation());
74        }
75
76        require_once 'PHPUnit/Util/Filter.php';
77
78        // point PHPUnit_MAIN_METHOD define to non-existing method
79        if (!defined('PHPUnit_MAIN_METHOD'))
80        {
81            define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined');
82        }
83    }
84
85    public function main()
86    {
87        if (!is_dir(realpath($this->testdirectory))) {
88            throw new BuildException("NativePHPUnitTask requires a Test Directory path given, '".$this->testdirectory."' given.");
89        }
90        set_include_path(realpath($this->testdirectory) . PATH_SEPARATOR . get_include_path());
91
92        $printer = new NativePhpunitPrinter();
93
94        $arguments = array(
95            'configuration' => $this->configurationFile,
96            'coverageClover' => $this->coverageClover,
97            'junitLogfile' => $this->junitlogfile,
98            'printer' => $printer,
99        );
100
101        require_once "PHPUnit/TextUI/TestRunner.php";
102        $runner = new PHPUnit_TextUI_TestRunner();
103        $suite = $runner->getTest($this->test, $this->testfile, true);
104
105        try {
106            $result = $runner->doRun($suite, $arguments);
107            /* @var $result PHPUnit_Framework_TestResult */
108
109            if ( ($this->haltonfailure && $result->failureCount() > 0) || ($this->haltonerror && $result->errorCount() > 0) ) {
110                throw new BuildException("PHPUnit: ".$result->failureCount()." Failures and ".$result->errorCount()." Errors, ".
111                    "last failure message: ".$printer->getMessages());
112            }
113
114            $this->log("PHPUnit Success: ".count($result->passed())." tests passed, no ".
115                "failures (".$result->skippedCount()." skipped, ".$result->notImplementedCount()." not implemented)");
116
117        } catch(\Exception $e) {
118            throw new BuildException("NativePhpunitTask failed: ".$e->getMessage());
119        }
120    }
121}
122
123class NativePhpunitPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
124{
125    private $_messages = array();
126
127    public function write($buffer)
128    {
129        // do nothing
130    }
131
132    public function getMessages()
133    {
134        return $this->_messages;
135    }
136
137    /**
138     * An error occurred.
139     *
140     * @param  PHPUnit_Framework_Test $test
141     * @param  Exception              $e
142     * @param  float                  $time
143     */
144    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
145    {
146        $this->_messages[] = "Test ERROR: ".$test->getName().": ".$e->getMessage();
147    }
148
149    /**
150     * A failure occurred.
151     *
152     * @param  PHPUnit_Framework_Test                 $test
153     * @param  PHPUnit_Framework_AssertionFailedError $e
154     * @param  float                                  $time
155     */
156    public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
157    {
158        $this->_messages[] = "Test FAILED: ".$test->getName().": ".$e->getMessage();
159    }
160
161    /**
162     * Incomplete test.
163     *
164     * @param  PHPUnit_Framework_Test $test
165     * @param  Exception              $e
166     * @param  float                  $time
167     */
168    public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
169    {
170
171    }
172
173    /**
174     * Skipped test.
175     *
176     * @param  PHPUnit_Framework_Test $test
177     * @param  Exception              $e
178     * @param  float                  $time
179     * @since  Method available since Release 3.0.0
180     */
181    public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
182    {
183
184    }
185
186    /**
187     * A test suite started.
188     *
189     * @param  PHPUnit_Framework_TestSuite $suite
190     * @since  Method available since Release 2.2.0
191     */
192    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
193    {
194
195    }
196
197    /**
198     * A test suite ended.
199     *
200     * @param  PHPUnit_Framework_TestSuite $suite
201     * @since  Method available since Release 2.2.0
202     */
203    public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
204    {
205
206    }
207
208    /**
209     * A test started.
210     *
211     * @param  PHPUnit_Framework_Test $test
212     */
213    public function startTest(PHPUnit_Framework_Test $test)
214    {
215
216    }
217
218    /**
219     * A test ended.
220     *
221     * @param  PHPUnit_Framework_Test $test
222     * @param  float                  $time
223     */
224    public function endTest(PHPUnit_Framework_Test $test, $time)
225    {
226       
227    }
228}