]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Extensions/TicketListener.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Extensions / TicketListener.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @package    PHPUnit
38  * @subpackage Extensions_TicketListener
39  * @author     Sean Coates <sean@caedmon.net>
40  * @author     Raphael Stolt <raphael.stolt@gmail.com>
41  * @author     Sebastian Bergmann <sebastian@phpunit.de>
42  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
43  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
44  * @link       http://www.phpunit.de/
45  * @since      File available since Release 3.4.0
46  */
47
48 /**
49  * Base class for test listeners that interact with an issue tracker.
50  *
51  * @package    PHPUnit
52  * @subpackage Extensions_TicketListener
53  * @author     Sean Coates <sean@caedmon.net>
54  * @author     Sebastian Bergmann <sebastian@phpunit.de>
55  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
56  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
57  * @version    Release: 3.5.13
58  * @link       http://www.phpunit.de/
59  * @since      Class available since Release 3.4.0
60  */
61 abstract class PHPUnit_Extensions_TicketListener implements PHPUnit_Framework_TestListener
62 {
63     /**
64      * @var array
65      */
66     protected $ticketCounts = array();
67
68     /**
69      * @var boolean
70      */
71     protected $ran = FALSE;
72
73     /**
74      * An error occurred.
75      *
76      * @param  PHPUnit_Framework_Test $test
77      * @param  Exception              $e
78      * @param  float                  $time
79      */
80     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
81     {
82     }
83
84     /**
85      * A failure occurred.
86      *
87      * @param  PHPUnit_Framework_Test                 $test
88      * @param  PHPUnit_Framework_AssertionFailedError $e
89      * @param  float                                  $time
90      */
91     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
92     {
93     }
94
95     /**
96      * Incomplete test.
97      *
98      * @param  PHPUnit_Framework_Test $test
99      * @param  Exception              $e
100      * @param  float                  $time
101      */
102     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
103     {
104     }
105
106     /**
107      * Skipped test.
108      *
109      * @param  PHPUnit_Framework_Test $test
110      * @param  Exception              $e
111      * @param  float                  $time
112      * @since  Method available since Release 3.0.0
113      */
114     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
115     {
116     }
117
118     /**
119      * A test suite started.
120      *
121      * @param  PHPUnit_Framework_TestSuite $suite
122      * @since  Method available since Release 2.2.0
123      */
124     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
125     {
126     }
127
128     /**
129      * A test suite ended.
130      *
131      * @param  PHPUnit_Framework_TestSuite $suite
132      * @since  Method available since Release 2.2.0
133      */
134     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
135     {
136     }
137
138     /**
139      * A test started.
140      *
141      * @param  PHPUnit_Framework_Test $test
142      */
143     public function startTest(PHPUnit_Framework_Test $test)
144     {
145         if (!$test instanceof PHPUnit_Framework_Warning) {
146             if ($this->ran) {
147                 return;
148             }
149
150             $name    = $test->getName(FALSE);
151             $tickets = PHPUnit_Util_Test::getTickets(get_class($test), $name);
152
153             foreach ($tickets as $ticket) {
154                 $this->ticketCounts[$ticket][$name] = 1;
155             }
156
157             $this->ran = TRUE;
158         }
159     }
160
161     /**
162      * A test ended.
163      *
164      * @param  PHPUnit_Framework_Test $test
165      * @param  float                  $time
166      */
167     public function endTest(PHPUnit_Framework_Test $test, $time)
168     {
169         if (!$test instanceof PHPUnit_Framework_Warning) {
170             if ($test->getStatus() == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
171                 $ifStatus   = array('assigned', 'new', 'reopened');
172                 $newStatus  = 'closed';
173                 $message    = 'Automatically closed by PHPUnit (test passed).';
174                 $resolution = 'fixed';
175                 $cumulative = TRUE;
176             }
177
178             else if ($test->getStatus() == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
179                 $ifStatus   = array('closed');
180                 $newStatus  = 'reopened';
181                 $message    = 'Automatically reopened by PHPUnit (test failed).';
182                 $resolution = '';
183                 $cumulative = FALSE;
184             }
185
186             else {
187                 return;
188             }
189
190             $name    = $test->getName(FALSE);
191             $tickets = PHPUnit_Util_Test::getTickets(get_class($test), $name);
192
193             foreach ($tickets as $ticket) {
194                 // Remove this test from the totals (if it passed).
195                 if ($test->getStatus() == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
196                     unset($this->ticketCounts[$ticket][$name]);
197                 }
198
199                 // Only close tickets if ALL referenced cases pass
200                 // but reopen tickets if a single test fails.
201                 if ($cumulative) {
202                     // Determine number of to-pass tests:
203                     if (count($this->ticketCounts[$ticket]) > 0) {
204                         // There exist remaining test cases with this reference.
205                         $adjustTicket = FALSE;
206                     } else {
207                         // No remaining tickets, go ahead and adjust.
208                         $adjustTicket = TRUE;
209                     }
210                 } else {
211                     $adjustTicket = TRUE;
212                 }
213
214                 $ticketInfo = $this->getTicketInfo($ticket);
215
216                 if ($adjustTicket && in_array($ticketInfo['status'], $ifStatus)) {
217                     $this->updateTicket($ticket, $newStatus, $message, $resolution);
218                 }
219             }
220         }
221     }
222
223     abstract protected function getTicketInfo($ticketId = NULL);
224     abstract protected function updateTicket($ticketId, $newStatus, $message, $resolution);
225 }