Browse Source

[1.11] Promoted Zend_Service_ShortUrl to trunk

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23112 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 15 years ago
parent
commit
c528581188

+ 5 - 0
documentation/manual/en/manual.xml.in

@@ -1902,6 +1902,11 @@
                     <xi:include href="../en/module_specs/Zend_Service-ReCaptcha.xml" />
                 </xi:fallback>
             </xi:include>
+            <xi:include href="module_specs/Zend_Service_ShortUrl.xml">
+                <xi:fallback>
+                    <xi:include href="../en/module_specs/Zend_Service_ShortUrl.xml" />
+                </xi:fallback>
+            </xi:include>
             <xi:include href="module_specs/Zend_Service_Simpy.xml">
                 <xi:fallback>
                     <xi:include href="../en/module_specs/Zend_Service_Simpy.xml" />

+ 147 - 0
documentation/manual/en/module_specs/Zend_Service_ShortUrl.xml

@@ -0,0 +1,147 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Reviewed: no -->
+<sect1 id="zend.service.short-url">
+    <title>Zend_Service_ShortUrl</title>
+
+    <sect2 id="zend.service.short-url.intro">
+        <title>Overview</title>
+
+        <para>
+            URL shorteners have exploded in popularity in the last several years, in large part due
+            to the social nature of the web and the desire to share links.
+            <classname>Zend_Service_ShortUrl</classname> provides an API for accessing a number of
+            different URL shortener services, with the ability to both create short URLs as well as
+            retrieve the original URL.
+        </para>
+
+        <para>
+            Adapters provided include:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>
+                    <classname>Zend_Service_ShortUrl_JdemCz</classname>, which accesses the <ulink
+                        url="http://jdem.cz/">jdem.cz</ulink> service.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <classname>Zend_Service_ShortUrl_TinyUrlCom</classname>, which accesses the <ulink
+                        url="http://tinyurl.com">tinyurl.com</ulink> service.
+                </para>
+            </listitem>
+        </itemizedlist>
+    </sect2>
+
+    <sect2 id="zend.service.short-url.quick-start">
+        <title>Quick Start</title>
+
+        <para>
+            Using one of the URL shortener services is straightforward. Each URL shortener follows a
+            simple interface that defines two methods: <methodname>shorten()</methodname> and
+            <methodname>unshorten()</methodname>. Instantiate the class, and call the appropriate
+            method.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$tinyurl = new Zend_Service_ShortUrl_TinyUrlCom();
+
+// Shorten a URL:
+$short = $tinyurl->shorten('http://framework.zend.com/'); // http://tinyurl.com/rxtuq
+
+// Inflate or unshorten a short URL:
+$long  = $tinyurl->unshorten('http://tinyurl.com/rxtuq'); // http://framework.zend.com/
+]]></programlisting>
+    </sect2>
+
+    <sect2 id="zend.service.short-url.methods">
+        <title>Available Methods</title>
+
+        <variablelist>
+            <varlistentry id="zend.service.short-url.methods.shorten">
+                <term>
+                    <methodsynopsis>
+                        <methodname>shorten</methodname>
+                        <methodparam>
+                            <funcparams>$url</funcparams>
+                        </methodparam>
+                    </methodsynopsis>
+                </term>
+
+                <listitem>
+                    <para>
+                        Takes the given <varname>$url</varname> and passes it to the service in
+                        order to obtain a shortened URL.
+                    </para>
+
+                    <para>
+                        If the provided <varname>$url</varname> is invalid, an exception will be
+                        raised.
+                    </para>
+                </listitem>
+            </varlistentry>
+
+            <varlistentry id="zend.service.short-url.methods.unshorten">
+                <term>
+                    <methodsynopsis>
+                        <methodname>unshorten</methodname>
+                        <methodparam>
+                            <funcparams>$shortenedUrl</funcparams>
+                        </methodparam>
+                    </methodsynopsis>
+                </term>
+
+                <listitem>
+                    <para>
+                        Takes the provided <varname>$shortenedUrl</varname> and passes it to the
+                        service in order to obtain the original URL.
+                    </para>
+
+                    <para>
+                        If the provided <varname>$shortenedUrl</varname> is invalid, an exception
+                        will be raised.
+                    </para>
+                </listitem>
+            </varlistentry>
+
+            <varlistentry id="zend.service.short-url.methods.set-http-client">
+                <term>
+                    <methodsynopsis>
+                        <methodname>setHttpClient</methodname>
+                        <methodparam>
+                            <funcparams>Zend_Http_Client $httpClient</funcparams>
+                        </methodparam>
+                    </methodsynopsis>
+                </term>
+
+                <listitem>
+                    <para>
+                        Use this method to set the HTTP client used for communicating with the
+                        service.
+                    </para>
+                </listitem>
+            </varlistentry>
+
+            <varlistentry id="zend.service.short-url.methods.get-http-client">
+                <term>
+                    <methodsynopsis>
+                        <methodname>getHttpClient</methodname>
+                        <methodparam>
+                            <funcparams></funcparams>
+                        </methodparam>
+                    </methodsynopsis>
+                </term>
+
+                <listitem>
+                    <para>
+                        Use this method to access the HTTP client attached to the service. By
+                        default, this will lazy-load an instance of
+                        <classname>Zend_Http_Client</classname> if no client is yet attached.
+                    </para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </sect2>
+</sect1>

