diff --git a/spec/suite/Adapters/AdapterInterfaceSpec.php b/spec/suite/Adapters/AdapterInterfaceSpec.php index e69de29..357fa56 100644 --- a/spec/suite/Adapters/AdapterInterfaceSpec.php +++ b/spec/suite/Adapters/AdapterInterfaceSpec.php @@ -0,0 +1,124 @@ +setParser()", function() { + + it("should not set parser if it not an instance of ParamsParser", function() { + + $adapter = new MyAdapter(10); + expect(function() use($adapter) { + $adapter->setParser([1, 2, 3]); + })->toThrow(); + + }); + + it("should set parser successfully", function() { + + $adapter = new MyAdapter(10); + $parser = new ParamsParser(10); + $adapter->setParser($parser); + expect($adapter->getParser())->toBe($parser); + + }); + + }); + + describe("->setColumns()", function() { + + it("should throw when columns are not array", function() { + + $adapter = new MyAdapter(10); + expect(function() use($adapter) { + $adapter->setColumns('user, email, name'); + })->toThrow(); + + }); + + it("should successfully set columns", function() { + + $adapter = new MyAdapter(10); + $columns = ['user', 'name', 'email']; + $adapter->setColumns($columns); + expect($adapter->getColumns())->toBe($columns); + + }); + + }); + + it("->columnExists()", function() { + + $adapter = new MyAdapter(10); + $columns = ['user', 'name', 'email']; + $adapter->setColumns($columns); + expect($adapter->columnExists('user'))->toBe(true); + expect($adapter->columnExists('user1'))->toBe(false); + + }); + + describe("->formResponse()", function() { + + beforeEach(function() { + + $adapter = new MyAdapter(10); + $parser = new ParamsParser(10); + $adapter->setParser($parser); + + $this->adapter = $adapter; + + }); + + it("should set default params", function() { + + $response = $this->adapter->formResponse([]); + expect($response)->toBe([ + 'draw' => null, + 'recordsTotal' => 0, + 'recordsFiltered' => 0, + 'data' => [] + ]); + + }); + + }); + + it("->sanitaze()", function() { + + $string = str_repeat('*', 100); + $adapter = new MyAdapter(10); + $newString = $adapter->sanitaze($string); + expect(strlen($newString))->toBe(10); + + }); + + describe("->bind()", function() { + + it("should throw an exception on unknown bind", function() { + + expect(function() { + $adapter = new MyAdapter(10); + $adapter->bind('some_unknown_action', function() { + echo 'Hello!'; + }); + })->toThrow(); + + }); + + }); + + +}); diff --git a/spec/suite/Adapters/QueryBuilderSpec.php b/spec/suite/Adapters/QueryBuilderSpec.php index 2669a0c..0359dad 100644 --- a/spec/suite/Adapters/QueryBuilderSpec.php +++ b/spec/suite/Adapters/QueryBuilderSpec.php @@ -37,6 +37,61 @@ }); + describe("Limit&Offset", function() { + + beforeEach(function() { + + $_GET = ['start' => 2, 'length' => 1]; + + }); + + it("should work with start&length", function() { + + $dataTables = new QueryBuilder(20); + $dataTables->setBuilder($this->builder); + $dataTables->setParser(new ParamsParser(10)); + $response = $dataTables->getResponse(); + expect(count($response['data']))->toBe(1); + + $dataOne = $response['data']; + + $_GET['start'] = 3; + $dataTables = new QueryBuilder(20); + $dataTables->setBuilder($this->builder); + $dataTables->setParser(new ParamsParser(10)); + $response = $dataTables->getResponse(); + expect(count($response['data']))->toBe(1); + expect($response['data'])->not->toBe($dataOne); + + }); + + it("should work with a filter", function() { + + $_GET['search'] = ['value' => 'kr']; + $_GET['columns'] = [ + [ + 'data' => 'name', + 'searchable' => "true" + ] + ]; + + $dataTables = new QueryBuilder(20); + $dataTables->setBuilder($this->builder); + $dataTables->setParser(new ParamsParser(10)); + $dataTables->setColumns(['name']); + $response = $dataTables->getResponse(); + expect(count($response['data']))->toBe(1); + + }); + + afterEach(function() { + + unset($_GET); + + }); + + }); + it("should work with a global search", function() { $_GET = [ diff --git a/spec/suite/Adapters/ResultSetSpec.php b/spec/suite/Adapters/ResultSetSpec.php index 1f13316..087e33b 100644 --- a/spec/suite/Adapters/ResultSetSpec.php +++ b/spec/suite/Adapters/ResultSetSpec.php @@ -37,6 +37,61 @@ }); + describe("Limit&Offset", function() { + + beforeEach(function() { + + $_GET = ['start' => 2, 'length' => 1]; + + }); + + it("should work with start&length", function() { + + $dataTables = new ResultSet(10); + $dataTables->setResultSet($this->query); + $dataTables->setParser(new ParamsParser(10)); + $response = $dataTables->getResponse(); + expect(count($response['data']))->toBe(1); + + $dataOne = $response['data']; + + $_GET['start'] = 3; + $dataTables = new ResultSet(10); + $dataTables->setResultSet($this->query); + $dataTables->setParser(new ParamsParser(10)); + $response = $dataTables->getResponse(); + expect(count($response['data']))->toBe(1); + expect($response['data'])->not->toBe($dataOne); + + }); + + it("should work with a filter", function() { + + $_GET['search'] = ['value' => 'kr']; + $_GET['columns'] = [ + [ + 'data' => 'name', + 'searchable' => "true" + ] + ]; + + $dataTables = new ResultSet(10); + $dataTables->setResultSet($this->query); + $dataTables->setParser(new ParamsParser(10)); + $dataTables->setColumns(['name']); + $response = $dataTables->getResponse(); + expect(count($response['data']))->toBe(1); + + }); + + afterEach(function() { + + unset($_GET); + + }); + + }); + it("should work with a global search", function() { $_GET = [ diff --git a/src/Adapters/AdapterInterface.php b/src/Adapters/AdapterInterface.php index 77a9b3c..dc8958e 100644 --- a/src/Adapters/AdapterInterface.php +++ b/src/Adapters/AdapterInterface.php @@ -2,6 +2,8 @@ namespace DataTables\Adapters; +use DataTables\ParamsParser; + abstract class AdapterInterface { protected $parser = null; @@ -14,14 +16,18 @@ public function __construct($length) { abstract public function getResponse(); - public function setParser($parser) { + public function setParser(ParamsParser $parser) { $this->parser = $parser; } - public function setColumns($columns) { + public function setColumns(array $columns) { $this->columns = $columns; } + public function getColumns() { + return $this->columns; + } + public function columnExists($column) { return in_array($column, $this->columns); } @@ -102,7 +108,6 @@ public function bind($case, $closure) { break; default: throw new \Exception('Unknown bind type'); - break; } }