AudioscrobblerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_Service_Audioscrobbler
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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. /**
  23. * @see Zend_Service_Audioscrobbler
  24. */
  25. require_once 'Zend/Service/Audioscrobbler.php';
  26. require_once 'AudioscrobblerTestCase.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Service_Audioscrobbler
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Service
  34. * @group Zend_Service_Audioscrobbler
  35. */
  36. class Zend_Service_Audioscrobbler_AudioscrobblerTest extends Zend_Service_Audioscrobbler_AudioscrobblerTestCase
  37. {
  38. public function testRequestThrowsHttpClientExceptionWithNoUserError()
  39. {
  40. $this->setAudioscrobblerResponse(self::readTestResponse('errorNoUserExists'));
  41. $as = $this->getAudioscrobblerService();
  42. $as->set('user', 'foobarfoo');
  43. try {
  44. $response = $as->userGetProfileInformation();
  45. $this->fail('Expected Zend_Http_Client_Exception not thrown');
  46. } catch(Zend_Http_Client_Exception $e) {
  47. $this->assertContains("No user exists with this name", $e->getMessage());
  48. }
  49. }
  50. public function testRequestThrowsHttpClientExceptionWithoutSuccessfulResponse()
  51. {
  52. $this->setAudioscrobblerResponse(self::readTestResponse('errorResponseStatusError'));
  53. $as = $this->getAudioscrobblerService();
  54. $as->set('user', 'foobarfoo');
  55. try {
  56. $response = $as->userGetProfileInformation();
  57. $this->fail('Expected Zend_Http_Client_Exception not thrown');
  58. } catch(Zend_Http_Client_Exception $e) {
  59. $this->assertContains("404", $e->getMessage());
  60. }
  61. }
  62. /**
  63. * @group ZF-4509
  64. */
  65. public function testSetViaCallIntercept()
  66. {
  67. $as = new Zend_Service_Audioscrobbler();
  68. $as->setUser("foobar");
  69. $as->setAlbum("Baz");
  70. $this->assertEquals("foobar", $as->get("user"));
  71. $this->assertEquals("Baz", $as->get("album"));
  72. }
  73. /**
  74. * @group ZF-6251
  75. */
  76. public function testUnknownMethodViaCallInterceptThrowsException()
  77. {
  78. $this->setExpectedException("Zend_Service_Exception");
  79. $as = new Zend_Service_Audioscrobbler();
  80. $as->someInvalidMethod();
  81. }
  82. /**
  83. * @group ZF-6251
  84. */
  85. public function testCallInterceptMethodsRequireExactlyOneParameterAndThrowExceptionOtherwise()
  86. {
  87. $this->setExpectedException("Zend_Service_Exception");
  88. $as = new Zend_Service_Audioscrobbler();
  89. $as->setUser();
  90. }
  91. public static function readTestResponse($file)
  92. {
  93. $message = file_get_contents(sprintf('%s/_files/%s', dirname(__FILE__), $file));
  94. // Line endings are sometimes an issue inside the canned responses; the
  95. // following is a negative lookbehind assertion, and replaces any \n
  96. // not preceded by \r with the sequence \r\n, ensuring that the message
  97. // is well-formed.
  98. return preg_replace("#(?<!\r)\n#", "\r\n", $message);
  99. }
  100. }