e282f8bc0c044ae83395611edcabf3df15f16d87
[yaffs-website] / vendor / phpunit / phpunit / src / Extensions / TicketListener.php
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  * Base class for test listeners that interact with an issue tracker.
13  *
14  * @since Class available since Release 3.4.0
15  */
16 abstract class PHPUnit_Extensions_TicketListener implements PHPUnit_Framework_TestListener
17 {
18     /**
19      * @var array
20      */
21     protected $ticketCounts = array();
22
23     /**
24      * @var bool
25      */
26     protected $ran = false;
27
28     /**
29      * An error occurred.
30      *
31      * @param PHPUnit_Framework_Test $test
32      * @param Exception              $e
33      * @param float                  $time
34      */
35     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
36     {
37     }
38
39     /**
40      * A failure occurred.
41      *
42      * @param PHPUnit_Framework_Test                 $test
43      * @param PHPUnit_Framework_AssertionFailedError $e
44      * @param float                                  $time
45      */
46     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
47     {
48     }
49
50     /**
51      * Incomplete test.
52      *
53      * @param PHPUnit_Framework_Test $test
54      * @param Exception              $e
55      * @param float                  $time
56      */
57     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
58     {
59     }
60
61     /**
62      * Risky test.
63      *
64      * @param PHPUnit_Framework_Test $test
65      * @param Exception              $e
66      * @param float                  $time
67      *
68      * @since  Method available since Release 4.0.0
69      */
70     public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
71     {
72     }
73
74     /**
75      * Skipped test.
76      *
77      * @param PHPUnit_Framework_Test $test
78      * @param Exception              $e
79      * @param float                  $time
80      *
81      * @since  Method available since Release 3.0.0
82      */
83     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
84     {
85     }
86
87     /**
88      * A test suite started.
89      *
90      * @param PHPUnit_Framework_TestSuite $suite
91      *
92      * @since  Method available since Release 2.2.0
93      */
94     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
95     {
96     }
97
98     /**
99      * A test suite ended.
100      *
101      * @param PHPUnit_Framework_TestSuite $suite
102      *
103      * @since  Method available since Release 2.2.0
104      */
105     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
106     {
107     }
108
109     /**
110      * A test started.
111      *
112      * @param PHPUnit_Framework_Test $test
113      */
114     public function startTest(PHPUnit_Framework_Test $test)
115     {
116         if (!$test instanceof PHPUnit_Framework_Warning) {
117             if ($this->ran) {
118                 return;
119             }
120
121             $name    = $test->getName(false);
122             $tickets = PHPUnit_Util_Test::getTickets(get_class($test), $name);
123
124             foreach ($tickets as $ticket) {
125                 $this->ticketCounts[$ticket][$name] = 1;
126             }
127
128             $this->ran = true;
129         }
130     }
131
132     /**
133      * A test ended.
134      *
135      * @param PHPUnit_Framework_Test $test
136      * @param float                  $time
137      */
138     public function endTest(PHPUnit_Framework_Test $test, $time)
139     {
140         if (!$test instanceof PHPUnit_Framework_Warning) {
141             if ($test->getStatus() == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
142                 $ifStatus   = array('assigned', 'new', 'reopened');
143                 $newStatus  = 'closed';
144                 $message    = 'Automatically closed by PHPUnit (test passed).';
145                 $resolution = 'fixed';
146                 $cumulative = true;
147             } elseif ($test->getStatus() == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
148                 $ifStatus   = array('closed');
149                 $newStatus  = 'reopened';
150                 $message    = 'Automatically reopened by PHPUnit (test failed).';
151                 $resolution = '';
152                 $cumulative = false;
153             } else {
154                 return;
155             }
156
157             $name    = $test->getName(false);
158             $tickets = PHPUnit_Util_Test::getTickets(get_class($test), $name);
159
160             foreach ($tickets as $ticket) {
161                 // Remove this test from the totals (if it passed).
162                 if ($test->getStatus() == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
163                     unset($this->ticketCounts[$ticket][$name]);
164                 }
165
166                 // Only close tickets if ALL referenced cases pass
167                 // but reopen tickets if a single test fails.
168                 if ($cumulative) {
169                     // Determine number of to-pass tests:
170                     if (count($this->ticketCounts[$ticket]) > 0) {
171                         // There exist remaining test cases with this reference.
172                         $adjustTicket = false;
173                     } else {
174                         // No remaining tickets, go ahead and adjust.
175                         $adjustTicket = true;
176                     }
177                 } else {
178                     $adjustTicket = true;
179                 }
180
181                 $ticketInfo = $this->getTicketInfo($ticket);
182
183                 if ($adjustTicket && in_array($ticketInfo['status'], $ifStatus)) {
184                     $this->updateTicket($ticket, $newStatus, $message, $resolution);
185                 }
186             }
187         }
188     }
189
190     /**
191      * @param mixed $ticketId
192      *
193      * @return mixed
194      */
195     abstract protected function getTicketInfo($ticketId = null);
196
197     /**
198      * @param string $ticketId
199      * @param string $newStatus
200      * @param string $message
201      * @param string $resolution
202      */
203     abstract protected function updateTicket($ticketId, $newStatus, $message, $resolution);
204 }