1<?php 2 3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 4 5/** 6 * Exception class test cases for the Crypt_GPG package. 7 * 8 * These tests require the PHPUnit 3.6 or greater package to be installed. 9 * PHPUnit is installable using PEAR. See the 10 * {@link http://www.phpunit.de/manual/3.6/en/installation.html manual} 11 * for detailed installation instructions. 12 * 13 * To run these tests, use: 14 * <code> 15 * $ phpunit ExceptionsTestCase 16 * </code> 17 * 18 * LICENSE: 19 * 20 * This library is free software; you can redistribute it and/or modify 21 * it under the terms of the GNU Lesser General Public License as 22 * published by the Free Software Foundation; either version 2.1 of the 23 * License, or (at your option) any later version. 24 * 25 * This library is distributed in the hope that it will be useful, 26 * but WITHOUT ANY WARRANTY; without even the implied warranty of 27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 28 * Lesser General Public License for more details. 29 * 30 * You should have received a copy of the GNU Lesser General Public 31 * License along with this library; if not, see 32 * <http://www.gnu.org/licenses/> 33 * 34 * @category Encryption 35 * @package Crypt_GPG 36 * @author Michael Gauthier <mike@silverorange.com> 37 * @copyright 2008-2011 silverorange 38 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 39 * @version CVS: $Id$ 40 * @link http://pear.php.net/package/Crypt_GPG 41 */ 42 43/** 44 * Base test case. 45 */ 46require_once 'TestCase.php'; 47 48/** 49 * Exception classes. 50 */ 51require_once 'Crypt/GPG/Exceptions.php'; 52 53/** 54 * Exception classes tests for Crypt_GPG. 55 * 56 * @category Encryption 57 * @package Crypt_GPG 58 * @author Michael Gauthier <mike@silverorange.com> 59 * @copyright 2008-2011 silverorange 60 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 61 * @link http://pear.php.net/package/Crypt_GPG 62 */ 63class ExceptionsTest extends Crypt_GPG_TestCase 64{ 65 /** 66 * @group exception 67 */ 68 public function testException() 69 { 70 $this->expectException('Crypt_GPG_Exception'); 71 $this->expectExceptionMessage('test exception'); 72 73 throw new Crypt_GPG_Exception('test exception'); 74 } 75 76 /** 77 * @group file-exception 78 */ 79 public function testFileException() 80 { 81 $this->expectException('Crypt_GPG_FileException'); 82 $this->expectExceptionMessage('test exception'); 83 84 throw new Crypt_GPG_FileException('test exception'); 85 } 86 87 /** 88 * @group file-exception 89 */ 90 public function testFileException_getFilename() 91 { 92 $e = new Crypt_GPG_FileException('test exception', 0, 93 'test-filename.php'); 94 95 $this->assertEquals('test-filename.php', $e->getFilename()); 96 } 97 98 /** 99 * @group open-subprocess-exception 100 */ 101 public function testOpenSubprocessException() 102 { 103 $this->expectException('Crypt_GPG_OpenSubprocessException'); 104 $this->expectExceptionMessage('test exception'); 105 106 throw new Crypt_GPG_OpenSubprocessException('test exception'); 107 } 108 109 /** 110 * @group open-subprocess-exception 111 */ 112 public function testOpenSubprocessException_getCommand() 113 { 114 $e = new Crypt_GPG_OpenSubprocessException('test exception', 0, 115 'gpg --verify'); 116 117 $this->assertEquals('gpg --verify', $e->getCommand()); 118 } 119 120 /** 121 * @group invalid-operation-exception 122 */ 123 public function testInvalidOperationException() 124 { 125 $this->expectException('Crypt_GPG_InvalidOperationException'); 126 $this->expectExceptionMessage('test exception'); 127 128 throw new Crypt_GPG_InvalidOperationException('test exception'); 129 } 130 131 /** 132 * @group invalid-operation-exception 133 */ 134 public function testInvalidOperationException_getOperation() 135 { 136 $e = new Crypt_GPG_InvalidOperationException('test exception', 0, 137 '--verify'); 138 139 $this->assertEquals('--verify', $e->getOperation()); 140 } 141 142 /** 143 * @group key-not-found-exception 144 */ 145 public function testKeyNotFoundException() 146 { 147 $this->expectException('Crypt_GPG_KeyNotFoundException'); 148 $this->expectExceptionMessage('test exception'); 149 150 throw new Crypt_GPG_KeyNotFoundException('test exception'); 151 } 152 153 /** 154 * @group key-not-found-exception 155 */ 156 public function testKeyNotFoundException_getKeyId() 157 { 158 $e = new Crypt_GPG_KeyNotFoundException('test exception', 0, 159 '9F93F9116728EF12'); 160 161 $this->assertEquals('9F93F9116728EF12', $e->getKeyId()); 162 } 163 164 /** 165 * @group no-data-exception 166 */ 167 public function testNoDataException() 168 { 169 $this->expectException('Crypt_GPG_NoDataException'); 170 $this->expectExceptionMessage('test exception'); 171 172 throw new Crypt_GPG_NoDataException('test exception'); 173 } 174 175 /** 176 * @group bad-passphrase-exception 177 */ 178 public function testBadPassphraseException() 179 { 180 $this->expectException('Crypt_GPG_BadPassphraseException'); 181 $this->expectExceptionMessage('test exception'); 182 183 throw new Crypt_GPG_BadPassphraseException('test exception'); 184 } 185 186 /** 187 * @group bad-passphrase-exception 188 */ 189 public function testBadPassphraseException_getBadPassphrases() 190 { 191 $e = new Crypt_GPG_BadPassphraseException('test exception', 0, 192 array('C097D9EC94C06363', '9F93F9116728EF12')); 193 194 $keyIds = $e->getBadPassphrases(); 195 $this->assertTrue(is_array($keyIds), 'Failed to assert returned ' . 196 'key ids for bad passphrases is an array.'); 197 198 $this->assertContains('C097D9EC94C06363', $keyIds); 199 $this->assertContains('9F93F9116728EF12', $keyIds); 200 } 201 202 /** 203 * @group bad-passphrase-exception 204 */ 205 public function testBadPassphraseException_getMissingPassphrase() 206 { 207 $e = new Crypt_GPG_BadPassphraseException('test exception', 0, array(), 208 array('C097D9EC94C06363', '9F93F9116728EF12')); 209 210 $keyIds = $e->getMissingPassphrases(); 211 $this->assertTrue(is_array($keyIds), 'Failed to assert returned ' . 212 'key ids for missing passphrases is an array.'); 213 214 $this->assertContains('C097D9EC94C06363', $keyIds); 215 $this->assertContains('9F93F9116728EF12', $keyIds); 216 } 217 218 /** 219 * @group delete-private-key-exception 220 */ 221 public function testDeletePrivateKeyException() 222 { 223 $this->expectException('Crypt_GPG_DeletePrivateKeyException'); 224 $this->expectExceptionMessage('test exception'); 225 226 throw new Crypt_GPG_DeletePrivateKeyException('test exception'); 227 } 228 229 /** 230 * @group delete-private-key-exception 231 */ 232 public function testDeletePrivateKeyException_getKeyId() 233 { 234 $e = new Crypt_GPG_DeletePrivateKeyException('test exception', 0, 235 '9F93F9116728EF12'); 236 237 $this->assertEquals('9F93F9116728EF12', $e->getKeyId()); 238 } 239} 240