]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Extensions/TicketListener/Trac.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Extensions / TicketListener / Trac.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sb@sebastian-bergmann.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  * @category   Testing
38  * @package    PHPUnit
39  * @author     Graham Christensen <graham@grahamc.com>
40  * @author     Sean Coates <sean@caedmon.net>
41  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
42  * @copyright  2002-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
43  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
44  * @link       http://www.phpunit.de/
45  * @since      Class available since Release 3.5.4
46  */
47
48 /**
49  * A ticket listener that interacts with Trac.
50  *
51  * <code>
52  * <phpunit>
53  *  <listeners>
54  *   <!-- You may need to update the path to the TicketListener -->
55  *   <listener class="PHPUnit_Extensions_TicketListener_Trac"
56  *             file="/usr/lib/php/PHPUnit/Extensions/TicketListener/Trac.php">
57  *    <arguments>
58  *     <!-- A user and their password. This user must have XML_RPC permissions -->
59  *     <string>trac_username</string>
60  *     <string>trac_password</string>
61  *     <!-- The URL for your XML-RPC endpoint. -->
62  *     <!-- For example, if trac is at 127.0.0.1/trac: -->
63  *     <string>127.0.0.1/trac/login/xmlrpc</string>
64  *    </arguments>
65  *   </listener>
66  *  </listeners>
67  * </phpunit>
68  * </code>
69  *
70  * @category   Testing
71  * @package    PHPUnit
72  * @author     Graham Christensen <graham@grahamc.com>
73  * @author     Sean Coates <sean@caedmon.net>
74  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
75  * @copyright  2002-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
76  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
77  * @version    Release: 3.5.13
78  * @link       http://www.phpunit.de/
79  * @since      Class available since Release 3.5.4
80  */
81 class PHPUnit_Extensions_TicketListener_Trac extends PHPUnit_Extensions_TicketListener
82 {
83     protected $username;
84     protected $password;
85     protected $hostpath;
86     protected $scheme;
87     private $printTicketStateChanges;
88
89     /**
90      * Constructor
91      *
92      * @param string $username Trac-XMLRPC username
93      * @param string $password Trac-XMLRPC password
94      * @param string $hostpath Trac-XMLRPC Host+Path (e.g. example.com/trac/login/xmlrpc)
95      * @param string $scheme Trac scheme (http or https)
96      * @param bool   $printTicketStateChanges To display changes or not
97      */
98     public function __construct($username, $password, $hostpath, $scheme = 'http', $printTicketStateChanges = FALSE)
99     {
100         $this->username                = $username;
101         $this->password                = $password;
102         $this->hostpath                = $hostpath;
103         $this->scheme                  = $scheme;
104         $this->printTicketStateChanges = $printTicketStateChanges;
105     }
106
107     /**
108      * Get the status of a ticket message
109      *
110      * @param  integer $ticketId The ticket ID
111      * @return array('status' => $status) ($status = new|closed|unknown_ticket)
112      */
113     public function getTicketInfo($ticketId = NULL)
114     {
115         if (!is_numeric($ticketId)) {
116             return array('status' => 'invalid_ticket_id');
117         }
118
119         try {
120             $info = $this->getClient()->get($ticketId);
121
122             switch ($info[3]['status']) {
123                 case 'closed': {
124                     return array('status' => 'closed');
125                 }
126                 break;
127
128                 case 'new':
129                 case 'reopened': {
130                     return array('status' => 'new');
131                 }
132                 break;
133
134                 default: {
135                     return array('status' => 'unknown_ticket');
136                 }
137             }
138         }
139
140         catch (Exception $e) {
141             return array('status' => 'unknown_ticket');
142         }
143     }
144
145     /**
146      * Update a ticket with a new status
147      *
148      * @param string $ticketId   The ticket number of the ticket under test (TUT).
149      * @param string $statusToBe The status of the TUT after running the associated test.
150      * @param string $message    The additional message for the TUT.
151      * @param string $resolution The resolution for the TUT.
152      */
153     protected function updateTicket($ticketId, $statusToBe, $message, $resolution)
154     {
155         $change = array('status' => $statusToBe, 'resolution' => $resolution);
156
157         $this->getClient()->update((int)$ticketId, $message, $change);
158
159         if ($this->printTicketStateChanges) {
160             printf(
161               "\nUpdating Trac issue #%d, status: %s\n", $ticketId, $statusToBe
162             );
163         }
164     }
165
166     /**
167      * Get a Trac XML_RPC2 client
168      *
169      * @return XML_RPC2_Client
170      */
171     protected function getClient()
172     {
173         if (!PHPUnit_Util_Filesystem::fileExistsInIncludePath('XML/RPC2/Client.php')) {
174             throw new PHPUnit_Framework_Exception('PEAR/XML_RPC2 is not available.');
175         }
176
177         require_once 'XML/RPC2/Client.php';
178
179         $url = sprintf(
180           '%s://%s:%s@%s',
181           $this->scheme,
182           $this->username,
183           $this->password,
184           $this->hostpath
185         );
186
187         return XML_RPC2_Client::create($url, array('prefix' => 'ticket.'));
188     }
189 }