2
0

ServiceExceptionTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata_Gapps
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. require_once 'PHPUnit/Framework/TestCase.php';
  23. require_once 'Zend/Gdata/Gapps/ServiceException.php';
  24. require_once 'Zend/Gdata/Gapps/Error.php';
  25. require_once 'Zend/Gdata/Gapps.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Gdata_Gapps
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Gdata
  33. * @group Zend_Gdata_Gapps
  34. */
  35. class Zend_Gdata_Gapps_ServiceExceptionTest extends PHPUnit_Framework_TestCase
  36. {
  37. protected $fixture;
  38. protected $data;
  39. public function setUp() {
  40. $this->xmlSample = file_get_contents(
  41. 'Zend/Gdata/Gapps/_files/AppsForYourDomainElementSample1.xml',
  42. true);
  43. $this->fixture = new Zend_Gdata_Gapps_ServiceException();
  44. $this->data[1] = new Zend_Gdata_Gapps_Error(1234, "foo", "bar");
  45. $this->data[2] = new Zend_Gdata_Gapps_Error(4317, "blah", "woof");
  46. $this->data[3] = new Zend_Gdata_Gapps_Error(5978, "blue", "puppy");
  47. $this->data[4] = new Zend_Gdata_Gapps_Error(2398, "red", "kitten");
  48. }
  49. /**
  50. * @expectedException Zend_Gdata_Gapps_ServiceException
  51. */
  52. public function testCanThrowServiceException() {
  53. throw $this->fixture;
  54. }
  55. public function testCanSetAndGetErrorArray() {
  56. $this->fixture->setErrors($this->data);
  57. $incoming = $this->fixture->getErrors();
  58. $this->assertTrue(is_array($incoming));
  59. $this->assertEquals(count($this->data), count($incoming));
  60. foreach ($this->data as $i) {
  61. $this->assertEquals($i, $incoming[$i->getErrorCode()]);
  62. }
  63. }
  64. public function testCanInsertSingleError() {
  65. $this->fixture->setErrors($this->data);
  66. $outgoing = new Zend_Gdata_Gapps_Error(1111, "a", "b");
  67. $this->fixture->addError($outgoing);
  68. $result = $this->fixture->getError(1111);
  69. $this->assertEquals($outgoing, $result);
  70. }
  71. public function testCanSetPropertiesViaConstructor() {
  72. $this->fixture = new Zend_Gdata_Gapps_ServiceException($this->data);
  73. $incoming = $this->fixture->getErrors();
  74. $this->assertTrue(is_array($incoming));
  75. $this->assertEquals(count($this->data), count($incoming));
  76. foreach($this->data as $i) {
  77. $this->assertEquals($i, $incoming[$i->getErrorCode()]);
  78. }
  79. }
  80. public function testCanRetrieveASpecificErrorByCode() {
  81. $this->fixture->setErrors($this->data);
  82. $result = $this->fixture->getError(5978);
  83. $this->assertEquals($this->data[3], $result);
  84. }
  85. public function testRetrievingNonexistantErrorCodeReturnsNull() {
  86. $this->fixture->setErrors($this->data);
  87. $result = $this->fixture->getError(0000);
  88. $this->assertEquals(null, $result);
  89. }
  90. public function testCanCheckIfAKeyExists() {
  91. $this->fixture->setErrors($this->data);
  92. $this->assertTrue($this->fixture->hasError(2398));
  93. $this->assertFalse($this->fixture->hasError(0000));
  94. }
  95. public function testCanConvertFromXML() {
  96. $this->fixture->importFromString($this->xmlSample);
  97. $incoming = $this->fixture->getErrors();
  98. $this->assertTrue(is_array($incoming));
  99. $this->assertEquals(3, count($incoming));
  100. $this->assertEquals("9925", $incoming[9925]->errorCode);
  101. $this->assertEquals("Foo", $incoming[9925]->invalidInput);
  102. $this->assertEquals("Bar", $incoming[9925]->reason);
  103. }
  104. public function testCanConvertToString() {
  105. $this->fixture->setErrors($this->data);
  106. $this->assertEquals("The server encountered the following errors processing the request:
  107. Error 1234: foo
  108. \tInvalid Input: \"bar\"
  109. Error 4317: blah
  110. \tInvalid Input: \"woof\"
  111. Error 5978: blue
  112. \tInvalid Input: \"puppy\"
  113. Error 2398: red
  114. \tInvalid Input: \"kitten\"", $this->fixture->__toString());
  115. }
  116. }