server/src/tests/Controllers/DiscourseControllerTest.php
changeset 377 52169c718513
parent 319 78990a8a069b
child 407 2dba812c7ef2
--- a/server/src/tests/Controllers/DiscourseControllerTest.php	Thu Oct 20 15:09:31 2016 +0200
+++ b/server/src/tests/Controllers/DiscourseControllerTest.php	Thu Oct 20 17:27:36 2016 +0200
@@ -2,24 +2,28 @@
 
 use Mockery as m;
 
-use EasyRdf\Resource;
-use EasyRdf\Literal;
-
 /**
  *
  */
 class DiscourseControllerTest extends TestCase {
 
-    private $sparqlClient;
+    const ES_QUERY = [
+        'index' => 'corpus',
+        'body' => [
+            "size" => 0,
+            "query" => [ "match_all" => [] ],
+            "aggs" => [
+                "discourses" => [
+                    "terms" => [ "field" => "discourse_types", "order" => [ "_count" => "desc" ], "size" => 0 ]
+                ]
+            ]
+        ]
+    ];
 
     public function setUp() {
 
         parent::setup();
 
-        // create a mock of the post repository interface and inject it into the
-        // IoC container
-        $this->sparqlClient = m::mock('CorpusParole\Libraries\Sparql\SparqlClient');
-        $this->app->instance('CorpusParole\Libraries\Sparql\SparqlClient', $this->sparqlClient);
     }
 
     public function tearDown() {
@@ -27,39 +31,79 @@
         parent::tearDown();
     }
 
-    public function testIndexQuery() {
-
-        $query = preg_replace('/\s+/', ' ', "SELECT (?o AS ?res) (COUNT(?s) AS ?count) WHERE {
-            ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.
-            ?s <http://purl.org/dc/elements/1.1/type> ?o.
-            FILTER(uri(?o) in (<".implode('>,<', array_keys(config('corpusparole.corpus_discourse_type'))).">))
-        }
-        GROUP BY ?o
-        ORDER BY DESC(?count)");
-
-        $this->sparqlClient
-            ->shouldReceive('query')
-            ->with($query)
-            ->once()
-            ->andReturn(new \ArrayIterator([]));
-        $this->get('/api/v1/stats/discourses/');
-    }
-
     public function testIndex() {
 
-        $this->sparqlClient
-            ->shouldReceive('query')
-            ->once()
-            ->andReturn(new \ArrayIterator([
-                (object)['res'=>new Resource('http://ark.bnf.fr/ark:/12148/cb12083158d'), 'count' => Literal::create(44)],
-                (object)['res'=>new Resource('http://ark.bnf.fr/ark:/12148/cb119783362'), 'count' => Literal::create(33)],
-                (object)['res'=>new Resource('http://ark.bnf.fr/ark:/12148/cb13319048g'), 'count' => Literal::create(22)],
-            ]));
+        Es::shouldReceive('search')
+                ->once()
+                ->with(self::ES_QUERY)
+                ->andReturn(json_decode('{
+  "took" : 116,
+  "timed_out" : false,
+  "_shards" : {
+    "total" : 1,
+    "successful" : 1,
+    "failed" : 0
+  },
+  "hits" : {
+    "total" : 3373,
+    "max_score" : 0.0,
+    "hits" : [ ]
+  },
+  "aggregations" : {
+    "discourses" : {
+      "doc_count_error_upper_bound" : 0,
+      "sum_other_doc_count" : 0,
+      "buckets" : [ {
+        "key" : "http://ark.bnf.fr/ark:/12148/cb12083158d",
+        "doc_count" : 44
+      }, {
+        "key" : "http://ark.bnf.fr/ark:/12148/cb119783362",
+        "doc_count" : 33
+      }, {
+        "key" : "http://ark.bnf.fr/ark:/12148/cb13319048g",
+        "doc_count" : 22
+      } ]
+    }
+  }
+}', true));
+
         $this->get('/api/v1/stats/discourses/')->assertTrue($this->response->isOk(), $this->response->content());
+
         $this->seeJsonEquals(["discourses" => [
             "http://ark.bnf.fr/ark:/12148/cb12083158d" => ["label" => "argumentation", "count" => 44],
             "http://ark.bnf.fr/ark:/12148/cb119783362" => ["label" => "bavardage", "count" => 33],
             "http://ark.bnf.fr/ark:/12148/cb13319048g" => ["label" => "chansons", "count" => 22],
         ]]);
+
+    }
+
+    public function testIndexQuery() {
+        Es::shouldReceive('search')
+                ->once()
+                ->with(self::ES_QUERY)
+                ->andReturn(json_decode('{
+  "took" : 116,
+  "timed_out" : false,
+  "_shards" : {
+    "total" : 1,
+    "successful" : 1,
+    "failed" : 0
+  },
+  "hits" : {
+    "total" : 3373,
+    "max_score" : 0.0,
+    "hits" : [ ]
+  },
+  "aggregations" : {
+    "discourses" : {
+      "doc_count_error_upper_bound" : 0,
+      "sum_other_doc_count" : 0,
+      "buckets" : [ ]
+    }
+  }
+}', true));
+        $this->get('/api/v1/stats/discourses/')->assertTrue($this->response->isOk(), $this->response->content());
+        $this->seeJsonEquals(["discourses" => [
+        ]]);
     }
 }