BitLyTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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-2012 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. Zend_Service_Abstract::setHttpClient(new Zend_Http_Client());
  37. }
  38. public function testShortenEmptyUrlException()
  39. {
  40. $this->setExpectedException('Zend_Service_ShortUrl_Exception');
  41. $s = new Zend_Service_ShortUrl_BitLy('test');
  42. $s->shorten('');
  43. }
  44. public function testShortenIncorrectUrlException()
  45. {
  46. $this->setExpectedException('Zend_Service_ShortUrl_Exception');
  47. $s = new Zend_Service_ShortUrl_BitLy('test');
  48. $s->shorten('wrongAdress.cccc');
  49. }
  50. public function testExceptionOnBadApiResponse()
  51. {
  52. $this->setExpectedException('Zend_Service_ShortUrl_Exception');
  53. $clientResponse = $this->getMock('Zend_Http_Response', array(), array(), '', false);
  54. $clientResponse->expects($this->once())->method('getStatus')->will($this->returnValue(500));
  55. $client = $this->getMock('Zend_Http_Client', array(), array(), '', false);
  56. $client->expects($this->once())->method('request')->will($this->returnValue($clientResponse));
  57. $s = new Zend_Service_ShortUrl_BitLy('test');
  58. $s->setHttpClient($client);
  59. $s->shorten('http://framework.zend.com');
  60. }
  61. public function testAuthenticationWithAccessToken()
  62. {
  63. $accessToken = 'test';
  64. $clientResponse = $this->getMock('Zend_Http_Response', array(), array(), '', false);
  65. $clientResponse->expects($this->once())->method('getStatus')->will($this->returnValue(200));
  66. $clientResponse->expects($this->once())->method('getBody')->will($this->returnValue('http://bit.ly/ZFramework'));
  67. $client = $this->getMock('Zend_Http_Client', array(), array(), '', false);
  68. $client->expects($this->any())->method('setParameterGet')->with($this->anything(),$this->anything());
  69. $client->expects($this->at(0))->method('setParameterGet')->with('access_token',$accessToken);
  70. $client->expects($this->once())->method('request')->will($this->returnValue($clientResponse));
  71. $s = new Zend_Service_ShortUrl_BitLy($accessToken);
  72. $s->setHttpClient($client);
  73. $s->shorten('http://framework.zend.com');
  74. }
  75. public function testAuthenticationWithUserCredentials()
  76. {
  77. $login = 'test';
  78. $apiKey = 'api';
  79. $clientResponse = $this->getMock('Zend_Http_Response', array(), array(), '', false);
  80. $clientResponse->expects($this->once())->method('getStatus')->will($this->returnValue(200));
  81. $clientResponse->expects($this->once())->method('getBody')->will($this->returnValue('http://bit.ly/ZFramework'));
  82. $client = $this->getMock('Zend_Http_Client', array(), array(), '', false);
  83. $client->expects($this->any())->method('setParameterGet')->with($this->anything(),$this->anything());
  84. $client->expects($this->at(0))->method('setParameterGet')->with('login',$login);
  85. $client->expects($this->at(1))->method('setParameterGet')->with('apiKey',$apiKey);
  86. $client->expects($this->once())->method('request')->will($this->returnValue($clientResponse));
  87. $s = new Zend_Service_ShortUrl_BitLy($login, $apiKey);
  88. $s->setHttpClient($client);
  89. $s->shorten('http://framework.zend.com');
  90. }
  91. }