server/src/tests/Controllers/DateStatsControllerTest.php
changeset 307 07b44a378ad8
child 375 145561ff51ff
equal deleted inserted replaced
306:3fccf43160a7 307:07b44a378ad8
       
     1 <?php
       
     2 
       
     3 use Mockery as m;
       
     4 
       
     5 use EasyRdf\Literal;
       
     6 
       
     7 class DateStatsControllerTest extends TestCase
       
     8 {
       
     9     private $sparqlClient;
       
    10 
       
    11     public function setUp() {
       
    12 
       
    13         parent::setup();
       
    14 
       
    15         // create a mock of the post repository interface and inject it into the
       
    16         // IoC container
       
    17         $this->sparqlClient = m::mock('CorpusParole\Libraries\Sparql\SparqlClient');
       
    18         $this->app->instance('CorpusParole\Libraries\Sparql\SparqlClient', $this->sparqlClient);
       
    19     }
       
    20 
       
    21     public function tearDown() {
       
    22         m::close();
       
    23         parent::tearDown();
       
    24     }
       
    25 
       
    26 
       
    27     public function testIndexQuery() {
       
    28 
       
    29         $query =  preg_replace('/\s+/', ' ', "SELECT (?d as ?date) (COUNT(?d) AS ?count)
       
    30             WHERE {
       
    31                 ?_ a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.
       
    32                 ?_ <http://purl.org/dc/terms/created> ?d
       
    33             }
       
    34             GROUP BY ?d
       
    35             ORDER BY ?d");
       
    36 
       
    37 
       
    38         $this->sparqlClient
       
    39             ->shouldReceive('query')
       
    40             ->with($query)
       
    41             ->once()
       
    42             ->andReturn(new \ArrayIterator([]));
       
    43         $this->get('/api/v1/stats/datestats/');
       
    44         $this->seeJsonEquals(["datestats" => []]);
       
    45     }
       
    46 
       
    47     public function testIndexMultiple() {
       
    48 
       
    49          $this->sparqlClient
       
    50              ->shouldReceive('query')
       
    51              ->once()
       
    52              ->andReturn(new \ArrayIterator([
       
    53                  (object)['date'=>new Literal('1975', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)],
       
    54                  (object)['date'=>new Literal('1965', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(2)],
       
    55                  (object)['date'=>new Literal('1955', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(3)],
       
    56              ]));
       
    57          $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
    58          $this->seeJsonEquals(["datestats" => [
       
    59              "1955" => 3,
       
    60              "1965" => 2,
       
    61              "1975" => 1,
       
    62          ]]);
       
    63     }
       
    64 
       
    65     public function testIndexSimple() {
       
    66 
       
    67          $this->sparqlClient
       
    68              ->shouldReceive('query')
       
    69              ->once()
       
    70              ->andReturn(new \ArrayIterator([
       
    71                  (object)['date'=>new Literal('1955', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)],
       
    72                  (object)['date'=>new Literal('1965', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)],
       
    73                  (object)['date'=>new Literal('1975', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)],
       
    74              ]));
       
    75          $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
    76          $this->seeJsonEquals(["datestats" => [
       
    77              "1955" => 1,
       
    78              "1965" => 1,
       
    79              "1975" => 1,
       
    80          ]]);
       
    81     }
       
    82 
       
    83     public function testIndexPeriod() {
       
    84 
       
    85         $this->sparqlClient
       
    86              ->shouldReceive('query')
       
    87              ->once()
       
    88              ->andReturn(new \ArrayIterator([
       
    89                  (object)['date'=>new Literal('start=1955; end=1965', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)],
       
    90              ]));
       
    91         $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
    92         $this->seeJsonEquals(["datestats" => [
       
    93             "1955" => 1,
       
    94             "1956" => 1,
       
    95             "1957" => 1,
       
    96             "1958" => 1,
       
    97             "1959" => 1,
       
    98             "1960" => 1,
       
    99             "1961" => 1,
       
   100             "1962" => 1,
       
   101             "1963" => 1,
       
   102             "1964" => 1,
       
   103             "1965" => 1,
       
   104         ]]);
       
   105     }
       
   106 
       
   107     public function testIndexPeriodRemainMore() {
       
   108 
       
   109         $this->sparqlClient
       
   110              ->shouldReceive('query')
       
   111              ->once()
       
   112              ->andReturn(new \ArrayIterator([
       
   113                  (object)['date'=>new Literal('start=1955; end=1965', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(15)],
       
   114              ]));
       
   115         $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
   116         $this->seeJsonEquals(["datestats" => [
       
   117             "1955" => 2,
       
   118             "1956" => 2,
       
   119             "1957" => 2,
       
   120             "1958" => 2,
       
   121             "1959" => 1,
       
   122             "1960" => 1,
       
   123             "1961" => 1,
       
   124             "1962" => 1,
       
   125             "1963" => 1,
       
   126             "1964" => 1,
       
   127             "1965" => 1,
       
   128         ]]);
       
   129     }
       
   130 
       
   131     public function testIndexPeriodRemainLess() {
       
   132 
       
   133         $this->sparqlClient
       
   134              ->shouldReceive('query')
       
   135              ->once()
       
   136              ->andReturn(new \ArrayIterator([
       
   137                  (object)['date'=>new Literal('start=1955; end=1965', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(10)],
       
   138              ]));
       
   139         $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
   140         $this->seeJsonEquals(["datestats" => [
       
   141             "1955" => 1,
       
   142             "1956" => 1,
       
   143             "1957" => 1,
       
   144             "1958" => 1,
       
   145             "1959" => 1,
       
   146             "1960" => 1,
       
   147             "1961" => 1,
       
   148             "1962" => 1,
       
   149             "1963" => 1,
       
   150             "1964" => 1,
       
   151         ]]);
       
   152     }
       
   153 
       
   154     public function testIndexMix() {
       
   155 
       
   156         $this->sparqlClient
       
   157              ->shouldReceive('query')
       
   158              ->once()
       
   159              ->andReturn(new \ArrayIterator([
       
   160                  (object)['date'=>new Literal('start=1955; end=1965', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)],
       
   161                  (object)['date'=>new Literal('1960', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(2)],
       
   162              ]));
       
   163         $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
   164         $this->seeJsonEquals(["datestats" => [
       
   165             "1955" => 1,
       
   166             "1956" => 1,
       
   167             "1957" => 1,
       
   168             "1958" => 1,
       
   169             "1959" => 1,
       
   170             "1960" => 3,
       
   171             "1961" => 1,
       
   172             "1962" => 1,
       
   173             "1963" => 1,
       
   174             "1964" => 1,
       
   175             "1965" => 1,
       
   176         ]]);
       
   177     }
       
   178 
       
   179     public function testIndexBadDate() {
       
   180 
       
   181          $this->sparqlClient
       
   182              ->shouldReceive('query')
       
   183              ->once()
       
   184              ->andReturn(new \ArrayIterator([
       
   185                  (object)['date'=>new Literal('1955', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)],
       
   186                  (object)['date'=>new Literal('HELLO', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)],
       
   187                  (object)['date'=>new Literal('1975', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)],
       
   188              ]));
       
   189          $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
   190          $this->seeJsonEquals(["datestats" => [
       
   191              "1955" => 1,
       
   192              "1975" => 1,
       
   193          ]]);
       
   194     }
       
   195 
       
   196     public function testIndexBadPeriod() {
       
   197 
       
   198         $this->sparqlClient
       
   199              ->shouldReceive('query')
       
   200              ->once()
       
   201              ->andReturn(new \ArrayIterator([
       
   202                  (object)['date'=>new Literal('start=1955; end=FOO', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)],
       
   203              ]));
       
   204         $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
   205         $this->seeJsonEquals(["datestats" => [
       
   206         ]]);
       
   207     }
       
   208 
       
   209     public function testIndexBadPeriodMissing() {
       
   210 
       
   211         $this->sparqlClient
       
   212              ->shouldReceive('query')
       
   213              ->once()
       
   214              ->andReturn(new \ArrayIterator([
       
   215                  (object)['date'=>new Literal('start=1955', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)],
       
   216              ]));
       
   217         $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
   218         $this->seeJsonEquals(["datestats" => [
       
   219         ]]);
       
   220     }
       
   221 
       
   222     public function testIndexFullPeriod() {
       
   223 
       
   224         $this->sparqlClient
       
   225              ->shouldReceive('query')
       
   226              ->once()
       
   227              ->andReturn(new \ArrayIterator([
       
   228                  (object)['date'=>new Literal('start=1955; end=1965; scheme=v3; name=v4;', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)],
       
   229              ]));
       
   230         $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
   231         $this->seeJsonEquals(["datestats" => [
       
   232             "1955" => 1,
       
   233             "1956" => 1,
       
   234             "1957" => 1,
       
   235             "1958" => 1,
       
   236             "1959" => 1,
       
   237             "1960" => 1,
       
   238             "1961" => 1,
       
   239             "1962" => 1,
       
   240             "1963" => 1,
       
   241             "1964" => 1,
       
   242             "1965" => 1,
       
   243         ]]);
       
   244     }
       
   245 
       
   246     public function testIndexMultipleFormat() {
       
   247 
       
   248          $this->sparqlClient
       
   249              ->shouldReceive('query')
       
   250              ->once()
       
   251              ->andReturn(new \ArrayIterator([
       
   252                  (object)['date'=>new Literal('1975-02-05', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)],
       
   253                  (object)['date'=>new Literal('1965-03', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(2)],
       
   254                  (object)['date'=>new Literal('1955-02-12T08:30:00+00:00', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(3)],
       
   255                  (object)['date'=>new Literal('1950-08-18T08:30:00Z', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(4)],
       
   256              ]));
       
   257          $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content());
       
   258          $this->seeJsonEquals(["datestats" => [
       
   259              "1950" => 4,
       
   260              "1955" => 3,
       
   261              "1965" => 2,
       
   262              "1975" => 1,
       
   263          ]]);
       
   264     }
       
   265 
       
   266 
       
   267 
       
   268 }