Services_Yahoo
[ class tree: Services_Yahoo ] [ index: Services_Yahoo ] [ all elements ]

Source for file Search.php

Documentation is available at Search.php

  1. <?php
  2. /**
  3.  * Services_Yahoo Search Unit Tests
  4.  *
  5.  * Copyright 2005-2006 Martin Jansen
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  *
  19.  * @category   Services
  20.  * @package    Services_Yahoo
  21.  * @author     Martin Jansen <mj@php.net>
  22.  * @copyright  2005-2006 Martin Jansen
  23.  * @license    http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
  24.  * @version    CVS: $Id: Search.php,v 1.6 2006/10/04 16:16:53 mj Exp $
  25.  * @link       http://pear.php.net/package/Services_Yahoo
  26.  */
  27.  
  28. require_once "PHPUnit/Framework/TestCase.php";
  29. require_once "Services/Yahoo/Search.php";
  30.  
  31. /**
  32.  * @category   Services
  33.  * @package    Services_Yahoo
  34.  * @author     Martin Jansen <mj@php.net>
  35.  * @copyright  2005-2006 Martin Jansen
  36.  * @license    http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
  37.  * @version    CVS: $Id: Search.php,v 1.6 2006/10/04 16:16:53 mj Exp $
  38.  */
  39. class Services_Yahoo_Tests_Search extends PHPUnit_Framework_TestCase {
  40.  
  41.     public function testFactory({
  42.         $client Services_Yahoo_Search::factory("image");
  43.         $this->assertType("Services_Yahoo_Search_image"$client);
  44.  
  45.         $client Services_Yahoo_Search::factory("local");
  46.         $this->assertType("Services_Yahoo_Search_local"$client);
  47.  
  48.         $client Services_Yahoo_Search::factory("news");
  49.         $this->assertType("Services_Yahoo_Search_news"$client);
  50.  
  51.         $client Services_Yahoo_Search::factory("video");
  52.         $this->assertType("Services_Yahoo_Search_video"$client);
  53.  
  54.         $client Services_Yahoo_Search::factory("web");
  55.         $this->assertType("Services_Yahoo_Search_web"$client);
  56.     }
  57.  
  58.     public function testFactoryFailing({
  59.         try {
  60.             $client Services_Yahoo_Search::factory("doesnotexist");
  61.         catch (Services_Yahoo_Exception $e{
  62.             $this->assertType("PEAR_Exception"$e);
  63.             $this->assertEquals("Unknown search type doesnotexist"$e->getMessage());
  64.             return;
  65.         }
  66.  
  67.         $this->fail("An expected Services_Yahoo_Exception has not been raised");
  68.     }
  69.  
  70.     public function testRequestFailing({
  71.         try {
  72.             $client Services_Yahoo_Search::factory("web");
  73.             $client->searchFor("");
  74.         catch (Services_Yahoo_Exception $e{
  75.             $this->assertEquals("Search query failed"$e->getMessage());
  76.             return;
  77.         }
  78.  
  79.         $this->fail("An expected Services_Yahoo_Exception has not been raised");
  80.     }
  81.  
  82.     public function testWithResults10({
  83.         $client Services_Yahoo_Search::factory("web");
  84.  
  85.         $result $client->searchFor("Madonna");
  86.  
  87.         $this->assertEquals(10$result->getTotalResultsReturned());
  88.     }
  89.  
  90.     public function testWithResults20({
  91.         $client Services_Yahoo_Search::factory("web");
  92.  
  93.         $result $client->withResults(20)->searchFor("Madonna");
  94.  
  95.         $this->assertEquals(20$result->getTotalResultsReturned());
  96.     }
  97.  
  98.     public function testWithResultsBoundaries({
  99.         $client Services_Yahoo_Search::factory("web");
  100.  
  101.         $result $client->withResults(-1)->searchFor("Madonna");
  102.         $this->assertEquals(10$result->getTotalResultsReturned());
  103.  
  104.         $result $client->withResults(51)->searchFor("Madonna");
  105.         $this->assertEquals(10$result->getTotalResultsReturned());
  106.     }
  107.  
  108.     public function testResultFormat({
  109.         $client Services_Yahoo_Search::factory("web");
  110.  
  111.         $results $client->searchFor("Madonna");
  112.  
  113.         foreach ($results as $key => $value{
  114.             $this->assertType("int"$key);
  115.             $this->assertType("array"$value);
  116.         }
  117.     }
  118.  
  119.     public function testHasMore({
  120.         $client Services_Yahoo_Search::factory("web");
  121.  
  122.         $results $client->searchFor("Madonna");
  123.  
  124.         $this->assertTrue($results->hasMore());
  125.     }
  126.  
  127.     public function testStartingAt({
  128.         $client Services_Yahoo_Search::factory("web");
  129.  
  130.         $results $client->startingAt(87)->searchFor("Madonna");
  131.  
  132.         $this->assertEquals(87$results->getFirstResultPosition());
  133.         $this->assertEquals(10$results->getTotalResultsReturned());
  134.     }
  135.  
  136.     public function testInFormat({
  137.         $client Services_Yahoo_Search::factory("web");
  138.  
  139.         $results $client->inFormat("html")->searchFor("Madonna");
  140.  
  141.         foreach ($results as $result{
  142.             $this->assertEquals("text/html"$result['MimeType']);
  143.         }
  144.     }
  145.  
  146.     public function testOnSite({
  147.         $client Services_Yahoo_Search::factory("web");
  148.  
  149.         $results $client->onSite("pear.php.net")->searchFor("Documentation");
  150.  
  151.         foreach ($results as $result{
  152.             $this->assertRegExp("~^http://pear\.php\.net~"$result['Url']);
  153.             $this->assertRegExp("~^pear\.php\.net~"$result['DisplayUrl']);
  154.         }
  155.     }
  156.  
  157.     public function testOnSites({
  158.         $client Services_Yahoo_Search::factory("web");
  159.  
  160.         $results $client->onSite("pear.php.net")->onSite("pecl.php.net")->searchFor("PHP");
  161.  
  162.         foreach ($results as $result{
  163.             $this->assertThat($result['Url']$this->logicalOr($this->matchesRegularExpression("~^http://pear\.php\.net~"),
  164.                                                                $this->matchesRegularExpression("~^http://pecl\.php\.net~")));
  165.  
  166.             $this->assertThat($result['DisplayUrl']$this->logicalOr($this->matchesRegularExpression("~^pear\.php\.net~"),
  167.                                                                       $this->matchesRegularExpression("~^pecl\.php\.net~")));
  168.         }
  169.     }
  170.  
  171.     public function testNewsSortedBy({
  172.         $client Services_Yahoo_Search::factory("news");
  173.  
  174.         $results $client->sortedBy("date")->searchFor("Madonna");
  175.  
  176.         $lastDate = 0;
  177.         foreach ($results as $result{
  178.             if ($lastDate == 0{
  179.                 $lastDate $result['PublishDate'];
  180.             }
  181.  
  182.             $this->assertTrue($result['PublishDate'<= $lastDate);
  183.             $lastDate $result['PublishDate'];
  184.         }
  185.     }
  186.  
  187.     public function testNewsInLanguage({
  188.         $client Services_Yahoo_Search::factory("news");
  189.  
  190.         $results $client->inLanguage("sv")->searchFor("Madonna");
  191.  
  192.         foreach ($results as $result{
  193.             $this->assertEquals("sv"$result['Language']);
  194.         }
  195.     }
  196.  
  197.     public function testLocalAtLocation({
  198.         $client Services_Yahoo_Search::factory("local");
  199.  
  200.         $results $client->atLocation("San Francisco, CA")->searchFor("pizza");
  201.  
  202.         foreach ($results as $result{
  203.             $this->assertEquals("San Francisco"$result['City']);
  204.             $this->assertEquals("CA"$result['State']);
  205.         }
  206.     }
  207.  
  208.     public function testLocalInStreet({
  209.         $client Services_Yahoo_Search::factory("local");
  210.  
  211.         $results $client->inCity("Los Angeles")->inStreet("Hollywood Blvd")->searchFor("pizza");
  212.  
  213.         foreach ($results as $result{
  214.             $this->assertEquals("Los Angeles"$result['City']);
  215.             $this->assertEquals("CA"$result['State']);
  216.         }
  217.     }
  218.  
  219.     public function testLocalInState1({
  220.         $client Services_Yahoo_Search::factory("local");
  221.         $results $client->inState("OR")->inCity("Portland")->searchFor("pizza");
  222.  
  223.         foreach ($results as $result{
  224.             $this->assertEquals("OR"$result['State']);
  225.             $this->assertEquals("Portland"$result['City']);
  226.         }
  227.     }
  228.  
  229.     public function testLocalInState2({
  230.         $client Services_Yahoo_Search::factory("local");
  231.         $results $client->inState("Oregon")->inCity("Portland")->searchFor("pizza");
  232.  
  233.         foreach ($results as $result{
  234.             $this->assertEquals("OR"$result['State']);
  235.             $this->assertEquals("Portland"$result['City']);
  236.         }
  237.     }
  238.  
  239.     public function testLocalWithZip({
  240.         $client Services_Yahoo_Search::factory("local");
  241.         $results $client->withZip("90028")->searchFor("pizza");
  242.  
  243.         foreach ($results as $result{
  244.             $this->assertEquals("Los Angeles"$result['City']);
  245.         }
  246.     }
  247. }

Documentation generated on Fri, 20 Apr 2007 14:30:08 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.