+ 83 - 0
library/Zend/Service/ShortUrl/AbstractShortener.php

@@ -0,0 +1,83 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Service_Abstract
+ */
+require_once 'Zend/Service/Abstract.php';
+
+/**
+ * @see Zend_Service_ShortUrl_Shortener
+ */
+require_once 'Zend/Service/ShortUrl/Shortener.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+abstract class Zend_Service_ShortUrl_AbstractShortener
+    extends Zend_Service_Abstract 
+    implements Zend_Service_ShortUrl_Shortener
+{
+    /**
+     * Base URI of the service
+     *
+     * @var string
+     */
+    protected $_baseUri = null;
+
+    
+    /**
+     * Checks whether URL to be shortened is valid
+     *
+     * @param string $url
+     * @throws Zend_Service_ShortUrl_Exception When URL is not valid
+     */
+    protected function _validateUri($url)
+    {
+        require_once 'Zend/Uri.php';
+        if (!Zend_Uri::check($url)) {
+            require_once 'Zend/Service/ShortUrl/Exception.php';
+            throw new Zend_Service_ShortUrl_Exception(sprintf(
+                'The url "%s" is not valid and cannot be shortened', $url
+            ));
+        }
+    }
+    
+    /**
+     * Verifies that the URL has been shortened by this service
+     *
+     * @throws Zend_Service_ShortUrl_Exception If the URL hasn't been shortened by this service
+     * @param string $shortenedUrl
+     */
+    protected function _verifyBaseUri($shortenedUrl)
+    {
+        if (strpos($shortenedUrl, $this->_baseUri) !== 0) {
+            require_once 'Zend/Service/ShortUrl/Exception.php';
+            throw new Zend_Service_ShortUrl_Exception(sprintf(
+                'The url "%s" is not valid for this service and the target cannot be resolved',
+                $shortenedUrl
+            ));
+        }
+    }
+}

+ 32 - 0
library/Zend/Service/ShortUrl/Exception.php

@@ -0,0 +1,32 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+require_once 'Zend/Service/Exception.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Service_ShortUrl_Exception extends Zend_Service_Exception
+{
+}

+ 84 - 0
library/Zend/Service/ShortUrl/JdemCz.php

@@ -0,0 +1,84 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Service_ShortUrl_AbstractShortener
+ */
+require_once 'Zend/Service/ShortUrl/AbstractShortener.php';
+
+/**
+ * Jdem.cz API implementation
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Service_ShortUrl_JdemCz extends Zend_Service_ShortUrl_AbstractShortener
+{
+    /**
+     * Base URI of the service
+     *
+     * @var string
+     */
+    protected $_baseUri = 'http://jdem.cz';
+    
+    /**
+     * This function shortens long url
+     *
+     * @param string $url URL to Shorten
+     * @throws Zend_Service_ShortUrl_Exception When URL is not valid
+     * @return string New URL
+     */
+    public function shorten($url)
+    {
+        $this->_validateUri($url);
+        
+        $serviceUri = 'http://www.jdem.cz/get';
+        
+        $this->getHttpClient()->setUri($serviceUri);
+        $this->getHttpClient()->setParameterGet('url', $url);
+        
+        $response = $this->getHttpClient()->request();
+        
+        return $response->getBody();
+    }
+
+   /**
+     * Reveals target for short URL
+     *
+     * @param string $shortenedUrl URL to reveal target of
+     * @throws Zend_Service_ShortUrl_Exception When URL is not valid or is not shortened by this service
+     * @return string
+     */
+    public function unshorten($shortenedUrl)
+    {
+        $this->_validateUri($shortenedUrl);
+
+        $this->_verifyBaseUri($shortenedUrl);
+        
+        $this->getHttpClient()->setUri($shortenedUrl)->setParameterGet('kam', 1);
+        
+        $response = $this->getHttpClient()->request();
+        
+        return $response->getBody();
+    }
+}

+ 45 - 0
library/Zend/Service/ShortUrl/Shortener.php

