/** * Will create a new ColumnConfiguration with all defaults but allows overriding of all properties through the method. * * @param string $name The name of the configuration, required for the configuration * @param string|callable $callable The function to execute, defaults to null which means the default will be set. * @param Searchable $searchable If the column should be searchable or not * @param Orderable $orderable If the column should be orderable or not * @return $this */ public function column($name, $callable = null, Searchable $searchable = null, Orderable $orderable = null) { /** * @var ColumnConfigurationBuilder */ $config = ColumnConfigurationBuilder::create(); if (is_string($name)) { $config->name($name); } else { throw new \InvalidArgumentException('$name must be a string'); } if (!is_null($callable)) { if (is_callable($callable)) { $config->withCallable($callable); } elseif (is_string($callable)) { $config->withCallable(function () use($callable) { return $callable; }); } } if (is_null($searchable)) { $config->searchable(Searchable::NORMAL()); } if (is_null($orderable)) { $config->orderable(Orderable::BOTH()); } $this->columnConfiguration[] = $config->build(); return $this; }
public function testConstructWithColumns() { $this->viewFactory = \Mockery::mock('Illuminate\\Contracts\\View\\Factory'); $this->configRepository = \Mockery::mock('Illuminate\\Contracts\\Config\\Repository'); $this->configRepository->shouldReceive('get')->andReturn("fooBar"); $column = ColumnConfigurationBuilder::create()->name('id')->build(); $this->dtv = new DatatableView("fooTable", "fooScript", $this->viewFactory, $this->configRepository, [$column]); }
public function testParse() { $column = ColumnConfigurationBuilder::create()->name("id")->build(); $column1 = ColumnConfigurationBuilder::create()->name("name")->build(); $cc = $this->version->parseRequest([$column, $column1]); $this->assertNotNull($cc); $rsp = $this->version->createResponse(new ResponseData(new Collection([]), 123), $cc, []); $this->assertNotNull($rsp); }
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); }
/** * Will test if the default callable can work with multiple data representations */ public function testCallable() { $obj = new FooClass(); $cc = ColumnConfigurationBuilder::create()->name("fooBar")->build(); $func = $cc->getCallable(); $this->assertSame("", $func(["foo" => "bar"])); $this->assertSame("bar", $func(["fooBar" => "bar"])); $this->assertTrue(is_object($obj)); $this->assertSame("", $func($obj)); $cc = ColumnConfigurationBuilder::create()->name("fooProperty")->build(); $func = $cc->getCallable(); $this->assertSame("barProperty", $func($obj)); $cc = ColumnConfigurationBuilder::create()->name("fooMethod")->build(); $func = $cc->getCallable(); $this->assertSame("barMethod", $func($obj)); }
/** * @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'); }
/** * Will test that an empty search will not trigger a search. */ public function testEmptySearch() { $this->request = new Request(['draw' => 13, 'start' => 11, 'length' => 103, 'search' => ['value' => '', 'regex' => true], 'order' => [0 => ['column' => 0, 'dir' => 'asc']], 'columns' => [0 => ['search' => ['value' => '', 'regex' => true]]]]); $this->parser = new Datatable110QueryParser($this->request); $column = ColumnConfigurationBuilder::create()->name("id")->build(); $conf = $this->parser->parse($this->request, [$column]); // assert column order $this->assertFalse($conf->isGlobalSearch()); $this->assertCount(0, $conf->searchColumns()); $this->assertFalse($conf->isColumnSearch()); }
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(); }
/** * Will test if the composer functions correct when the configure method is called */ public function testConfigureColumn() { $name = "fooBar"; $config = ColumnConfigurationBuilder::create()->name($name)->build(); $this->composer->add($config); // get configuration and verify $numberOfColumns = count($this->composer->getColumnConfiguration()); $this->assertSame($numberOfColumns, 1, "There should only be one column configuration"); /** * @var ColumnConfiguration */ $cc = $this->composer->getColumnConfiguration()[0]; $func = $cc->getCallable(); $this->assertTrue($cc->getSearch()->isSearchable(), "The column should not be searchable"); $this->assertTrue($cc->getOrder()->isOrderable(), "The column should be orderable"); $this->assertSame($name, $cc->getName(), "The name should be set to 'fooBar'"); $this->assertSame("bar", $func(["fooBar" => "bar"])); }
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()); }
public function testArrayHasValidKeys() { $requestParameters = ['draw' => 13, 'start' => 11, 'length' => 103, 'order' => [0 => ['column' => 0, 'dir' => 'asc']]]; $this->request = new Request($requestParameters); $this->parser = new Datatable110QueryParser($this->request); $column = ColumnConfigurationBuilder::create()->name("id")->build(); $conf = $this->parser->parse($this->request, [$column]); $this->assertFalse($conf->isGlobalSearch()); $requestParameters['search'] = ['string' => 'something']; $this->request = new Request($requestParameters); $this->parser = new Datatable110QueryParser($this->request); $conf = $this->parser->parse($this->request, [$column]); $this->assertFalse($conf->isGlobalSearch()); $requestParameters['search'] = ['string' => 'something', 'regex' => true]; $this->request = new Request($requestParameters); $this->parser = new Datatable110QueryParser($this->request); $conf = $this->parser->parse($this->request, [$column]); $this->assertFalse($conf->isGlobalSearch()); }
/** * Will test that an empty search will not trigger a search. */ public function testEmptySearch() { $this->request = new Request(['sEcho' => 13, 'iDisplayStart' => 11, 'iDisplayLength' => 103, 'sSearch' => '', 'sSearch_0' => '']); $this->parser = new Datatable19QueryParser($this->request); $column = ColumnConfigurationBuilder::create()->name("id")->build(); $conf = $this->parser->parse($this->request, [$column]); // assert column order $this->assertFalse($conf->isGlobalSearch()); $this->assertCount(0, $conf->searchColumns()); $this->assertFalse($conf->isColumnSearch()); }