server/src/tests/Controllers/ThemeControllerTest.php
changeset 158 366509ae2f37
child 160 c77f06ff3e54
equal deleted inserted replaced
157:1f9853178fcd 158:366509ae2f37
       
     1 <?php
       
     2 
       
     3 use Mockery as m;
       
     4 
       
     5 use EasyRdf\Resource;
       
     6 use EasyRdf\Literal;
       
     7 
       
     8 /**
       
     9  *
       
    10  */
       
    11 class ThemeControllerTest extends TestCase {
       
    12 
       
    13     private $sparqlClient;
       
    14 
       
    15     public function setUp() {
       
    16 
       
    17         parent::setup();
       
    18 
       
    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     }
       
    24 
       
    25     public function tearDown() {
       
    26         m::close();
       
    27         parent::tearDown();
       
    28     }
       
    29 
       
    30     public function testIndexQuery() {
       
    31 
       
    32                 $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
       
    33                     ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
       
    34                     ?s <http://purl.org/dc/elements/1.1/subject> ?o .
       
    35                     FILTER (isIRI(?o) && regex(str(?o), '^".config('corpusparole.bnf_ark_base_url')."')) .
       
    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/themes/');
       
    46     }
       
    47 
       
    48     public function testIndexQueryBnf() {
       
    49 
       
    50                 $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
       
    51                     ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
       
    52                     ?s <http://purl.org/dc/elements/1.1/subject> ?o .
       
    53                     FILTER (isIRI(?o) && regex(str(?o), '^".config('corpusparole.bnf_ark_base_url')."')) .
       
    54                 }
       
    55                 GROUP BY ?o
       
    56                 ORDER BY DESC(?count)");
       
    57 
       
    58         $this->sparqlClient
       
    59             ->shouldReceive('query')
       
    60             ->with($query)
       
    61             ->once()
       
    62             ->andReturn(new \ArrayIterator([]));
       
    63         $this->get('/api/v1/themes/?filter=bnf');
       
    64     }
       
    65 
       
    66 
       
    67     public function testIndexQueryAll() {
       
    68 
       
    69                 $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
       
    70                     ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
       
    71                     ?s <http://purl.org/dc/elements/1.1/subject> ?o .
       
    72                 }
       
    73                 GROUP BY ?o
       
    74                 ORDER BY DESC(?count)");
       
    75 
       
    76         $this->sparqlClient
       
    77             ->shouldReceive('query')
       
    78             ->with($query)
       
    79             ->once()
       
    80             ->andReturn(new \ArrayIterator([]));
       
    81         $this->get('/api/v1/themes/?filter=all');
       
    82     }
       
    83 
       
    84 
       
    85         public function testIndexQueryNone() {
       
    86 
       
    87                 $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
       
    88                     ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
       
    89                     ?s <http://purl.org/dc/elements/1.1/subject> ?o .
       
    90                 }
       
    91                 GROUP BY ?o
       
    92                 ORDER BY DESC(?count)");
       
    93 
       
    94         $this->sparqlClient
       
    95             ->shouldReceive('query')
       
    96             ->with($query)
       
    97             ->once()
       
    98             ->andReturn(new \ArrayIterator([]));
       
    99         $this->get('/api/v1/themes/?filter=none');
       
   100     }
       
   101 
       
   102 
       
   103     public function testIndexQueryEmpty() {
       
   104 
       
   105         $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
       
   106                 ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
       
   107                 ?s <http://purl.org/dc/elements/1.1/subject> ?o .
       
   108             }
       
   109             GROUP BY ?o
       
   110             ORDER BY DESC(?count)");
       
   111 
       
   112         $this->sparqlClient
       
   113             ->shouldReceive('query')
       
   114             ->with($query)
       
   115             ->once()
       
   116             ->andReturn(new \ArrayIterator([]));
       
   117         $this->get('/api/v1/themes/?filter=');
       
   118     }
       
   119 
       
   120     public function testIndexQueryUri() {
       
   121 
       
   122         $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
       
   123                 ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
       
   124                 ?s <http://purl.org/dc/elements/1.1/subject> ?o .
       
   125                 FILTER isIRI(?o) .
       
   126             }
       
   127             GROUP BY ?o
       
   128             ORDER BY DESC(?count)");
       
   129 
       
   130         $this->sparqlClient
       
   131             ->shouldReceive('query')
       
   132             ->with($query)
       
   133             ->once()
       
   134             ->andReturn(new \ArrayIterator([]));
       
   135         $this->get('/api/v1/themes/?filter=uri');
       
   136     }
       
   137 
       
   138 
       
   139     public function testIndex() {
       
   140 
       
   141         $this->sparqlClient
       
   142             ->shouldReceive('query')
       
   143             ->once()
       
   144             ->andReturn(new \ArrayIterator([
       
   145                 (object)['theme'=>new Resource('http://lexvo.org/id/iso639-3/gsw'), 'count' => Literal::create(44)],
       
   146                 (object)['theme'=>new Resource('http://ark.bnf.fr/ark:/12148/cb119339867'), 'count' => Literal::create(33)],
       
   147                 (object)['theme'=>Literal::create('Français', 'fr'), 'count' => Literal::create(22)],
       
   148             ]));
       
   149         $this->get('/api/v1/themes/')->assertTrue($this->response->isOk(), $this->response->content());
       
   150         $this->seeJsonEquals(["themes" => [
       
   151             "http://lexvo.org/id/iso639-3/gsw" => ["url" => "http://lexvo.org/id/iso639-3/gsw", "label" => null, "count" => 44],
       
   152             "http://ark.bnf.fr/ark:/12148/cb119339867" => ["url" => "http://ark.bnf.fr/ark:/12148/cb119339867", "label" => null, "count" => 33],
       
   153             "Français" => ["url" => null, "label" => "Français", "count" => 22],
       
   154         ]]);
       
   155     }
       
   156 }