ServiceExceptionTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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) 2006 Zend Technologies USA Inc. (http://www.zend.com);
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'PHPUnit/Framework/TestCase.php';
  22. require_once 'Zend/Gdata/Gapps/ServiceException.php';
  23. require_once 'Zend/Gdata/Gapps/Error.php';
  24. require_once 'Zend/Gdata/Gapps.php';
  25. /**
  26. * @package Zend_Gdata
  27. * @subpackage UnitTests
  28. */
  29. class Zend_Gdata_Gapps_ServiceExceptionTest extends PHPUnit_Framework_TestCase
  30. {
  31. protected $fixture;
  32. protected $data;
  33. public function setUp() {
  34. $this->xmlSample = file_get_contents(
  35. 'Zend/Gdata/Gapps/_files/AppsForYourDomainElementSample1.xml',
  36. true);
  37. $this->fixture = new Zend_Gdata_Gapps_ServiceException();
  38. $this->data[1] = new Zend_Gdata_Gapps_Error(1234, "foo", "bar");
  39. $this->data[2] = new Zend_Gdata_Gapps_Error(4317, "blah", "woof");
  40. $this->data[3] = new Zend_Gdata_Gapps_Error(5978, "blue", "puppy");
  41. $this->data[4] = new Zend_Gdata_Gapps_Error(2398, "red", "kitten");
  42. }
  43. /**
  44. * @expectedException Zend_Gdata_Gapps_ServiceException
  45. */
  46. public function testCanThrowServiceException() {
  47. throw $this->fixture;
  48. }
  49. public function testCanSetAndGetErrorArray() {
  50. $this->fixture->setErrors($this->data);
  51. $incoming = $this->fixture->getErrors();
  52. $this->assertTrue(is_array($incoming));
  53. $this->assertEquals(count($this->data), count($incoming));
  54. foreach ($this->data as $i) {
  55. $this->assertEquals($i, $incoming[$i->getErrorCode()]);
  56. }
  57. }
  58. public function testCanInsertSingleError() {
  59. $this->fixture->setErrors($this->data);
  60. $outgoing = new Zend_Gdata_Gapps_Error(1111, "a", "b");
  61. $this->fixture->addError($outgoing);
  62. $result = $this->fixture->getError(1111);
  63. $this->assertEquals($outgoing, $result);
  64. }
  65. public function testCanSetPropertiesViaConstructor() {
  66. $this->fixture = new Zend_Gdata_Gapps_ServiceException($this->data);
  67. $incoming = $this->fixture->getErrors();
  68. $this->assertTrue(is_array($incoming));
  69. $this->assertEquals(count($this->data), count($incoming));
  70. foreach($this->data as $i) {
  71. $this->assertEquals($i, $incoming[$i->getErrorCode()]);
  72. }
  73. }
  74. public function testCanRetrieveASpecificErrorByCode() {
  75. $this->fixture->setErrors($this->data);
  76. $result = $this->fixture->getError(5978);
  77. $this->assertEquals($this->data[3], $result);
  78. }
  79. public function testRetrievingNonexistantErrorCodeReturnsNull() {
  80. $this->fixture->setErrors($this->data);
  81. $result = $this->fixture->getError(0000);
  82. $this->assertEquals(null, $result);
  83. }
  84. public function testCanCheckIfAKeyExists() {
  85. $this->fixture->setErrors($this->data);
  86. $this->assertTrue($this->fixture->hasError(2398));
  87. $this->assertFalse($this->fixture->hasError(0000));
  88. }
  89. public function testCanConvertFromXML() {
  90. $this->fixture->importFromString($this->xmlSample);
  91. $incoming = $this->fixture->getErrors();
  92. $this->assertTrue(is_array($incoming));
  93. $this->assertEquals(3, count($incoming));
  94. $this->assertEquals("9925", $incoming[9925]->errorCode);
  95. $this->assertEquals("Foo", $incoming[9925]->invalidInput);
  96. $this->assertEquals("Bar", $incoming[9925]->reason);
  97. }
  98. public function testCanConvertToString() {
  99. $this->fixture->setErrors($this->data);
  100. $this->assertEquals("The server encountered the following errors processing the request:
  101. Error 1234: foo
  102. \tInvalid Input: \"bar\"
  103. Error 4317: blah
  104. \tInvalid Input: \"woof\"
  105. Error 5978: blue
  106. \tInvalid Input: \"puppy\"
  107. Error 2398: red
  108. \tInvalid Input: \"kitten\"", $this->fixture->__toString());
  109. }
  110. }