ServiceExceptionTest.php 4.5 KB

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