BitLyTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Service_ShortUrl_BitLy */
  22. require_once 'Zend/Service/ShortUrl/BitLy.php';
  23. /**
  24. * @package Zend_Service
  25. * @subpackage UnitTests
  26. */
  27. class Zend_Service_ShortUrl_BitLyTest extends PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * reset zend service http client
  31. *
  32. * @return void
  33. */
  34. public function setUp ()
  35. {
  36. if (!defined('TESTS_ZEND_SERVICE_SHORTURL_BITLY_ENABLED')
  37. || !constant('TESTS_ZEND_SERVICE_SHORTURL_BITLY_ENABLED')
  38. ) {
  39. $this->markTestSkipped('Testing Zend_Service_ShortUrl_BitLyTest only works when TESTS_ZEND_SERVICE_SHORTURL_BITLY_ENABLED is set.');
  40. }
  41. Zend_Service_Abstract::setHttpClient(new Zend_Http_Client());
  42. }
  43. public function testShortenEmptyUrlException()
  44. {
  45. $this->setExpectedException('Zend_Service_ShortUrl_Exception');
  46. $s = new Zend_Service_ShortUrl_BitLy('test');
  47. $s->shorten('');
  48. }
  49. public function testShortenIncorrectUrlException()
  50. {
  51. $this->setExpectedException('Zend_Service_ShortUrl_Exception');
  52. $s = new Zend_Service_ShortUrl_BitLy('test');
  53. $s->shorten('wrongAdress.cccc');
  54. }
  55. public function testExceptionOnBadApiResponse()
  56. {
  57. $this->setExpectedException('Zend_Service_ShortUrl_Exception');
  58. $clientResponse = $this->getMock('Zend_Http_Response', array(), array(), '', false);
  59. $clientResponse->expects($this->once())->method('getStatus')->will($this->returnValue(500));
  60. $client = $this->getMock('Zend_Http_Client', array(), array(), '', false);
  61. $client->expects($this->once())->method('request')->will($this->returnValue($clientResponse));
  62. $s = new Zend_Service_ShortUrl_BitLy('test');
  63. $s->setHttpClient($client);
  64. $s->shorten('http://framework.zend.com');
  65. }
  66. public function testAuthenticationWithAccessToken()
  67. {
  68. $accessToken = 'test';
  69. $clientResponse = $this->getMock('Zend_Http_Response', array(), array(), '', false);
  70. $clientResponse->expects($this->once())->method('getStatus')->will($this->returnValue(200));
  71. $clientResponse->expects($this->once())->method('getBody')->will($this->returnValue('http://bit.ly/ZFramework'));
  72. $client = $this->getMock('Zend_Http_Client', array(), array(), '', false);
  73. $client->expects($this->any())->method('setParameterGet')->with($this->anything(),$this->anything());
  74. $client->expects($this->at(0))->method('setParameterGet')->with('access_token',$accessToken);
  75. $client->expects($this->once())->method('request')->will($this->returnValue($clientResponse));
  76. $s = new Zend_Service_ShortUrl_BitLy($accessToken);
  77. $s->setHttpClient($client);
  78. $s->shorten('http://framework.zend.com');
  79. }
  80. public function testAuthenticationWithUserCredentials()
  81. {
  82. $login = 'test';
  83. $apiKey = 'api';
  84. $clientResponse = $this->getMock('Zend_Http_Response', array(), array(), '', false);
  85. $clientResponse->expects($this->once())->method('getStatus')->will($this->returnValue(200));
  86. $clientResponse->expects($this->once())->method('getBody')->will($this->returnValue('http://bit.ly/ZFramework'));
  87. $client = $this->getMock('Zend_Http_Client', array(), array(), '', false);
  88. $client->expects($this->any())->method('setParameterGet')->with($this->anything(),$this->anything());
  89. $client->expects($this->at(0))->method('setParameterGet')->with('login',$login);
  90. $client->expects($this->at(1))->method('setParameterGet')->with('apiKey',$apiKey);
  91. $client->expects($this->once())->method('request')->will($this->returnValue($clientResponse));
  92. $s = new Zend_Service_ShortUrl_BitLy($login, $apiKey);
  93. $s->setHttpClient($client);
  94. $s->shorten('http://framework.zend.com');
  95. }
  96. }