server/src/tests/Services/GeonamesResolverTest.php
changeset 304 20071981ba2a
child 537 d2e6ee099125
equal deleted inserted replaced
303:2fef8007c2b2 304:20071981ba2a
       
     1 <?php
       
     2 
       
     3 use GuzzleHttp\Client;
       
     4 use GuzzleHttp\Handler\MockHandler;
       
     5 use GuzzleHttp\HandlerStack;
       
     6 use GuzzleHttp\Psr7\Response;
       
     7 use GuzzleHttp\Psr7\Request;
       
     8 use GuzzleHttp\Middleware;
       
     9 use GuzzleHttp\Exception\RequestException;
       
    10 
       
    11 
       
    12 class GeonamesResolverTest extends TestCase
       
    13 {
       
    14 
       
    15     const TEST_INPUT_DOCS = [
       
    16         '2968801' => __DIR__.'/files/GeonamesResolverTest/geonames_rdf_2968801.rdf',
       
    17         '2988507' => __DIR__.'/files/GeonamesResolverTest/geonames_rdf_2988507.rdf',
       
    18         '6255148' => __DIR__.'/files/GeonamesResolverTest/geonames_rdf_6255148.rdf',
       
    19     ];
       
    20 
       
    21     private $inputGraphes = [];
       
    22 
       
    23     private function getClient($keys, &$container) {
       
    24         $history = Middleware::history($container);
       
    25         $responses = [];
       
    26         foreach ($keys as $key) {
       
    27             if(array_key_exists($key, self::TEST_INPUT_DOCS)) {
       
    28                 array_push($responses, new Response(200, [], file_get_contents(self::TEST_INPUT_DOCS[$key])));
       
    29             }
       
    30             else {
       
    31                 array_push($responses, new Response(intval($key)));  
       
    32             }
       
    33 
       
    34         }
       
    35         $mock = new MockHandler($responses);
       
    36         $handler = HandlerStack::create($mock);
       
    37         $handler->push($history);
       
    38 
       
    39         return new Client(['handler' => $handler]);
       
    40     }
       
    41 
       
    42     public function setUp() {
       
    43         parent::setup();
       
    44 
       
    45         foreach(self::TEST_INPUT_DOCS as $key => $inputDoc) {
       
    46             $this->inputGraphes[$key] =  new EasyRdf\Graph(config('corpusparole.geonames_base_url').$key."/", file_get_contents($inputDoc));
       
    47         }
       
    48     }    
       
    49 
       
    50     /**
       
    51      * Just test the setup.
       
    52      *
       
    53      * @return void
       
    54      */
       
    55     public function testSetup()
       
    56     {
       
    57         $this->assertTrue(true);
       
    58     }
       
    59 
       
    60     /**
       
    61      * test getLabel
       
    62      */
       
    63     public function testGetLabel() {
       
    64 
       
    65         $container = [];
       
    66         $client = $this->getClient(['2968801'], $container);
       
    67 
       
    68         $resolver = $this->app->make('CorpusParole\Services\GeonamesResolver', [$client]);
       
    69         $label = $resolver->getLabel('2968801');
       
    70 
       
    71         $this->assertEquals('Villedieu-les-Poêles', $label, "Label is Villedieu-les-Poêles");
       
    72 
       
    73         $this->assertCount(1, $container);
       
    74 
       
    75         $this->assertEquals("http://sws.geonames.org/2968801/", (string)$container[0]['request']->getUri());
       
    76 
       
    77     }
       
    78 
       
    79     /**
       
    80      * test getLabel
       
    81      */
       
    82     public function testGetLabel2988507() {
       
    83 
       
    84         $container = [];
       
    85         $client = $this->getClient(['2988507'], $container);
       
    86 
       
    87         $resolver = $this->app->make('CorpusParole\Services\GeonamesResolver', [$client]);
       
    88         $label = $resolver->getLabel('2988507');
       
    89 
       
    90         $this->assertEquals('Paris', $label, "Label is Paris");
       
    91 
       
    92         $this->assertCount(1, $container);
       
    93 
       
    94         $this->assertEquals("http://sws.geonames.org/2988507/", (string)$container[0]['request']->getUri());
       
    95 
       
    96     }
       
    97 
       
    98     /**
       
    99      * test getLabel
       
   100      */
       
   101     public function testGetLabel6255148() {
       
   102 
       
   103         $container = [];
       
   104         $client = $this->getClient(['6255148'], $container);
       
   105 
       
   106         $resolver = $this->app->make('CorpusParole\Services\GeonamesResolver', [$client]);
       
   107         $label = $resolver->getLabel('6255148');
       
   108 
       
   109         $this->assertEquals('Europe', $label, "Label is Europe");
       
   110 
       
   111         $this->assertCount(1, $container);
       
   112 
       
   113         $this->assertEquals("http://sws.geonames.org/6255148/", (string)$container[0]['request']->getUri());
       
   114 
       
   115     }
       
   116 
       
   117 
       
   118     /**
       
   119      * test getLabel 404
       
   120      */
       
   121     public function testGetLabel404() {
       
   122         $container = [];
       
   123         $client = $this->getClient(['404'], $container);
       
   124 
       
   125         $resolver = $this->app->make('CorpusParole\Services\GeonamesResolver', [$client]);
       
   126 
       
   127         $name = $resolver->getLabel('12345');
       
   128 
       
   129         $this->assertNull($name);
       
   130         
       
   131         $this->assertCount(1, $container);
       
   132         $this->assertEquals("http://sws.geonames.org/12345/", (string)$container[0]['request']->getUri());
       
   133 
       
   134     }
       
   135 
       
   136     /**
       
   137      * test getLabel unknown
       
   138      */
       
   139     public function testGetLabelUnknown() {
       
   140         $container = [];
       
   141         $client = $this->getClient(['6255148'], $container);
       
   142 
       
   143         $resolver = $this->app->make('CorpusParole\Services\GeonamesResolver', [$client]);
       
   144 
       
   145         $name = $resolver->getLabel('12345');
       
   146 
       
   147         $this->assertNull($name);
       
   148         
       
   149         $this->assertCount(1, $container);
       
   150         $this->assertEquals("http://sws.geonames.org/12345/", (string)$container[0]['request']->getUri());
       
   151 
       
   152     }
       
   153 
       
   154     /**
       
   155      * test getLabels
       
   156      */
       
   157     public function testGetLabels() {
       
   158 
       
   159         $container = [];
       
   160         $client = $this->getClient(['2968801', '2988507', '6255148'], $container);
       
   161 
       
   162         $resolver = $this->app->make('CorpusParole\Services\GeonamesResolver', [$client]);
       
   163         $labels = $resolver->getLabels(['2968801', '2988507', '6255148']);
       
   164 
       
   165         $this->assertEquals(['2968801' => 'Villedieu-les-Poêles', '2988507' => 'Paris', '6255148' => 'Europe'] , $labels);
       
   166 
       
   167         $this->assertCount(3, $container);
       
   168 
       
   169         $this->assertEquals("http://sws.geonames.org/2968801/", (string)$container[0]['request']->getUri());
       
   170         $this->assertEquals("http://sws.geonames.org/2988507/", (string)$container[1]['request']->getUri());
       
   171         $this->assertEquals("http://sws.geonames.org/6255148/", (string)$container[2]['request']->getUri());
       
   172 
       
   173     }
       
   174 
       
   175     /**
       
   176      * Test exception 401
       
   177      * @expectedException        CorpusParole\Services\GeonamesResolverException
       
   178      * @expectedExceptionMessage Client error: `GET http://sws.geonames.org/12345/` resulted in a `401 Unauthorized` response:
       
   179      * @expectedExceptionCode 401
       
   180      */
       
   181     public function test401Error() {
       
   182         $container = [];
       
   183         $client = $this->getClient(['401'], $container);
       
   184 
       
   185         $resolver = $this->app->make('CorpusParole\Services\GeonamesResolver', [$client]);
       
   186 
       
   187         $name = $resolver->getLabel('12345');
       
   188     }
       
   189 
       
   190 
       
   191     /**
       
   192      * Test exception 500
       
   193      * @expectedException        CorpusParole\Services\GeonamesResolverException
       
   194      * @expectedExceptionMessage Server error: `GET http://sws.geonames.org/12345/` resulted in a `500 Internal Server Error` response:
       
   195      * @expectedExceptionCode 500
       
   196      */
       
   197     public function test500Error() {
       
   198         $container = [];
       
   199         $client = $this->getClient(['500'], $container);
       
   200 
       
   201         $resolver = $this->app->make('CorpusParole\Services\GeonamesResolver', [$client]);
       
   202 
       
   203         $name = $resolver->getLabel('12345');
       
   204     }
       
   205 
       
   206     /**
       
   207      * Test exception malformed
       
   208      * @expectedException        CorpusParole\Services\GeonamesResolverException
       
   209      * @expectedExceptionMessage GeonamesId not in correct format
       
   210      * @expectedExceptionCode 400
       
   211      */
       
   212     public function testMalformedError() {
       
   213         $container = [];
       
   214         $client = $this->getClient(['200'], $container);
       
   215 
       
   216         $resolver = $this->app->make('CorpusParole\Services\GeonamesResolver', [$client]);
       
   217 
       
   218         $name = $resolver->getLabel('abcd');
       
   219     }
       
   220 
       
   221 
       
   222 
       
   223 
       
   224 }