GooglLibTest.php 4.2 KB

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