Skip to content

Commit

Permalink
Improved code coverage;
Browse files Browse the repository at this point in the history
Improved checking in AdapterInterface;
  • Loading branch information
m1ome committed Feb 7, 2015
1 parent 9e625c9 commit f804a25
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 3 deletions.
124 changes: 124 additions & 0 deletions spec/suite/Adapters/AdapterInterfaceSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
namespace Spec\Adapters;

use kahlan\plugin\Stub;
use DataTables\Adapters\AdapterInterface;
use DataTables\ParamsParser;

class MyAdapter extends AdapterInterface{

public function getResponse() {
return [];
}

}


describe("AdapterInterface", function() {

describe("->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();

});

});


});
55 changes: 55 additions & 0 deletions spec/suite/Adapters/QueryBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
55 changes: 55 additions & 0 deletions spec/suite/Adapters/ResultSetSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
11 changes: 8 additions & 3 deletions src/Adapters/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DataTables\Adapters;

use DataTables\ParamsParser;

abstract class AdapterInterface {

protected $parser = null;
Expand All @@ -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);
}
Expand Down Expand Up @@ -102,7 +108,6 @@ public function bind($case, $closure) {
break;
default:
throw new \Exception('Unknown bind type');
break;
}

}
Expand Down

0 comments on commit f804a25

Please sign in to comment.