@@ -0,0 +1,45 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+interface Zend_Service_ShortUrl_Shortener
+{
+    /**
+     * This function shortens long url
+     * 
+     * @param  string $url URL to Shorten
+     * @return string Shortened Url
+     */
+    function shorten($shortenedUrl);
+    
+    /**
+     * Reveals target for short URL
+     *
+     * @param  string $shortenedUrl URL to reveal target of
+     * @return string Unshortened Url
+     */
+    function unshorten($shortenedUrl);
+}

+ 100 - 0
library/Zend/Service/ShortUrl/TinyUrlCom.php

@@ -0,0 +1,100 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Service_ShortUrl_AbstractShortener
+ */
+require_once 'Zend/Service/ShortUrl/AbstractShortener.php';
+
+/**
+ * TinyUrl.com API implementation
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Service_ShortUrl_TinyUrlCom extends Zend_Service_ShortUrl_AbstractShortener
+{
+    /**
+     * Base URI of the service
+     *
+     * @var string
+     */
+    protected $_baseUri = 'http://tinyurl.com';
+    
+    /**
+     * This function shortens long url
+     *
+     * @param string $url URL to Shorten
+     * @throws Zend_Service_ShortUrl_Exception When URL is not valid
+     * @return string New URL
+     */
+    public function shorten($url)
+    {
+        $this->_validateUri($url);
+
+        $serviceUri = 'http://tinyurl.com/api-create.php';
+        
+        $this->getHttpClient()->setUri($serviceUri);
+        $this->getHttpClient()->setParameterGet('url', $url);
+        
+        $response = $this->getHttpClient()->request();
+        
+        return $response->getBody();
+    }
+
+    /**
+     * Reveals target for short URL
+     *
+     * @param string $shortenedUrl URL to reveal target of
+     * @throws Zend_Service_ShortUrl_Exception When URL is not valid or is not shortened by this service
+     * @return string
+     */
+    public function unshorten($shortenedUrl)
+    {
+        $this->_validateUri($shortenedUrl);
+        
+        $this->_verifyBaseUri($shortenedUrl);
+        
+        //TinyUrl.com does not have an API for that, but we can use preview feature
+        //we need new Zend_Http_Client
+        $this->setHttpClient(new Zend_Http_Client());
+        
+        $this->getHttpClient()
+             ->setCookie('preview', 1)
+             ->setUri($shortenedUrl);
+
+        //get response
+        $response = $this->getHttpClient()->request();
+        
+        require_once 'Zend/Dom/Query.php';
+        $dom = new Zend_Dom_Query($response->getBody());
+        
+        //find the redirect url link
+        $results = $dom->query('a#redirecturl');
+        
+        //get href
+        $originalUrl = $results->current()->getAttribute('href');
+        
+        return $originalUrl;
+    }
+}

+ 2 - 0
tests/Zend/Service/AllTests.php

@@ -35,6 +35,7 @@ require_once 'Zend/Service/Flickr/AllTests.php';
 require_once 'Zend/Service/LiveDocx/AllTests.php';
 require_once 'Zend/Service/Nirvanix/AllTests.php';
 require_once 'Zend/Service/ReCaptcha/AllTests.php';
+require_once 'Zend/Service/ShortUrl/AllTests.php';
 require_once 'Zend/Service/Simpy/AllTests.php';
 require_once 'Zend/Service/SlideShareTest.php';
 require_once 'Zend/Service/StrikeIron/AllTests.php';
@@ -81,6 +82,7 @@ class Zend_Service_AllTests
         $suite->addTest(Zend_Service_LiveDocx_AllTests::suite());
         $suite->addTest(Zend_Service_Nirvanix_AllTests::suite());
         $suite->addTest(Zend_Service_ReCaptcha_AllTests::suite());
+        $suite->addTest(Zend_Service_ShortUrl_AllTests::suite());
         $suite->addTest(Zend_Service_Simpy_AllTests::suite());
         $suite->addTestSuite('Zend_Service_SlideShareTest');
         $suite->addTest(Zend_Service_StrikeIron_AllTests::suite());

+ 70 - 0
tests/Zend/Service/ShortUrl/AllTests.php

