GooglLibTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. App::uses('GooglLib', 'Tools.Lib');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. /**
  5. */
  6. class GooglLibTest extends MyCakeTestCase {
  7. public function setUp() {
  8. parent::setUp();
  9. //Configure::write('Googl.key', 'YOUR KEY');
  10. $this->Googl = new TestGooglLib();
  11. $this->Googl->setLive(!$this->isDebug());
  12. }
  13. public function tearDown() {
  14. parent::tearDown();
  15. unset($this->Googl);
  16. }
  17. //TODO
  18. public function testOAuth() {
  19. }
  20. public function testHistory() {
  21. $this->skipIf(true, 'Login required');
  22. $is = $this->Googl->getHistory();
  23. $this->debug($is);
  24. }
  25. /**
  26. * GooglLibTest::testShortenAndUnshorten()
  27. *
  28. * @return void
  29. */
  30. public function testShortenAndUnshorten() {
  31. // Shorten without key (publically)
  32. Configure::write('Googl.key', '');
  33. $url = 'http://www.spiegel.de';
  34. $is = $this->Googl->getShort($url);
  35. $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['longUrl'] == $url . '/');
  36. // Unshorten
  37. $shortUrl = $is['id'];
  38. $is = $this->Googl->getLong($shortUrl);
  39. $this->assertTrue(!empty($is));
  40. $this->assertTrue(!empty($is['id']));
  41. $this->assertSame('urlshortener#url', $is['kind']);
  42. $this->assertSame('OK', $is['status']);
  43. $this->assertSame($url . '/', $is['longUrl']);
  44. }
  45. /**
  46. * GooglLibTest::testApi()
  47. *
  48. * @return void
  49. */
  50. public function testApi() {
  51. $this->skipIf(!Configure::read('Googl.key'), 'No Api Key found');
  52. // Shorten with key
  53. $url = 'http://www.blue.de';
  54. $is = $this->Googl->getShort($url);
  55. $this->debug($is);
  56. $res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['longUrl'] == $url . '/');
  57. // Unshorten
  58. $shortUrl = $is['id'];
  59. $is = $this->Googl->getLong($shortUrl);
  60. $this->debug($is);
  61. $res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['status'] === 'OK' && $is['longUrl'] == $url . '/');
  62. // FULL INFOS
  63. $url = 'http://www.web.de#123456';
  64. $is = $this->Googl->getShort($url);
  65. $this->debug($is);
  66. $res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['longUrl'] === 'http://www.web.de/#123456');
  67. $shortUrl = $is['id'];
  68. $is = $this->Googl->getLong($shortUrl, GooglLib::PROJECTION_CLICKS);
  69. $this->debug($is);
  70. $res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['status'] === 'OK' && $is['longUrl'] === 'http://www.web.de/#123456');
  71. }
  72. }
  73. /**
  74. * Wrapper to mock the API calls away
  75. */
  76. class TestGooglLib extends GooglLib {
  77. protected $_debug = true;
  78. protected $_map = [
  79. 'http://www.spiegel.de' => '{
  80. "kind": "urlshortener#url",
  81. "id": "http://goo.gl/nBBg",
  82. "longUrl": "http://www.spiegel.de/"
  83. }',
  84. 'http://goo.gl/nBBg' => '{
  85. "kind": "urlshortener#url",
  86. "id": "http://goo.gl/nBBg",
  87. "longUrl": "http://www.spiegel.de/",
  88. "status": "OK"
  89. }',
  90. 'http://www.blue.de' => '{
  91. "kind": "urlshortener#url",
  92. "id": "http://goo.gl/leVfu4",
  93. "longUrl": "http://www.blue.de/"
  94. }',
  95. 'http://goo.gl/leVfu4' => '{
  96. "kind": "urlshortener#url",
  97. "id": "http://goo.gl/leVfu4",
  98. "longUrl": "http://www.blue.de/",
  99. "status": "OK"
  100. }',
  101. 'http://www.web.de#123456' => '{
  102. "kind": "urlshortener#url",
  103. "id": "http://goo.gl/7937W",
  104. "longUrl": "http://www.web.de/#123456"
  105. }',
  106. 'http://goo.gl/7937W' => '{
  107. "kind": "urlshortener#url",
  108. "id": "http://goo.gl/7937W",
  109. "longUrl": "http://www.web.de/#123456",
  110. "status": "OK",
  111. "analytics": {
  112. "allTime": {
  113. "shortUrlClicks": "1",
  114. "longUrlClicks": "1"
  115. },
  116. "month": {
  117. "shortUrlClicks": "0",
  118. "longUrlClicks": "0"
  119. },
  120. "week": {
  121. "shortUrlClicks": "0",
  122. "longUrlClicks": "0"
  123. },
  124. "day": {
  125. "shortUrlClicks": "0",
  126. "longUrlClicks": "0"
  127. },
  128. "twoHours": {
  129. "shortUrlClicks": "0",
  130. "longUrlClicks": "0"
  131. }
  132. }
  133. }'
  134. ];
  135. public function setLive($live = true) {
  136. $this->_debug = !$live;
  137. }
  138. public function getShort($url) {
  139. if ($this->_debug) {
  140. return json_decode($this->_map[$url], true);
  141. }
  142. return parent::getShort($url);
  143. }
  144. public function getLong($url, $projection = null) {
  145. if ($this->_debug) {
  146. return json_decode($this->_map[$url], true);
  147. }
  148. return parent::getLong($url, $projection);
  149. }
  150. }