server/src/tests/Controllers/DiscourseControllerTest.php
changeset 377 52169c718513
parent 319 78990a8a069b
child 407 2dba812c7ef2
equal deleted inserted replaced
376:02f113d43f18 377:52169c718513
     1 <?php
     1 <?php
     2 
     2 
     3 use Mockery as m;
     3 use Mockery as m;
     4 
       
     5 use EasyRdf\Resource;
       
     6 use EasyRdf\Literal;
       
     7 
     4 
     8 /**
     5 /**
     9  *
     6  *
    10  */
     7  */
    11 class DiscourseControllerTest extends TestCase {
     8 class DiscourseControllerTest extends TestCase {
    12 
     9 
    13     private $sparqlClient;
    10     const ES_QUERY = [
       
    11         'index' => 'corpus',
       
    12         'body' => [
       
    13             "size" => 0,
       
    14             "query" => [ "match_all" => [] ],
       
    15             "aggs" => [
       
    16                 "discourses" => [
       
    17                     "terms" => [ "field" => "discourse_types", "order" => [ "_count" => "desc" ], "size" => 0 ]
       
    18                 ]
       
    19             ]
       
    20         ]
       
    21     ];
    14 
    22 
    15     public function setUp() {
    23     public function setUp() {
    16 
    24 
    17         parent::setup();
    25         parent::setup();
    18 
    26 
    19         // create a mock of the post repository interface and inject it into the
       
    20         // IoC container
       
    21         $this->sparqlClient = m::mock('CorpusParole\Libraries\Sparql\SparqlClient');
       
    22         $this->app->instance('CorpusParole\Libraries\Sparql\SparqlClient', $this->sparqlClient);
       
    23     }
    27     }
    24 
    28 
    25     public function tearDown() {
    29     public function tearDown() {
    26         m::close();
    30         m::close();
    27         parent::tearDown();
    31         parent::tearDown();
    28     }
    32     }
    29 
    33 
    30     public function testIndexQuery() {
       
    31 
       
    32         $query = preg_replace('/\s+/', ' ', "SELECT (?o AS ?res) (COUNT(?s) AS ?count) WHERE {
       
    33             ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.
       
    34             ?s <http://purl.org/dc/elements/1.1/type> ?o.
       
    35             FILTER(uri(?o) in (<".implode('>,<', array_keys(config('corpusparole.corpus_discourse_type'))).">))
       
    36         }
       
    37         GROUP BY ?o
       
    38         ORDER BY DESC(?count)");
       
    39 
       
    40         $this->sparqlClient
       
    41             ->shouldReceive('query')
       
    42             ->with($query)
       
    43             ->once()
       
    44             ->andReturn(new \ArrayIterator([]));
       
    45         $this->get('/api/v1/stats/discourses/');
       
    46     }
       
    47 
       
    48     public function testIndex() {
    34     public function testIndex() {
    49 
    35 
    50         $this->sparqlClient
    36         Es::shouldReceive('search')
    51             ->shouldReceive('query')
    37                 ->once()
    52             ->once()
    38                 ->with(self::ES_QUERY)
    53             ->andReturn(new \ArrayIterator([
    39                 ->andReturn(json_decode('{
    54                 (object)['res'=>new Resource('http://ark.bnf.fr/ark:/12148/cb12083158d'), 'count' => Literal::create(44)],
    40   "took" : 116,
    55                 (object)['res'=>new Resource('http://ark.bnf.fr/ark:/12148/cb119783362'), 'count' => Literal::create(33)],
    41   "timed_out" : false,
    56                 (object)['res'=>new Resource('http://ark.bnf.fr/ark:/12148/cb13319048g'), 'count' => Literal::create(22)],
    42   "_shards" : {
    57             ]));
    43     "total" : 1,
       
    44     "successful" : 1,
       
    45     "failed" : 0
       
    46   },
       
    47   "hits" : {
       
    48     "total" : 3373,
       
    49     "max_score" : 0.0,
       
    50     "hits" : [ ]
       
    51   },
       
    52   "aggregations" : {
       
    53     "discourses" : {
       
    54       "doc_count_error_upper_bound" : 0,
       
    55       "sum_other_doc_count" : 0,
       
    56       "buckets" : [ {
       
    57         "key" : "http://ark.bnf.fr/ark:/12148/cb12083158d",
       
    58         "doc_count" : 44
       
    59       }, {
       
    60         "key" : "http://ark.bnf.fr/ark:/12148/cb119783362",
       
    61         "doc_count" : 33
       
    62       }, {
       
    63         "key" : "http://ark.bnf.fr/ark:/12148/cb13319048g",
       
    64         "doc_count" : 22
       
    65       } ]
       
    66     }
       
    67   }
       
    68 }', true));
       
    69 
    58         $this->get('/api/v1/stats/discourses/')->assertTrue($this->response->isOk(), $this->response->content());
    70         $this->get('/api/v1/stats/discourses/')->assertTrue($this->response->isOk(), $this->response->content());
       
    71 
    59         $this->seeJsonEquals(["discourses" => [
    72         $this->seeJsonEquals(["discourses" => [
    60             "http://ark.bnf.fr/ark:/12148/cb12083158d" => ["label" => "argumentation", "count" => 44],
    73             "http://ark.bnf.fr/ark:/12148/cb12083158d" => ["label" => "argumentation", "count" => 44],
    61             "http://ark.bnf.fr/ark:/12148/cb119783362" => ["label" => "bavardage", "count" => 33],
    74             "http://ark.bnf.fr/ark:/12148/cb119783362" => ["label" => "bavardage", "count" => 33],
    62             "http://ark.bnf.fr/ark:/12148/cb13319048g" => ["label" => "chansons", "count" => 22],
    75             "http://ark.bnf.fr/ark:/12148/cb13319048g" => ["label" => "chansons", "count" => 22],
    63         ]]);
    76         ]]);
       
    77 
       
    78     }
       
    79 
       
    80     public function testIndexQuery() {
       
    81         Es::shouldReceive('search')
       
    82                 ->once()
       
    83                 ->with(self::ES_QUERY)
       
    84                 ->andReturn(json_decode('{
       
    85   "took" : 116,
       
    86   "timed_out" : false,
       
    87   "_shards" : {
       
    88     "total" : 1,
       
    89     "successful" : 1,
       
    90     "failed" : 0
       
    91   },
       
    92   "hits" : {
       
    93     "total" : 3373,
       
    94     "max_score" : 0.0,
       
    95     "hits" : [ ]
       
    96   },
       
    97   "aggregations" : {
       
    98     "discourses" : {
       
    99       "doc_count_error_upper_bound" : 0,
       
   100       "sum_other_doc_count" : 0,
       
   101       "buckets" : [ ]
       
   102     }
       
   103   }
       
   104 }', true));
       
   105         $this->get('/api/v1/stats/discourses/')->assertTrue($this->response->isOk(), $this->response->content());
       
   106         $this->seeJsonEquals(["discourses" => [
       
   107         ]]);
    64     }
   108     }
    65 }
   109 }