public function testResponseConstruction() { $data = [['id' => 1, 'name' => 'fooBar'], ['id' => 2, 'name' => 'BazQua']]; $queryConfiguration = QueryConfigurationBuilder::create()->build(); $columnConfiguration = ColumnConfigurationBuilder::create()->name('id')->build(); $respData = new ResponseData(new Collection($data), 123); $rsp = new Datatable19ResponseCreator(); $response = $rsp->createResponse($respData, $queryConfiguration, [$columnConfiguration]); $this->assertSame(200, $response->getStatusCode()); $body = json_decode($response->getContent()); $this->assertSame(1, $body->sEcho); $this->assertSame(123, $body->iTotalRecords); $this->assertSame(2, $body->iTotalDisplayRecords); }
/** * @expectedException OpenSkill\Datatable\DatatableException * @expectedExceptionMessage A requested column was not found in the columnConfiguration. */ public function testExceptionIfColumnIsNotFound() { $queryConfiguration = QueryConfigurationBuilder::create()->start(0)->length(2)->drawCall(1)->columnOrder('name', 'desc')->build(); $columnConfiguration = ColumnConfigurationBuilder::create()->name('foundColumn')->build(); /** * Create a mocked query builder... */ $queryBuilder = $this->setupMockQueryBuilder(); $queryBuilder->shouldReceive('orderBy')->with('name', 'desc')->once(); $queryBuilder->shouldReceive('skip')->with(0); $queryBuilder->shouldReceive('limit')->with(2); $queryBuilder->shouldReceive('count')->withNoArgs(); $queryBuilder->shouldReceive('get')->withArgs([['name']]); $provider = new QueryBuilderProvider($queryBuilder); $provider->prepareForProcessing($queryConfiguration, [$columnConfiguration]); $getColumnFromName = new \ReflectionMethod($provider, 'getColumnFromName'); $getColumnFromName->setAccessible(true); //$provider->getColumnFromName('notFound'); $getColumnFromName->invoke($provider, 'notFound'); }
/** * Method that should parse the request and return a DTQueryConfiguration * * @param ColumnConfiguration[] $columnConfiguration The configuration of the columns * * @return QueryConfiguration the configuration the provider can use to prepare the data */ public function parse(array $columnConfiguration) { $query = $this->request->query; $builder = QueryConfigurationBuilder::create(); if ($query->has('sEcho')) { $builder->drawCall($query->get('sEcho')); } if ($query->has('iDisplayStart')) { $builder->start($query->get('iDisplayStart')); } if ($query->has('iDisplayLength')) { $builder->length($query->get('iDisplayLength')); } if ($query->has('sSearch') && !$this->isEmpty($query->get('sSearch'))) { $builder->searchValue($query->get('sSearch')); } if ($query->has('bRegex')) { $builder->searchRegex($query->get('bRegex')); } // for each column we need to see if there is a search value foreach ($columnConfiguration as $i => $c) { // increment the index as we are 0 based but data tables is not $i++; // check if there is something search related if ($c->getSearch()->isSearchable() && $query->has("sSearch_" . $i) && !$this->isEmpty($query->get("sSearch_" . $i))) { // search for this column is available $builder->columnSearch($c->getName(), $query->get("sSearch_" . $i)); } // check if there is something order related if ($c->getOrder()->isOrderable() && $query->has("iSortCol_" . $i) && !$this->isEmpty($query->get("iSortCol_" . $i))) { // order for this column is available $builder->columnOrder($c->getName(), $query->get("sSortDir_" . $i)); } } return $builder->build(); }
private function orderAndSearchNotImplementedTest($searchableType = null) { $queryConfiguration = QueryConfigurationBuilder::create()->start(0)->length(4)->drawCall(1)->columnOrder('name', 'asc')->columnOrder('id', 'desc'); $queryConfiguration = $queryConfiguration->searchValue('blah'); $queryConfiguration = $queryConfiguration->build(); $columnConfiguration = []; $columnConfiguration[] = ColumnConfigurationBuilder::create()->name('id')->searchable(Searchable::NONE())->build(); $columnConfiguration[] = ColumnConfigurationBuilder::create()->name('name')->searchable($searchableType)->build(); // Set up mock item $queryBuilder = $this->setupMockQueryBuilder(); $provider = new QueryBuilderProvider($queryBuilder); $provider->prepareForProcessing($queryConfiguration, $columnConfiguration); $provider->process(); }
public function testDefaultOrderMulti() { $data = [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar'], ['id' => 3, 'name' => 'foo'], ['id' => 4, 'name' => 'bar']]; $queryConfiguration = QueryConfigurationBuilder::create()->start(0)->length(4)->drawCall(1)->columnOrder('name', 'asc')->columnOrder('id', 'desc')->build(); $columnConfiguration = ColumnConfigurationBuilder::create()->name('id')->build(); $columnConfiguration2 = ColumnConfigurationBuilder::create()->name('name')->build(); $provider = new CollectionProvider(new Collection($data)); $provider->prepareForProcessing($queryConfiguration, [$columnConfiguration, $columnConfiguration2]); $data = $provider->process(); $this->assertSame(4, $data->data()->count()); $first = $data->data()->first(); $second = $data->data()->get(1); $this->assertSame(4, $first['id']); $this->assertSame(2, $second['id']); }
/** * @param ParameterBag $query * @param QueryConfigurationBuilder $builder * @param ColumnConfiguration[] $columnConfiguration */ private function getOrder(ParameterBag $query, QueryConfigurationBuilder $builder, array $columnConfiguration) { //loop over the order if ($query->has('order')) { $order = $query->get('order'); foreach ($order as $i => $config) { if (array_key_exists($config['column'], $columnConfiguration)) { $column = $columnConfiguration[$config['column']]; if ($column->getOrder()->isOrderable()) { $builder->columnOrder($column->getName(), $config['dir']); } } } } }
public function testCustomColumn() { $data = [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]; $queryConfiguration = QueryConfigurationBuilder::create()->start(0)->length(2)->columnSearch('id', 'foo')->drawCall(1)->build(); $columnConfiguration = ColumnConfigurationBuilder::create()->name('id')->build(); $provider = new CollectionProvider(new Collection($data)); $provider->searchColumn('id', function ($data, $search) { // we only accept columns with the id 1 if the user searched in the column return $data['id'] == 1; }); $provider->prepareForProcessing($queryConfiguration, [$columnConfiguration]); $data = $provider->process(); $this->assertSame(1, $data->data()->count()); }
/** * @expectedException \InvalidArgumentException */ public function testSearchOrderException() { $b = QueryConfigurationBuilder::create(); $b->columnSearch("foo", 123); }
/** * @param ParameterBag $query * @param QueryConfigurationBuilder $builder */ public function getRegex($query, $builder) { if ($query->has('bRegex')) { $builder->searchRegex($query->get('bRegex')); } }