5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
38 * @subpackage Extensions_TicketListener
39 * @author Jan Sorgalla <jsorgalla@googlemail.com>
40 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @link http://www.phpunit.de/
43 * @since File available since Release 3.5.0
47 * A ticket listener that interacts with the GoogleCode issue API.
50 * @subpackage Extensions_TicketListener
51 * @author Jan Sorgalla <jsorgalla@googlemail.com>
52 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
53 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
54 * @version Release: 3.5.13
55 * @link http://www.phpunit.de/
56 * @since Class available since Release 3.5.0
58 class PHPUnit_Extensions_TicketListener_GoogleCode extends PHPUnit_Extensions_TicketListener
64 private $statusClosed;
65 private $statusReopened;
67 private $printTicketStateChanges;
69 private $authUrl = 'https://www.google.com/accounts/ClientLogin';
70 private $apiBaseUrl = 'http://code.google.com/feeds/issues/p/%s/issues';
74 * @param string $email The email associated with the Google account.
75 * @param string $password The password associated with the Google account.
76 * @param string $project The project name of the system under test (SUT) on Google Code.
77 * @param string $printTicketChanges Boolean flag to print the ticket state changes in the test result.
78 * @param string $statusClosed The status name of the closed state.
79 * @param string $statusReopened The status name of the reopened state.
80 * @throws RuntimeException
82 public function __construct($email, $password, $project, $printTicketStateChanges = FALSE, $statusClosed = 'Fixed', $statusReopened = 'Started')
84 if (!extension_loaded('curl')) {
85 throw new RuntimeException('ext/curl is not available');
88 if (!extension_loaded('simplexml')) {
89 throw new RuntimeException('ext/simplexml is not available');
92 $this->email = $email;
93 $this->password = $password;
94 $this->project = $project;
95 $this->statusClosed = $statusClosed;
96 $this->statusReopened = $statusReopened;
97 $this->printTicketStateChanges = $printTicketStateChanges;
98 $this->apiBaseUrl = sprintf($this->apiBaseUrl, $project);
102 * @param integer $ticketId
104 * @throws RuntimeException
106 public function getTicketInfo($ticketId = NULL)
108 if (!is_numeric($ticketId)) {
109 return array('status' => 'invalid_ticket_id');
112 $url = $this->apiBaseUrl . '/full/' . $ticketId;
114 'Authorization: GoogleLogin auth=' . $this->getAuthToken()
117 list($status, $response) = $this->callGoogleCode($url, $header);
119 if ($status != 200 || !$response) {
120 return array('state' => 'unknown_ticket');
123 $ticket = new SimpleXMLElement(str_replace("xmlns=", "ns=", $response));
124 $result = $ticket->xpath('//issues:state');
125 $state = (string)$result[0];
127 if ($state === 'open') {
128 return array('status' => 'new');
131 if ($state === 'closed') {
132 return array('status' => 'closed');
135 return array('status' => $state);
139 * @param string $ticketId The ticket number of the ticket under test (TUT).
140 * @param string $statusToBe The status of the TUT after running the associated test.
141 * @param string $message The additional message for the TUT.
142 * @param string $resolution The resolution for the TUT.
143 * @throws RuntimeException
145 protected function updateTicket($ticketId, $statusToBe, $message, $resolution)
147 $url = $this->apiBaseUrl . '/' . $ticketId . '/comments/full';
150 'Authorization: GoogleLogin auth=' . $this->getAuthToken(),
151 'Content-Type: application/atom+xml'
154 if ($statusToBe == 'closed') {
155 $ticketStatus = $this->statusClosed;
157 $ticketStatus = $this->statusReopened;
160 list($author,) = explode('@', $this->email);
162 $post = '<?xml version="1.0" encoding="UTF-8"?>' .
163 '<entry xmlns="http://www.w3.org/2005/Atom" ' .
164 ' xmlns:issues="http://schemas.google.com/projecthosting/issues/2009">' .
165 ' <content type="html">' . htmlspecialchars($message, ENT_COMPAT, 'UTF-8') . '</content>' .
167 ' <name>' . htmlspecialchars($author, ENT_COMPAT, 'UTF-8') . '</name>' .
169 ' <issues:updates>' .
170 ' <issues:status>' . htmlspecialchars($ticketStatus, ENT_COMPAT, 'UTF-8') . '</issues:status>' .
171 ' </issues:updates>' .
174 list($status, $response) = $this->callGoogleCode($url, $header, $post);
176 if ($status != 201) {
177 throw new RuntimeException('Updating GoogleCode issue failed with status code ' . $status);
180 if ($this->printTicketStateChanges) {
182 "\nUpdating GoogleCode issue #%d, status: %s\n",
190 * @return string The auth token
191 * @throws RuntimeException
193 private function getAuthToken()
195 if (NULL !== $this->authToken) {
196 return $this->authToken;
200 'Content-Type: application/x-www-form-urlencoded',
204 'accountType' => 'GOOGLE',
205 'Email' => $this->email,
206 'Passwd' => $this->password,
208 'source' => 'PHPUnit-TicketListener_GoogleCode-' . PHPUnit_Runner_Version::id(),
211 list($status, $response) = $this->callGoogleCode(
214 http_build_query($post, NULL, '&')
217 if ($status != 200) {
218 throw new RuntimeException('Google account authentication failed');
221 foreach (explode("\n", $response) as $line) {
222 if (strpos(trim($line), 'Auth') === 0) {
223 list($name, $token) = explode('=', $line);
224 $this->authToken = trim($token);
229 if (NULL === $this->authToken) {
230 throw new RuntimeException('Could not detect auth token in response');
233 return $this->authToken;
237 * @param string $url URL to call
238 * @param array $header Header
239 * @param string $post Post data
242 private function callGoogleCode($url, array $header = NULL, $post = NULL)
244 $curlHandle = curl_init();
246 curl_setopt($curlHandle, CURLOPT_URL, $url);
247 curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, TRUE);
248 curl_setopt($curlHandle, CURLOPT_FAILONERROR, TRUE);
249 curl_setopt($curlHandle, CURLOPT_FRESH_CONNECT, TRUE);
250 curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, TRUE);
251 curl_setopt($curlHandle, CURLOPT_HTTPPROXYTUNNEL, TRUE);
252 curl_setopt($curlHandle, CURLOPT_USERAGENT, __CLASS__);
253 curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, FALSE);
255 if (NULL !== $header) {
256 curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $header);
259 if (NULL !== $post) {
260 curl_setopt($curlHandle, CURLOPT_POST, TRUE);
261 curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post);
264 $response = curl_exec($curlHandle);
265 $status = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
268 throw new RuntimeException(curl_error($curlHandle));
271 curl_close($curlHandle);
273 return array($status, $response);