server/src/tests/Controllers/LanguageControllerTest.php
changeset 377 52169c718513
parent 306 3fccf43160a7
child 407 2dba812c7ef2
equal deleted inserted replaced
376:02f113d43f18 377:52169c718513
     9 /**
     9 /**
    10  *
    10  *
    11  */
    11  */
    12 class LanguageControllerTest extends TestCase {
    12 class LanguageControllerTest extends TestCase {
    13 
    13 
    14     private $sparqlClient;
    14     const ES_QUERY = [
       
    15         'index' => 'corpus',
       
    16         'body' => [
       
    17             "size" => 0,
       
    18             "query" => [ "match_all" => [] ],
       
    19             "aggs" => [
       
    20                 "languages" => [
       
    21                     "terms" => [ "field" => "language", "order" => [ "_count" => "desc" ], "size" => 0 ]
       
    22                 ]
       
    23             ]
       
    24         ]
       
    25     ];
    15 
    26 
    16     public function setUp() {
    27     public function setUp() {
    17 
       
    18         parent::setup();
    28         parent::setup();
    19 
       
    20         // create a mock of the post repository interface and inject it into the
       
    21         // IoC container
       
    22         $this->sparqlClient = m::mock('CorpusParole\Libraries\Sparql\SparqlClient');
       
    23         $this->app->instance('CorpusParole\Libraries\Sparql\SparqlClient', $this->sparqlClient);
       
    24     }
    29     }
    25 
    30 
    26     public function tearDown() {
    31     public function tearDown() {
    27         m::close();
    32         m::close();
    28         parent::tearDown();
    33         parent::tearDown();
    29     }
    34     }
    30 
    35 
    31     public function testIndex() {
    36     public function testIndex() {
    32         $query = "select ?lang (count(?lang) as ?count) where {
    37 
    33             ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.
    38         Es::shouldReceive('search')
    34             ?s <http://purl.org/dc/elements/1.1/language> ?lang
    39                 ->once()
       
    40                 ->with(self::ES_QUERY)
       
    41                 ->andReturn(json_decode('{
       
    42     "took": 92,
       
    43     "timed_out": false,
       
    44     "_shards": {
       
    45         "total": 1,
       
    46         "successful": 1,
       
    47         "failed": 0
       
    48     },
       
    49     "hits": {
       
    50         "total": 3373,
       
    51         "max_score": 0.0,
       
    52         "hits": []
       
    53     },
       
    54     "aggregations": {
       
    55         "languages": {
       
    56             "doc_count_error_upper_bound": 0,
       
    57             "sum_other_doc_count": 0,
       
    58             "buckets": [{
       
    59                 "key": "http://lexvo.org/id/iso639-3/fra",
       
    60                 "doc_count": 1669
       
    61             }, {
       
    62                 "key": "http://lexvo.org/id/iso639-3/gsw",
       
    63                 "doc_count": 851
       
    64             }, {
       
    65                 "key": "http://lexvo.org/id/iso639-3/bre",
       
    66                 "doc_count": 403
       
    67             }]
    35         }
    68         }
    36         GROUP BY ?lang
    69     }
    37         ORDER BY DESC(?count)";
    70 }', true));
    38 
    71 
    39         $this->sparqlClient
    72         $response = $this->get('/api/v1/stats/languages/')->assertTrue($this->response->isOk(), $this->response->content());
    40             ->shouldReceive('query')
    73         $this->seeJsonEquals(['languages' => [
    41             ->with($query)
    74                 'http://lexvo.org/id/iso639-3/fra' => 1669,
    42             ->once()
    75                 'http://lexvo.org/id/iso639-3/gsw' => 851,
    43             ->andReturn(new \ArrayIterator([
    76                 'http://lexvo.org/id/iso639-3/bre' => 403,
    44                 (object)['lang'=>new Resource('http://lexvo.org/id/iso639-3/gsw'), 'count' => Literal::create(44)],
       
    45                 (object)['lang'=>new Resource('http://lexvo.org/id/iso639-3/fra'), 'count' => Literal::create(33)],
       
    46                 (object)['lang'=>new Resource('http://lexvo.org/id/iso639-3/bre'), 'count' => Literal::create(22)],
       
    47             ]));
       
    48         $response = $this->get('/api/v1/stats/languages/')->
       
    49             seeJsonEquals(['languages' => [
       
    50                 'http://lexvo.org/id/iso639-3/gsw' => 44,
       
    51                 'http://lexvo.org/id/iso639-3/fra' => 33,
       
    52                 'http://lexvo.org/id/iso639-3/bre' => 22,
       
    53             ]]);
    77             ]]);
    54     }
    78     }
    55 }
    79 }