/** * Method that should parse the request and return a DTQueryConfiguration * * @param Request $request The current request that should be investigated * @param ColumnConfiguration[] $columnConfiguration The configuration of the columns * @return QueryConfiguration the configuration the provider can use to prepare the data */ public function parse(Request $request, array $columnConfiguration) { $query = $request->query; $builder = QueryConfigurationBuilder::create(); $this->getDrawCall($query, $builder); $this->getStart($query, $builder); $this->getLength($query, $builder); $this->getSearch($query, $builder); $this->getRegex($query, $builder); // 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(); }
/** * Method that should parse the request and return a DTQueryConfiguration * * @param Request $request The current request that should be investigated * @param ColumnConfiguration[] $columnConfiguration The configuration of the columns * @return QueryConfiguration the configuration the provider can use to prepare the data */ public function parse(Request $request, array $columnConfiguration) { $query = $request->query; $builder = QueryConfigurationBuilder::create(); $this->getDrawCall($query, $builder); $this->getStart($query, $builder); $this->getLength($query, $builder); $this->getSearch($query, $builder); $this->getRegex($query, $builder); $this->getSearchColumns($query, $builder, $columnConfiguration); return $builder->build(); }
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'); }
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']); }
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); }