server/src/tests/Services/LexvoResolverTest.php
changeset 28 b0b56e0f8c7f
child 405 f239c8c5bb94
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 <?php
       
     2 
       
     3 use Mockery as m;
       
     4 
       
     5 use EasyRdf\Http;
       
     6 
       
     7 class LexvoResolverTest extends TestCase
       
     8 {
       
     9     const LEXVO_EMPTY_RDF_RESP = <<<EOT
       
    10 {
       
    11   "head" : {
       
    12     "vars" : [
       
    13       "s",
       
    14       "o"
       
    15     ]
       
    16   },
       
    17   "results" : {
       
    18     "bindings" : [ ]
       
    19   }
       
    20 }
       
    21 EOT;
       
    22     const LEXVO_FRA_RDF_RESP = <<<EOT
       
    23 {
       
    24   "head" : {
       
    25     "vars" : [
       
    26       "s",
       
    27       "o"
       
    28     ]
       
    29   },
       
    30   "results" : {
       
    31     "bindings" : [
       
    32       {
       
    33         "s" : {
       
    34           "type" : "uri",
       
    35           "value" : "http://lexvo.org/id/iso639-3/fra"
       
    36         },
       
    37         "o" : {
       
    38           "xml:lang" : "en",
       
    39           "type" : "literal",
       
    40           "value" : "French"
       
    41         }
       
    42       },
       
    43       {
       
    44         "s" : {
       
    45           "type" : "uri",
       
    46           "value" : "http://lexvo.org/id/iso639-3/fra"
       
    47         },
       
    48         "o" : {
       
    49           "xml:lang" : "en",
       
    50           "type" : "literal",
       
    51           "value" : "French language"
       
    52         }
       
    53       },
       
    54       {
       
    55         "s" : {
       
    56           "type" : "uri",
       
    57           "value" : "http://lexvo.org/id/iso639-3/fra"
       
    58         },
       
    59         "o" : {
       
    60           "xml:lang" : "fr",
       
    61           "type" : "literal",
       
    62           "value" : "Français"
       
    63         }
       
    64       },
       
    65       {
       
    66         "s" : {
       
    67           "type" : "uri",
       
    68           "value" : "http://lexvo.org/id/iso639-3/fra"
       
    69         },
       
    70         "o" : {
       
    71           "xml:lang" : "fr",
       
    72           "type" : "literal",
       
    73           "value" : "français"
       
    74         }
       
    75       }
       
    76     ]
       
    77   }
       
    78 }
       
    79 EOT;
       
    80 
       
    81 const LEXVO_FRA2_RDF_RESP = <<<EOT
       
    82 {
       
    83     "head" : {
       
    84         "vars" : [
       
    85             "s",
       
    86             "o"
       
    87         ]
       
    88     },
       
    89     "results" : {
       
    90         "bindings" : [
       
    91             {
       
    92                 "s" : {
       
    93                     "type" : "uri",
       
    94                     "value" : "http://lexvo.org/id/iso639-3/fra"
       
    95                 },
       
    96                 "o" : {
       
    97                     "xml:lang" : "en",
       
    98                     "type" : "literal",
       
    99                     "value" : "French"
       
   100                 }
       
   101             },
       
   102             {
       
   103                 "s" : {
       
   104                     "type" : "uri",
       
   105                     "value" : "http://lexvo.org/id/iso639-3/fra"
       
   106                 },
       
   107                 "o" : {
       
   108                     "xml:lang" : "en",
       
   109                     "type" : "literal",
       
   110                     "value" : "French language"
       
   111                 }
       
   112             },
       
   113             {
       
   114                 "s" : {
       
   115                     "type" : "uri",
       
   116                     "value" : "http://lexvo.org/id/iso639-3/fra"
       
   117                 },
       
   118                 "o" : {
       
   119                     "xml:lang" : "fr",
       
   120                     "type" : "literal",
       
   121                     "value" : "français"
       
   122                 }
       
   123             },
       
   124             {
       
   125                 "s" : {
       
   126                     "type" : "uri",
       
   127                     "value" : "http://lexvo.org/id/iso639-3/fra"
       
   128                 },
       
   129                 "o" : {
       
   130                     "xml:lang" : "fr",
       
   131                     "type" : "literal",
       
   132                     "value" : "Français"
       
   133                 }
       
   134             }
       
   135         ]
       
   136     }
       
   137 }
       
   138 EOT;
       
   139 
       
   140     const LEXVO_FRA_AFR_RDF_RESP = <<<EOT
       
   141 {
       
   142   "head" : {
       
   143     "vars" : [
       
   144       "s",
       
   145       "o"
       
   146     ]
       
   147   },
       
   148   "results" : {
       
   149     "bindings" : [
       
   150       {
       
   151         "s" : {
       
   152           "type" : "uri",
       
   153           "value" : "http://lexvo.org/id/iso639-3/fra"
       
   154         },
       
   155         "o" : {
       
   156           "xml:lang" : "en",
       
   157           "type" : "literal",
       
   158           "value" : "French"
       
   159         }
       
   160       },
       
   161       {
       
   162         "s" : {
       
   163           "type" : "uri",
       
   164           "value" : "http://lexvo.org/id/iso639-3/fra"
       
   165         },
       
   166         "o" : {
       
   167           "xml:lang" : "en",
       
   168           "type" : "literal",
       
   169           "value" : "French language"
       
   170         }
       
   171       },
       
   172       {
       
   173         "s" : {
       
   174           "type" : "uri",
       
   175           "value" : "http://lexvo.org/id/iso639-3/fra"
       
   176         },
       
   177         "o" : {
       
   178           "xml:lang" : "fr",
       
   179           "type" : "literal",
       
   180           "value" : "Français"
       
   181         }
       
   182       },
       
   183       {
       
   184         "s" : {
       
   185           "type" : "uri",
       
   186           "value" : "http://lexvo.org/id/iso639-3/fra"
       
   187         },
       
   188         "o" : {
       
   189           "xml:lang" : "fr",
       
   190           "type" : "literal",
       
   191           "value" : "français"
       
   192         }
       
   193       },
       
   194       {
       
   195         "s" : {
       
   196           "type" : "uri",
       
   197           "value" : "http://lexvo.org/id/iso639-3/afr"
       
   198         },
       
   199         "o" : {
       
   200           "xml:lang" : "en",
       
   201           "type" : "literal",
       
   202           "value" : "Afrikaans"
       
   203         }
       
   204       },
       
   205       {
       
   206         "s" : {
       
   207           "type" : "uri",
       
   208           "value" : "http://lexvo.org/id/iso639-3/afr"
       
   209         },
       
   210         "o" : {
       
   211           "xml:lang" : "fr",
       
   212           "type" : "literal",
       
   213           "value" : "Afrikaans"
       
   214         }
       
   215       },
       
   216       {
       
   217         "s" : {
       
   218           "type" : "uri",
       
   219           "value" : "http://lexvo.org/id/iso639-3/afr"
       
   220         },
       
   221         "o" : {
       
   222           "xml:lang" : "fr",
       
   223           "type" : "literal",
       
   224           "value" : "afrikaans"
       
   225         }
       
   226       }
       
   227     ]
       
   228   }
       
   229 }
       
   230 EOT;
       
   231     const LEXVO_XAG_RDF_RESP = <<<EOT
       
   232 {
       
   233   "head" : {
       
   234     "vars" : [
       
   235       "s",
       
   236       "o"
       
   237     ]
       
   238   },
       
   239   "results" : {
       
   240     "bindings" : [
       
   241       {
       
   242         "s" : {
       
   243           "type" : "uri",
       
   244           "value" : "http://lexvo.org/id/iso639-3/xag"
       
   245         },
       
   246         "o" : {
       
   247           "xml:lang" : "en",
       
   248           "type" : "literal",
       
   249           "value" : "Aghwan"
       
   250         }
       
   251       },
       
   252       {
       
   253         "s" : {
       
   254           "type" : "uri",
       
   255           "value" : "http://lexvo.org/id/iso639-3/xag"
       
   256         },
       
   257         "o" : {
       
   258           "xml:lang" : "en",
       
   259           "type" : "literal",
       
   260           "value" : "Caucasian Albanian language"
       
   261         }
       
   262       }
       
   263     ]
       
   264   }
       
   265 }
       
   266 EOT;
       
   267 
       
   268 const LEXVO_XAG2_RDF_RESP = <<<EOT
       
   269 {
       
   270 "head" : {
       
   271 "vars" : [
       
   272   "s",
       
   273   "o"
       
   274 ]
       
   275 },
       
   276 "results" : {
       
   277 "bindings" : [
       
   278     {
       
   279       "s" : {
       
   280         "type" : "uri",
       
   281         "value" : "http://lexvo.org/id/iso639-3/xag"
       
   282       },
       
   283       "o" : {
       
   284         "xml:lang" : "en",
       
   285         "type" : "literal",
       
   286         "value" : "Caucasian Albanian language"
       
   287       }
       
   288   },
       
   289   {
       
   290     "s" : {
       
   291       "type" : "uri",
       
   292       "value" : "http://lexvo.org/id/iso639-3/xag"
       
   293     },
       
   294     "o" : {
       
   295       "xml:lang" : "en",
       
   296       "type" : "literal",
       
   297       "value" : "Aghwan"
       
   298     }
       
   299   }
       
   300 ]
       
   301 }
       
   302 }
       
   303 EOT;
       
   304 
       
   305     private function setUpSparqlClient($queryResult) {
       
   306         $response = m::mock('EasyRdf\Http\Response')
       
   307             ->shouldReceive('isSuccessful')->andReturn(true)
       
   308             ->shouldReceive('getBody')->andReturn($queryResult)
       
   309             ->shouldReceive('getStatus')->andReturn(200)
       
   310             ->shouldReceive('getHeader')->andReturn('application/sparql-results+json;charset=UTF-8')
       
   311             ->mock();
       
   312         $this->httpClient = m::mock('EasyRdf\Http\Client')
       
   313             ->shouldReceive('setConfig')
       
   314             ->shouldReceive('resetParameters')
       
   315             ->shouldReceive('setHeaders')//->with(m::on(function($headers) { print("HEADER => $headers\n"); return true;}),m::on(function($value) { print("VALUE => $value\n"); return true;}))
       
   316             ->shouldReceive('setMethod')
       
   317             ->shouldReceive('setUri')//->with(m::on(function($uri) { print($uri."\n"); return true;}))
       
   318             ->shouldReceive('request')->andReturn($response)
       
   319             ->mock();
       
   320         Http::setDefaultHttpClient($this->httpClient);
       
   321         $this->lexvoResolver = $this->app->make('CorpusParole\Services\LexvoResolverInterface');
       
   322     }
       
   323 
       
   324     public function setUp() {
       
   325         parent::setUp();
       
   326     }
       
   327 
       
   328     public function tearDown() {
       
   329         parent::tearDown();
       
   330         m::close();
       
   331     }
       
   332 
       
   333     /**
       
   334      * Just test the setup
       
   335      *
       
   336      * @return void
       
   337      */
       
   338     public function testSetUp() {
       
   339         $this->assertTrue(true);
       
   340     }
       
   341 
       
   342     /**
       
   343      * resolve french
       
   344      * @return void
       
   345      */
       
   346     public function testResolveSingleId() {
       
   347 
       
   348         $this->setUpSparqlClient(self::LEXVO_FRA_RDF_RESP);
       
   349 
       
   350         $resName = $this->lexvoResolver->getName('fra');
       
   351 
       
   352         $this->assertEquals('français', $resName, "Result must be français");
       
   353     }
       
   354 
       
   355     /**
       
   356      * resolve french
       
   357      * @return void
       
   358      */
       
   359     public function testResolveSingleIdFullURL() {
       
   360 
       
   361         $this->setUpSparqlClient(self::LEXVO_FRA_RDF_RESP);
       
   362 
       
   363         $resName = $this->lexvoResolver->getName('http://lexvo.org/id/iso639-3/fra');
       
   364 
       
   365         $this->assertEquals('français', $resName, "Result must be français");
       
   366     }
       
   367 
       
   368     /**
       
   369      * resolve foo
       
   370      * @return void
       
   371      */
       
   372     public function testResolveBadId() {
       
   373 
       
   374         $this->setUpSparqlClient(self::LEXVO_EMPTY_RDF_RESP);
       
   375 
       
   376         $resName = $this->lexvoResolver->getName('foo');
       
   377 
       
   378         $this->assertNull($resName, "Result must be null");
       
   379     }
       
   380 
       
   381     /**
       
   382      * resolve foo
       
   383      * @return void
       
   384      * @expectedException        CorpusParole\Services\LexvoResolverException
       
   385      * @expectedExceptionMessage the provided id "21dsasd;;" is not a Lexvo id
       
   386      * @expectedExceptionCode 0
       
   387      */
       
   388     public function testResolveBadFormat() {
       
   389 
       
   390         $this->setUpSparqlClient(self::LEXVO_EMPTY_RDF_RESP);
       
   391 
       
   392         $resName = $this->lexvoResolver->getName('21dsasd;;');
       
   393     }
       
   394 
       
   395     /**
       
   396      * resolve foo
       
   397      * @return void
       
   398      * @expectedException        CorpusParole\Services\LexvoResolverException
       
   399      * @expectedExceptionMessage the provided id "http://sdsasd.org/foo" is not a Lexvo id
       
   400      * @expectedExceptionCode 0
       
   401      */
       
   402     public function testResolveBadFormatFullId() {
       
   403 
       
   404         $this->setUpSparqlClient(self::LEXVO_EMPTY_RDF_RESP);
       
   405 
       
   406         $resName = $this->lexvoResolver->getName('http://sdsasd.org/foo');
       
   407     }
       
   408 
       
   409 
       
   410     /**
       
   411      * resolve french
       
   412      * @return void
       
   413      */
       
   414     public function testResolveMultipleId() {
       
   415 
       
   416         $this->setUpSparqlClient(self::LEXVO_FRA_AFR_RDF_RESP);
       
   417 
       
   418         $resnames = $this->lexvoResolver->getNames(['fra', 'afr']);
       
   419 
       
   420         $this->assertCount(2, $resnames, "Must have 2 results");
       
   421         $this->assertArrayHasKey('fra', $resnames);
       
   422         $this->assertArrayHasKey('afr', $resnames);
       
   423 
       
   424         $this->assertEquals('français', $resnames['fra'], "Result for fra must be français");
       
   425         $this->assertEquals('afrikaans', $resnames['afr'], "Result for afr must be afrikaans");
       
   426     }
       
   427 
       
   428     /**
       
   429      * resolve french
       
   430      * @return void
       
   431      */
       
   432     public function testResolveMultipleFullId() {
       
   433 
       
   434         $this->setUpSparqlClient(self::LEXVO_FRA_AFR_RDF_RESP);
       
   435 
       
   436         $resnames = $this->lexvoResolver->getNames(['http://lexvo.org/id/iso639-3/fra', 'http://lexvo.org/id/iso639-3/afr']);
       
   437 
       
   438         $this->assertCount(2, $resnames, "Must have 2 results");
       
   439         $this->assertArrayHasKey('http://lexvo.org/id/iso639-3/fra', $resnames);
       
   440         $this->assertArrayHasKey('http://lexvo.org/id/iso639-3/afr', $resnames);
       
   441 
       
   442         $this->assertEquals('français', $resnames['http://lexvo.org/id/iso639-3/fra'], "Result for fra must be français");
       
   443         $this->assertEquals('afrikaans', $resnames['http://lexvo.org/id/iso639-3/afr'], "Result for afr must be afrikaans");
       
   444     }
       
   445 
       
   446     /**
       
   447      * check query
       
   448      * @return void
       
   449      */
       
   450     public function testQuery() {
       
   451 
       
   452         $expectedUri = config('corpusparole.lexvo_sesame_query_url')."?query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0ASELECT+%3Fs+%3Fo+WHERE+%7B%7B%3Chttp%3A%2F%2Flexvo.org%2Fid%2Fiso639-3%2Ffra%3E+rdfs%3Alabel+%3Fo.+%3Fs+rdfs%3Alabel+%3Fo+FILTER%28%3Fs+%3D+%3Chttp%3A%2F%2Flexvo.org%2Fid%2Fiso639-3%2Ffra%3E+%26%26+%28lang%28%3Fo%29+%3D+%22fr%22+%7C%7C+lang%28%3Fo%29+%3D+%22en%22%29%29%7D+UNION+%7B%3Chttp%3A%2F%2Flexvo.org%2Fid%2Fiso639-3%2Fafr%3E+rdfs%3Alabel+%3Fo.+%3Fs+rdfs%3Alabel+%3Fo+FILTER%28%3Fs+%3D+%3Chttp%3A%2F%2Flexvo.org%2Fid%2Fiso639-3%2Fafr%3E+%26%26+%28lang%28%3Fo%29+%3D+%22fr%22+%7C%7C+lang%28%3Fo%29+%3D+%22en%22%29%29%7D%7D";
       
   453         $response = m::mock('EasyRdf\Http\Response')
       
   454             ->shouldReceive('isSuccessful')->andReturn(true)
       
   455             ->shouldReceive('getBody')->andReturn(self::LEXVO_EMPTY_RDF_RESP) //result not important
       
   456             ->shouldReceive('getStatus')->andReturn(200)
       
   457             ->shouldReceive('getHeader')->andReturn('application/sparql-results+json;charset=UTF-8')
       
   458             ->mock();
       
   459         $this->httpClient = m::mock('EasyRdf\Http\Client')
       
   460             ->shouldReceive('setConfig')
       
   461             ->shouldReceive('resetParameters')
       
   462             ->shouldReceive('setHeaders')//->with(m::on(function($headers) { print("HEADER => $headers\n"); return true;}),m::on(function($value) { print("VALUE => $value\n"); return true;}))
       
   463             ->shouldReceive('setMethod')
       
   464             ->shouldReceive('setUri')
       
   465             ->with($expectedUri)//->with(m::on(function($uri) { print($uri."\n"); return true;}))
       
   466             ->shouldReceive('request')->andReturn($response)
       
   467             ->mock();
       
   468         Http::setDefaultHttpClient($this->httpClient);
       
   469         $this->lexvoResolver = $this->app->make('CorpusParole\Services\LexvoResolverInterface');
       
   470 
       
   471         $resName = $this->lexvoResolver->getNames(['fra','afr']);
       
   472 
       
   473     }
       
   474 
       
   475     /**
       
   476      * check query
       
   477      * @return void
       
   478      */
       
   479     public function testQueryFullId() {
       
   480 
       
   481         $expectedUri = config('corpusparole.lexvo_sesame_query_url')."?query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0ASELECT+%3Fs+%3Fo+WHERE+%7B%7B%3Chttp%3A%2F%2Flexvo.org%2Fid%2Fiso639-3%2Ffra%3E+rdfs%3Alabel+%3Fo.+%3Fs+rdfs%3Alabel+%3Fo+FILTER%28%3Fs+%3D+%3Chttp%3A%2F%2Flexvo.org%2Fid%2Fiso639-3%2Ffra%3E+%26%26+%28lang%28%3Fo%29+%3D+%22fr%22+%7C%7C+lang%28%3Fo%29+%3D+%22en%22%29%29%7D+UNION+%7B%3Chttp%3A%2F%2Flexvo.org%2Fid%2Fiso639-3%2Fafr%3E+rdfs%3Alabel+%3Fo.+%3Fs+rdfs%3Alabel+%3Fo+FILTER%28%3Fs+%3D+%3Chttp%3A%2F%2Flexvo.org%2Fid%2Fiso639-3%2Fafr%3E+%26%26+%28lang%28%3Fo%29+%3D+%22fr%22+%7C%7C+lang%28%3Fo%29+%3D+%22en%22%29%29%7D%7D";
       
   482         $response = m::mock('EasyRdf\Http\Response')
       
   483             ->shouldReceive('isSuccessful')->andReturn(true)
       
   484             ->shouldReceive('getBody')->andReturn(self::LEXVO_EMPTY_RDF_RESP) //result not important
       
   485             ->shouldReceive('getStatus')->andReturn(200)
       
   486             ->shouldReceive('getHeader')->andReturn('application/sparql-results+json;charset=UTF-8')
       
   487             ->mock();
       
   488         $this->httpClient = m::mock('EasyRdf\Http\Client')
       
   489             ->shouldReceive('setConfig')
       
   490             ->shouldReceive('resetParameters')
       
   491             ->shouldReceive('setHeaders')//->with(m::on(function($headers) { print("HEADER => $headers\n"); return true;}),m::on(function($value) { print("VALUE => $value\n"); return true;}))
       
   492             ->shouldReceive('setMethod')
       
   493             ->shouldReceive('setUri')
       
   494             ->with($expectedUri)//->with(m::on(function($uri) { print($uri."\n"); return true;}))
       
   495             ->shouldReceive('request')->andReturn($response)
       
   496             ->mock();
       
   497         Http::setDefaultHttpClient($this->httpClient);
       
   498         $this->lexvoResolver = $this->app->make('CorpusParole\Services\LexvoResolverInterface');
       
   499 
       
   500         $resName = $this->lexvoResolver->getNames(['http://lexvo.org/id/iso639-3/fra','http://lexvo.org/id/iso639-3/afr']);
       
   501 
       
   502     }
       
   503 
       
   504     /**
       
   505      * resolve french
       
   506      * @return void
       
   507      */
       
   508     public function testResolveSingleIdNofr() {
       
   509 
       
   510         $this->setUpSparqlClient(self::LEXVO_XAG_RDF_RESP);
       
   511 
       
   512         $resName = $this->lexvoResolver->getName('xag');
       
   513 
       
   514         $this->assertEquals('aghwan', $resName, "Result must be aghwan");
       
   515     }
       
   516 
       
   517     /**
       
   518      * resolve french
       
   519      * @return void
       
   520      */
       
   521     public function testResolveSingleIdOrderNotFr() {
       
   522 
       
   523         $this->setUpSparqlClient(self::LEXVO_XAG2_RDF_RESP);
       
   524 
       
   525         $resName = $this->lexvoResolver->getName('xag');
       
   526 
       
   527         $this->assertEquals('aghwan', $resName, "Result must be aghwan");
       
   528     }
       
   529 
       
   530     /**
       
   531      * resolve french
       
   532      * @return void
       
   533      */
       
   534     public function testResolveSingleIdOrder() {
       
   535 
       
   536         $this->setUpSparqlClient(self::LEXVO_FRA2_RDF_RESP);
       
   537 
       
   538         $resName = $this->lexvoResolver->getName('fra');
       
   539 
       
   540         $this->assertEquals('français', $resName, "Result must be français");
       
   541     }
       
   542 
       
   543 }