@@ -0,0 +1,70 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Service
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Service_ShortUrl_AllTests::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * @see Zend_Service_ShortUrl_JdemCzTest
+ */
+require_once 'Zend/Service/ShortUrl/JdemCzTest.php';
+
+/**
+ * @see Zend_Service_ShortUrl_TinyUrlComTest
+ */
+require_once 'Zend/Service/ShortUrl/TinyUrlComTest.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @group      Zend_Service
+ */
+class Zend_Service_ShortUrl_AllTests
+{
+    public static function main()
+    {
+        PHPUnit_TextUI_TestRunner::run(self::suite());
+    }
+
+    public static function suite()
+    {
+        $suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend_Service_ShortUrl');
+
+        $suite->addTestSuite('Zend_Service_ShortUrl_JdemCzTest');
+        $suite->addTestSuite('Zend_Service_ShortUrl_TinyUrlComTest');
+        
+        return $suite;
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Service_ShortUrl_AllTests::main') {
+    Zend_Service_ShortUrl_AllTests::main();
+}

+ 106 - 0
tests/Zend/Service/ShortUrl/JdemCzTest.php

@@ -0,0 +1,106 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Service
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/** Zend_Service_ShortUrl_JdemCz */
+require_once 'Zend/Service/ShortUrl/JdemCz.php';
+
+/**
+ * @package  Zend_Service
+ * @subpackage  UnitTests
+ */
+class Zend_Service_ShortUrl_JdemCzTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Zend_Service_JdemCz object
+     *
+     * @var Zend_Service_JdemCz
+     */
+    protected $_s;
+    
+    /**
+     * Creates a new Zend_Service_JdemCz object for each test method
+     *
+     * @return void
+     */
+    public function setUp ()
+    {
+        $this->_s = new Zend_Service_ShortUrl_JdemCz();
+    }
+
+    public function testShortenEmptyUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->shorten('');
+    }
+    
+    public function testShortenIncorrectUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->shorten('wrongAdress.cccc');
+    }
+    
+    public function testShorten()
+    {
+        $urls = array(
+            'http://framework.zend.com/'           => 'http://jdem.cz/ab2l1',
+            'http://framework.zend.com/manual/en/' => 'http://jdem.cz/ab3z7'
+        );
+        
+        foreach ($urls as $url => $shortenedUrl) {
+            $this->assertEquals($shortenedUrl, $this->_s->shorten($url));
+        }
+    }
+    
+    public function testUnshorten()
+    {
+        $urls = array(
+            'http://framework.zend.com/'           => 'http://jdem.cz/ab2l1',
+            'http://framework.zend.com/manual/en/' => 'http://jdem.cz/ab3z7'
+        );
+        
+        foreach ($urls as $url => $shortenedUrl) {
+            $this->assertEquals($url, $this->_s->unshorten($shortenedUrl));
+        }
+    }
+    
+    public function testUnshortenEmptyUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('');
+    }
+    
+    public function testUnshortenIncorrectUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('wrongAdress.cccc');
+    }
+    
+    public function testUnshortenWrongUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('http://www.zend.com');
+    }
+}

+ 106 - 0
tests/Zend/Service/ShortUrl/TinyUrlComTest.php

@@ -0,0 +1,106 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Service
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/** Zend_Service_TinyUrlCom */
+require_once 'Zend/Service/ShortUrl/TinyUrlCom.php';
+
+/**
+ * @package  Zend_Service
+ * @subpackage  UnitTests
+ */
+class Zend_Service_ShortUrl_TinyUrlComTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Zend_Service_TinyUrlCom object
+     *
+     * @var Zend_Service_TinyUrlCom
+     */
+    protected $_s;
+
+    /**
+     * Creates a new Zend_Service_TinyUrlCom object for each test method
+     *
+     * @return void
+     */
+    public function setUp ()
+    {
+        $this->_s = new Zend_Service_ShortUrl_TinyUrlCom();
+    }
+
+    public function testShortenEmptyUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->shorten('');
+    }
+    
+    public function testShortenIncorrectUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->shorten('wrongAdress.cccc');
+    }
+    
+    public function testShorten()
+    {
+        $urls = array(
+            'http://framework.zend.com/'           => 'http://tinyurl.com/rxtuq',
+            'http://framework.zend.com/manual/en/' => 'http://tinyurl.com/ynvdzf'
+        );
+        
+        foreach ($urls as $url => $shortenedUrl) {
+            $this->assertEquals($shortenedUrl, $this->_s->shorten($url));
+        }
+    }
+    
+    public function testUnshorten()
+    {
+        $urls = array(
+            'http://framework.zend.com/'           => 'http://tinyurl.com/rxtuq',
+            'http://framework.zend.com/manual/en/' => 'http://tinyurl.com/ynvdzf'
+        );
+        
+        foreach ($urls as $url => $shortenedUrl) {
+            $this->assertEquals($url, $this->_s->unshorten($shortenedUrl));
+        }
+    }
+    
+    public function testUnshortenEmptyUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('');
+    }
+    
+    public function testUnshortenIncorrectUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('wrongAdress.cccc');
+    }
+    
+    public function testUnshortenWrongUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('http://zend.com');
+    }
+}