| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- App::uses('GooglLib', 'Tools.Lib');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- /**
- */
- class GooglLibTest extends MyCakeTestCase {
- public function setUp() {
- parent::setUp();
- //Configure::write('Googl.key', 'YOUR KEY');
- $this->Googl = new TestGooglLib();
- if ($this->isDebug()) {
- $this->Googl->setLive();
- }
- }
- public function tearDown() {
- parent::tearDown();
- unset($this->Googl);
- }
- //TODO
- public function testOAuth() {
- }
- public function testHistory() {
- $this->skipIf(true, 'Login required');
- $is = $this->Googl->getHistory();
- $this->debug($is);
- }
- /**
- * GooglLibTest::testShortenAndUnshorten()
- *
- * @return void
- */
- public function testShortenAndUnshorten() {
- // Shorten without key (publicly)
- Configure::write('Googl.key', '');
- $url = 'http://www.spiegel.de';
- $is = $this->Googl->getShort($url);
- $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['longUrl'] == $url . '/');
- // Unshorten
- $shortUrl = $is['id'];
- $is = $this->Googl->getLong($shortUrl);
- $this->assertTrue(!empty($is));
- $this->assertTrue(!empty($is['id']));
- $this->assertSame('urlshortener#url', $is['kind']);
- $this->assertSame('OK', $is['status']);
- $this->assertSame($url . '/', $is['longUrl']);
- }
- /**
- * GooglLibTest::testApi()
- *
- * @return void
- */
- public function testApi() {
- $this->skipIf(!Configure::read('Googl.key'), 'No Api Key found');
- // Shorten with key
- $url = 'http://www.blue.de';
- $is = $this->Googl->getShort($url);
- $this->debug($is);
- $res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['longUrl'] == $url . '/');
- // Unshorten
- $shortUrl = $is['id'];
- $is = $this->Googl->getLong($shortUrl);
- $this->debug($is);
- $res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['status'] === 'OK' && $is['longUrl'] == $url . '/');
- // FULL INFOS
- $url = 'http://www.web.de#123456';
- $is = $this->Googl->getShort($url);
- $this->debug($is);
- $res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['longUrl'] === 'http://www.web.de/#123456');
- $shortUrl = $is['id'];
- $is = $this->Googl->getLong($shortUrl, GooglLib::PROJECTION_CLICKS);
- $this->debug($is);
- $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');
- }
- }
- /**
- * Wrapper to mock the API calls away
- */
- class TestGooglLib extends GooglLib {
- protected $_debug = true;
- protected $_map = [
- 'http://www.spiegel.de' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/nBBg",
- "longUrl": "http://www.spiegel.de/"
- }',
- 'http://goo.gl/nBBg' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/nBBg",
- "longUrl": "http://www.spiegel.de/",
- "status": "OK"
- }',
- 'http://www.blue.de' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/leVfu4",
- "longUrl": "http://www.blue.de/"
- }',
- 'http://goo.gl/leVfu4' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/leVfu4",
- "longUrl": "http://www.blue.de/",
- "status": "OK"
- }',
- 'http://www.web.de#123456' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/7937W",
- "longUrl": "http://www.web.de/#123456"
- }',
- 'http://goo.gl/7937W' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/7937W",
- "longUrl": "http://www.web.de/#123456",
- "status": "OK",
- "analytics": {
- "allTime": {
- "shortUrlClicks": "1",
- "longUrlClicks": "1"
- },
- "month": {
- "shortUrlClicks": "0",
- "longUrlClicks": "0"
- },
- "week": {
- "shortUrlClicks": "0",
- "longUrlClicks": "0"
- },
- "day": {
- "shortUrlClicks": "0",
- "longUrlClicks": "0"
- },
- "twoHours": {
- "shortUrlClicks": "0",
- "longUrlClicks": "0"
- }
- }
- }'
- ];
- public function setLive($live = true) {
- $this->_debug = !$live;
- }
- public function getShort($url) {
- if ($this->_debug) {
- return json_decode($this->_map[$url], true);
- }
- return parent::getShort($url);
- }
- public function getLong($url, $projection = null) {
- if ($this->_debug) {
- return json_decode($this->_map[$url], true);
- }
- return parent::getLong($url, $projection);
- }
- }
|