├── .travis.yml
├── LICENSE
├── README.md
├── composer.json
├── composer.lock
├── phpunit.xml
├── src
└── Jacopo
│ └── Bootstrap3Table
│ ├── BootstrapTable.php
│ ├── TableHeader.php
│ ├── TableLine.php
│ └── TableRow.php
├── tests
├── BootstrapTableTest.php
└── TableLineTest.php
└── travis.yml
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - 5.3
5 | - 5.4
6 | - 5.5
7 |
8 | before_script:
9 | - curl -s http://getcomposer.org/installer | php
10 | - php composer.phar install --dev
11 |
12 | script: phpunit
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2013 jacopo
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Bootstrap 3 Table generator
2 |
3 | This package is a dynamic table generator for Bootstrap 3 written in Php.
4 | This package is fully tested and ready for use.
5 |
6 | [](https://travis-ci.org/intrip/bootstrap-table-generator)
7 |
8 | ### Installation
9 |
10 | The first requisite to run the package is to include Bootstrap 3 in your Application.
11 | For more info see: this link.
12 |
13 | The next step is to install this package through Composer. Edit your project's `composer.json` file to require `"jacopo/bootstrap-3-table-generator": "dev-master"`.
14 |
15 | "require": {
16 | "jacopo/bootstrap-3-table-generator": "dev-master"
17 | },
18 | "minimum-stability" : "dev"
19 |
20 | Next, update Composer from the Terminal:
21 |
22 | composer update
23 |
24 | That's it. You successfully installed Bootstrap 3 table generator!
25 |
26 | ### Usage
27 |
28 | To use te package you need to require `Jacopo\Bootstrap3Table\BootstrapTable`.
29 | Here is an example for a quick usage of the tool:
30 |
31 | ```PHP
32 | use Jacopo\Bootstrap3Table\BootstrapTable;
33 |
34 | // create the generator class
35 | $table = new BootstrapTable();
36 | // set the configuration
37 | $table->setConfig(array("table-hover"=>false, "table-condensed"=>true, "table-striped"=>true ) );
38 | // set header content (optional)
39 | $table->setHeader(array("firstCol") );
40 | // add table row
41 | $table->addRows(array("cell1","cell2"), array("custom-class1"));
42 | // you can also add a bigger row
43 | $table->addRows(array("cell1","cell2","cell3"));
44 | // or add a smaller row
45 | $table->addRows(array("cell1"));
46 |
47 | // setup extra custom css classes for the table
48 | $table->setTableExtraClasses(array("extra-table"));
49 | // print the table
50 | echo $table; // equals to echo $table->getHtml();
51 | ```
52 |
53 | ### Methods overview
54 |
55 | The methods available are:
56 |
57 | `setConfig`: set the base configuration of the table. Accepts an array of options. The option available are:
58 |
59 | `table-striped`: Adds zebra-striping to any table row .
60 | `table-bordered`: Add borders and rounded corners to the table.
61 | `table-hover`: Enable a hover state on table rows.
62 | `table-condensed`: Makes tables more compact by cutting cell padding in half.
63 | `table-responsive`: Makes table responsive.
64 | `id`: Set the id of the table.
65 |
66 | You can also add extra css classes to the `
`tag. To do that you need to set the
67 | `setTableExtraClasses()`method.
68 |
69 | Example:
70 |
71 | ```PHP
72 | $table = new BootstrapTable();
73 | $table->setTableExtraClasses(array("extra-custom-class") );
74 | ```
75 |
76 | `setHeader`: this method setup the header of the file, the only parameter is an array that contains the data of each ``.
77 | Setting header is optional, if not setted the header won't be shown.
78 |
79 | Example:
80 |
81 | ```PHP
82 | $table = new BootstrapTable();
83 | $table->setHeader(array("First header column data") );
84 | ```
85 |
86 | `addRows`: add a row of data to the table. Accepts two params: the fist is the array of data, the second is an array of custom css classes to add to the ` | ` tag.
87 |
88 | Example:
89 | ```PHP
90 | $table = new BootstrapTable();
91 | $table->addRows(array("First data column data"), array("custom-class1") );
92 | ```
93 |
94 | #### Dynamic Size
95 |
96 | Keep in mind that you dont have to set the size of the table, you can add as many rows as you want
97 | and the table size will adjust automatically!
98 |
99 | #### Printing the table
100 |
101 | When you're done setting up you table you can just do `echo $table` and you'll see the table as html.
102 | If you prefer you can get the html string of the table instead with the `table->getHtml()` method.
103 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jacopo/bootstrap-3-table-generator",
3 | "description": "Dynamic table generator for bootstrap 3",
4 | "keywords": ["generator", "table", "bootstrap","bootstrap3","create"],
5 | "license": "MIT",
6 | "authors": [
7 | {
8 | "name": "Jacopo",
9 | "email": "beschi.jacopo@gmail.com"
10 | }
11 | ],
12 | "require": {
13 | "php": ">=5.3.0"
14 | },
15 | "require-dev": {
16 | "mockery/mockery": "dev-master"
17 | },
18 | "autoload": {
19 | "psr-0": {
20 | "Jacopo\\Bootstrap3Table": "src/"
21 | }
22 | },
23 | "minimum-stability": "dev"
24 | }
--------------------------------------------------------------------------------
/composer.lock:
--------------------------------------------------------------------------------
1 | {
2 | "_readme": [
3 | "This file locks the dependencies of your project to a known state",
4 | "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
5 | ],
6 | "hash": "6923afb3391b7fc8fe578cd5bb74331f",
7 | "packages": [
8 |
9 | ],
10 | "packages-dev": [
11 | {
12 | "name": "mockery/mockery",
13 | "version": "dev-master",
14 | "source": {
15 | "type": "git",
16 | "url": "https://github.com/padraic/mockery.git",
17 | "reference": "1e1e189dabda8bf47de0a64371987e7a80db581f"
18 | },
19 | "dist": {
20 | "type": "zip",
21 | "url": "https://api.github.com/repos/padraic/mockery/zipball/1e1e189dabda8bf47de0a64371987e7a80db581f",
22 | "reference": "1e1e189dabda8bf47de0a64371987e7a80db581f",
23 | "shasum": ""
24 | },
25 | "require": {
26 | "lib-pcre": ">=7.0",
27 | "php": ">=5.3.2"
28 | },
29 | "require-dev": {
30 | "hamcrest/hamcrest": "1.1.0"
31 | },
32 | "type": "library",
33 | "autoload": {
34 | "psr-0": {
35 | "Mockery": "library/"
36 | }
37 | },
38 | "notification-url": "https://packagist.org/downloads/",
39 | "license": [
40 | "BSD-3-Clause"
41 | ],
42 | "authors": [
43 | {
44 | "name": "Pádraic Brady",
45 | "email": "padraic.brady@gmail.com",
46 | "homepage": "http://blog.astrumfutura.com"
47 | }
48 | ],
49 | "description": "Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succint API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL). Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library, Mockery is easy to integrate with PHPUnit and can operate alongside phpunit-mock-objects without the World ending.",
50 | "homepage": "http://github.com/padraic/mockery",
51 | "keywords": [
52 | "BDD",
53 | "TDD",
54 | "library",
55 | "mock",
56 | "mock objects",
57 | "mockery",
58 | "stub",
59 | "test",
60 | "test double",
61 | "testing"
62 | ],
63 | "time": "2013-10-02 19:52:52"
64 | }
65 | ],
66 | "aliases": [
67 |
68 | ],
69 | "minimum-stability": "dev",
70 | "stability-flags": {
71 | "mockery/mockery": 20
72 | },
73 | "platform": {
74 | "php": ">=5.3.0"
75 | },
76 | "platform-dev": [
77 |
78 | ]
79 | }
80 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
15 | ./tests/
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/Jacopo/Bootstrap3Table/BootstrapTable.php:
--------------------------------------------------------------------------------
1 | setConfig(array("table-hover"=>true));
8 | * $table->setHeader(array("firstCol",...));
9 | * $table->addRow(array("cell1","cell2",...), array("class1"));
10 | * echo $table; //returns getHtml()
11 | *
12 | * @author Jacopo Beschi
13 | */
14 |
15 | class BootstrapTable
16 | {
17 | /**
18 | * Table data rows
19 | */
20 | protected $rows;
21 | /**
22 | * Table heading
23 | */
24 | protected $header;
25 | /**
26 | * max lenght of rows insert
27 | */
28 | protected $max_length_rows = 0;
29 | /**
30 | * Id of the table
31 | * @var String|null
32 | */
33 | public $id = null;
34 | /**
35 | * if tables are zebra striped
36 | */
37 | public $table_striped = false;
38 | /**
39 | * if table have border on sides
40 | */
41 | public $table_bordered = false;
42 | /**
43 | * if table color on hover
44 | */
45 | public $table_hover = false;
46 | /**
47 | * if table is show in condensed mode
48 | */
49 | public $table_condensed = false;
50 | /**
51 | * if table is responsive
52 | */
53 | public $table_responsive = false;
54 | /**
55 | * Extra custom classes for the table
56 | * @var array
57 | */
58 | protected $table_extra_classes = array();
59 |
60 | public function __construct(array $config = array() )
61 | {
62 | if($config)
63 | $this->setConfig($config);
64 | }
65 |
66 | /**
67 | * Set the configuration of the table
68 | *
69 | * @param array $config
70 | * @throws InvalidArgumentException
71 | * @return void
72 | */
73 | public function setConfig(array $configs = array() )
74 | {
75 | foreach($configs as $config_key => $config)
76 | {
77 | switch($config_key)
78 | {
79 | case 'table-striped':
80 | $this->table_striped = $config;
81 | break;
82 | case 'table-bordered':
83 | $this->table_bordered = $config;
84 | break;
85 | case 'table-hover':
86 | $this->table_hover = $config;
87 | break;
88 | case 'table-condensed':
89 | $this->table_condensed = $config;
90 | break;
91 | case 'table-responsive':
92 | $this->table_responsive = $config;
93 | break;
94 | case 'id':
95 | $this->id = $config;
96 | break;
97 | default:
98 | throw new \InvalidArgumentException();
99 | break;
100 | }
101 | }
102 | }
103 |
104 | /**
105 | * Set the header of the table
106 | *
107 | * @param array $header_cols columns of the header
108 | * @return void
109 | */
110 | public function setHeader(array $header_cols){
111 | $this->header = new TableHeader($header_cols);
112 |
113 | $this->updateMaxRowLength($this->header);
114 | }
115 |
116 | /**
117 | * Add a row to the table
118 | *
119 | * @param array $row row with data columns
120 | * @return void
121 | */
122 | public function addRows(array $rows, array $classes = array() ){
123 | $table_row = new TableRow($rows, $classes);
124 | $this->rows[] = $table_row;
125 |
126 | $this->updateMaxRowLength($table_row);
127 | }
128 |
129 | /**
130 | * Update the max lenght of rows in the table
131 | *
132 | * @param $table_line
133 | * @return $max_length_rows
134 | */
135 | protected function updateMaxRowLength($table_line)
136 | {
137 | $length = $table_line->getLength();
138 | if($length > $this->max_length_rows)
139 | {
140 | $this->max_length_rows = $length;
141 | }
142 |
143 | return $this->max_length_rows;
144 | }
145 |
146 | /**
147 | * Fill the rest of the row with empty cells
148 | *
149 | * @throws InvalidArgumentException
150 | * @return String $html
151 | */
152 | protected function fillWithEmptyCells($table_row,$tag_row)
153 | {
154 | $html = '';
155 |
156 | if( ! ($table_row instanceof TableLine) )
157 | {
158 | throw new \InvalidArgumentException;
159 | }
160 |
161 | $length_row = $table_row->getLength();
162 | $diff = $this->max_length_rows - $length_row;
163 |
164 | if( $diff > 0 )
165 | {
166 | // add empty cells
167 | foreach( range(1,$diff) as $key)
168 | {
169 | $html.="\t\t\t<{$tag_row}>{$tag_row}>\n";
170 | }
171 | }
172 |
173 | $html.="\t\t
\n";
174 |
175 | return $html;
176 | }
177 |
178 | /**
179 | * Get the classes in string format
180 | * separated by a space
181 | *
182 | * @return String $classes
183 | */
184 | protected function getTableClasses()
185 | {
186 | $classes = "";
187 |
188 | if($this->table_striped)
189 | {
190 | $classes.= "table-striped ";
191 | }
192 | if($this->table_bordered)
193 | {
194 | $classes.= "table-bordered ";
195 | }
196 | if($this->table_hover)
197 | {
198 | $classes.= "table-hover ";
199 | }
200 | if($this->table_condensed)
201 | {
202 | $classes.= "table-condensed ";
203 | }
204 |
205 | $classes.= $this->getTableExtraClassesString();
206 |
207 | return $classes;
208 | }
209 |
210 | /**
211 | * Return the extra classes as concatenated strings
212 | *
213 | * @return String $classes
214 | */
215 | protected function getTableExtraClassesString()
216 | {
217 | $classes_html = '';
218 |
219 | if( ! empty($this->table_extra_classes) )
220 | {
221 | foreach($this->table_extra_classes as $class)
222 | {
223 | $classes_html.= "{$class} ";
224 | }
225 | }
226 |
227 | return $classes_html;
228 | }
229 |
230 | /**
231 | * Add extra classes to the table
232 | *
233 | * @param array $classes
234 | * @return void
235 | * @throws \InvalidArgumentException
236 | */
237 | public function setTableExtraClasses($classes = array())
238 | {
239 | if( ! empty($classes) )
240 | {
241 | // validate classes
242 | foreach($classes as $class)
243 | {
244 | if(str_word_count($class) > 1)
245 | {
246 | throw new \InvalidArgumentException;
247 | }
248 | }
249 | $this->table_extra_classes = $classes;
250 | }
251 | }
252 |
253 | public function getTableExtraClasses()
254 | {
255 | return $this->table_extra_classes;
256 | }
257 |
258 | /**
259 | * Return the table as Html
260 | *
261 | * @return String $html
262 | */
263 | public function getHtml()
264 | {
265 | $html = "";
266 |
267 | $table_classes = $this->getTableClasses();
268 |
269 | if($this->table_responsive)
270 | {
271 | $html.= "\n";
272 | }
273 |
274 | $id_tag = $this->getTagId();
275 |
276 | $html.= "
\n";
277 |
278 | // table header
279 | if( isset($this->header) )
280 | {
281 | $html.= "\t\n";
282 | $html.= $this->header->getHtml();
283 | $html.= $this->fillWithEmptyCells($this->header, $this->header->getTagRow() );
284 | $html.= "\t\n";
285 | }
286 |
287 | // table data
288 | if( isset($this->rows))
289 | {
290 | $html.= "\t\n";
291 | foreach($this->rows as $row)
292 | {
293 | $html.= $row->getHtml();
294 | $html.= $this->fillWithEmptyCells($row, $row->getTagRow() );
295 | }
296 | $html.= "\t\n";
297 | }
298 |
299 | $html.= "
\n";
300 |
301 | if($this->table_responsive)
302 | {
303 | $html.= "
\n";
304 | }
305 |
306 | return $html;
307 | }
308 |
309 | /**
310 | * [getTagId description]
311 | * @return [type] [description]
312 | */
313 | protected function getTagId()
314 | {
315 | return ($this->id) ? "id=\"{$this->id}\"" : "";
316 | }
317 |
318 | public function getHeader()
319 | {
320 | return $this->header;
321 | }
322 |
323 | public function getRows()
324 | {
325 | return $this->rows;
326 | }
327 |
328 | public function __toString()
329 | {
330 | return $this->getHtml();
331 | }
332 |
333 | }
334 |
--------------------------------------------------------------------------------
/src/Jacopo/Bootstrap3Table/TableHeader.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 |
8 | class TableHeader extends TableLine
9 | {
10 | protected $tag_row = "th";
11 | }
--------------------------------------------------------------------------------
/src/Jacopo/Bootstrap3Table/TableLine.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 |
8 | abstract class TableLine
9 | {
10 | public $data;
11 | /**
12 | * Custom css classes for the line
13 | * @var String
14 | */
15 | public $css_classes = array();
16 | protected $data_html;
17 |
18 | /**
19 | * The string inside the tag < > to rappresent the line eg: th, td
20 | * @var String
21 | */
22 | protected $tag_row;
23 |
24 | public function __construct(array $data, array $classes = array() )
25 | {
26 | $this->data = $data;
27 | if(! empty($classes) )
28 | $this->css_classes = $classes;
29 | }
30 |
31 | public function getLength()
32 | {
33 | return count($this->data);
34 | }
35 |
36 | public function getHtml()
37 | {
38 | $tag = $this->getTagRow();
39 | $classes = $this->getHtmlClasses();
40 |
41 | $html = "\t\t\n";
42 |
43 | foreach($this->data as $data)
44 | {
45 | $html.="\t\t\t<{$tag}>$data{$tag}>\n";
46 | }
47 |
48 | $this->data_html = $html;
49 |
50 | return $this->data_html;
51 | }
52 |
53 | /**
54 | * Return the tag for the row: only the part inside the < > tag
55 | * @return String
56 | */
57 | public function getTagRow()
58 | {
59 | return $this->tag_row;
60 | }
61 |
62 | /**
63 | * Return the custom classes as html attribute
64 | * @return String
65 | */
66 | protected function getHtmlClasses()
67 | {
68 | $classes = '';
69 |
70 | if( ! empty($this->css_classes) )
71 | {
72 | // open attribute
73 | $classes.= " class=\"";
74 | foreach($this->css_classes as $class)
75 | {
76 | $classes.="{$class} ";
77 | }
78 | // close attribute
79 | $classes.= "\"";
80 | }
81 |
82 | return $classes;
83 | }
84 |
85 | public function __toString()
86 | {
87 | return $this->getHtml();
88 | }
89 | }
--------------------------------------------------------------------------------
/src/Jacopo/Bootstrap3Table/TableRow.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 |
8 | class TableRow extends TableLine
9 | {
10 | protected $tag_row = "td";
11 | }
--------------------------------------------------------------------------------
/tests/BootstrapTableTest.php:
--------------------------------------------------------------------------------
1 | table = m::mock('Jacopo\Bootstrap3Table\BootstrapTable')->makePartial();
20 | }
21 |
22 | public function tearDown()
23 | {
24 | m::close();
25 | }
26 |
27 | public function testCanIstantiate()
28 | {
29 | $table = new BootstrapTable();
30 | }
31 |
32 | public function testSetConfigSuccess()
33 | {
34 | $config = array(
35 | "table-striped" => true,
36 | "table-bordered" => true,
37 | "table-hover" => false,
38 | "table-condensed" => false,
39 | "table-responsive" => true
40 | );
41 |
42 | $this->table->setConfig($config);
43 | $this->assertEquals(true, $this->table->table_striped);
44 | $this->assertEquals(true, $this->table->table_bordered);
45 | $this->assertEquals(false, $this->table->table_hover);
46 | $this->assertEquals(false, $this->table->table_condensed);
47 | $this->assertEquals(true, $this->table->table_responsive);
48 | }
49 |
50 | /**
51 | * @expectedException InvalidArgumentException
52 | */
53 | public function testSetConfigThrowsInvalidArgumentException()
54 | {
55 | $config = array("invalid"=>true);
56 |
57 | $this->table->setConfig($config);
58 | }
59 |
60 | public function testSetHeaderSuccess()
61 | {
62 | $header = array("first","second");
63 | $expected_header = new TableHeader($header);
64 | $this->table->setHeader($header);
65 |
66 | $this->assertEquals($expected_header, $this->table->getHeader() );
67 | }
68 |
69 | public function testAddRowSuccess()
70 | {
71 | $row1 = array("one","two");
72 | $expected_row1 = new TableRow($row1);
73 | $row2 = array("one","two");
74 | $custom_classes = array("class");
75 | $expected_row2 = new TableRow($row2, $custom_classes);
76 | $expected_array_rows = array(
77 | $expected_row1,
78 | $expected_row2
79 | );
80 | $this->table->addRows($row1);
81 | $this->table->addRows($row2,$custom_classes);
82 | $this->assertEquals($expected_array_rows, $this->table->getRows() );
83 | }
84 |
85 | public function testUpdateMaxRowLength()
86 | {
87 | $row1 = new TableRow( array("one","two") );
88 | $expected_length = 2;
89 |
90 | $length = $this->table->updateMaxRowLength($row1);
91 | $this->assertEquals($expected_length,$length);
92 |
93 | $length = $this->table->updateMaxRowLength($row1);
94 | $this->assertEquals($expected_length,$length);
95 |
96 | $row2 = new TableRow( array("one","two","three") );
97 | $expected_length = 3;
98 |
99 | $length = $this->table->updateMaxRowLength($row2);
100 | $this->assertEquals($expected_length,$length);
101 | }
102 |
103 | public function testFillWithEmpyCells()
104 | {
105 | $row_fill_array = array("one");
106 | $this->table->addRows($row_fill_array);
107 | $row_fill = new TableRow($row_fill_array);
108 |
109 | $expected_filler= "\t\t
\n";
110 | $this->assertEquals($expected_filler, $this->table->fillWithEmptyCells($row_fill,"tag") );
111 |
112 | $row_max_array = array("one","two","three","four");
113 | $this->table->setHeader($row_max_array);
114 |
115 | $row_two_array = array("one","two");
116 | $row_two = new TableRow($row_two_array);
117 | $this->table->addRows($row_two_array);
118 |
119 | $expected_filler = "\t\t\t\n";
120 | $expected_filler.= "\t\t\t\n";
121 | $expected_filler.= "\t\t\n";
122 |
123 | $this->assertEquals($expected_filler, $this->table->fillWithEmptyCells($row_two,"tag") );
124 | }
125 |
126 | /**
127 | * @expectedException InvalidArgumentException
128 | */
129 | public function testFillWithEmpyCellsThrowsInvalidArgumentException()
130 | {
131 | $array = array("notValidArgument");
132 |
133 | $this->table->fillWithEmptyCells($array,"tag");
134 | }
135 |
136 | public function testGetTableClasses()
137 | {
138 | $expected_classes = "";
139 | $this->assertEquals($expected_classes, $this->table->getTableClasses() );
140 |
141 | $config = array(
142 | "table-striped" => true,
143 | "table-bordered" => true,
144 | "table-hover" => true,
145 | "table-condensed" => true
146 | );
147 | $extra_classes = array("table-extra-class");
148 | $this->table->setTableExtraClasses($extra_classes);
149 | $expected_classes = "table-striped table-bordered table-hover table-condensed table-extra-class ";
150 | $this->table->setConfig($config);
151 |
152 | $this->assertEquals($expected_classes, $this->table->getTableClasses() );
153 | }
154 |
155 | public function testGetTagId()
156 | {
157 | $id = $this->table->getTagId();
158 | $this->assertEquals("",$id);
159 |
160 | $this->table->id="id";
161 |
162 | $id = $this->table->getTagId();
163 | $this->assertEquals("id=\"id\"",$id);
164 | }
165 |
166 | public function testGetHtml()
167 | {
168 | // test responsive no header
169 | $header = array("first","second");
170 | $row1 = array("one","two");
171 | $row2 = array("oneOnly");
172 | $this->table->setConfig(array("table-responsive"=>true,"table-hover"=>true));
173 | $this->table->addRows($row1);
174 | $class_row2 = array("row2-class");
175 | $this->table->addRows($row2, $class_row2);
176 | $this->table->id="id1";
177 | $extra_classes = array("test-extra-class");
178 | $this->table->setTableExtraClasses($extra_classes);
179 |
180 | $expected_html = "\n";
181 | $expected_html.= "
\n";
182 | $expected_html.= "\t\n";
183 | $expected_html.= "\t\t\n";
184 | $expected_html.= "\t\t\tone | \n";
185 | $expected_html.= "\t\t\ttwo | \n";
186 | $expected_html.= "\t\t
\n";
187 | $expected_html.= "\t\t\n";
188 | $expected_html.= "\t\t\toneOnly | \n";
189 | $expected_html.= "\t\t\t | \n";
190 | $expected_html.= "\t\t
\n";
191 | $expected_html.= "\t\n";
192 | $expected_html.= "
\n";
193 | $expected_html.= "
\n";
194 |
195 | $this->assertEquals($expected_html,$this->table->getHtml() );
196 |
197 | // test no responsive
198 | $header = array("first","second");
199 | $row1 = array("one","two","three");
200 | $row2 = array("oneOnly");
201 | $this->table->setConfig(array("table-hover"=>true,"table-striped"=>true));
202 | $this->table->setHeader($header);
203 | $this->table->addRows($row1);
204 | $this->table->addRows($row2);
205 |
206 | $expected_html= "\n";
207 | $expected_html.= "\t\n";
208 | $expected_html.= "\t\t\n";
209 | $expected_html.= "\t\t\tfirst | \n";
210 | $expected_html.= "\t\t\tsecond | \n";
211 | $expected_html.= "\t\t\t | \n";
212 | $expected_html.= "\t\t
\n";
213 | $expected_html.= "\t\n";
214 | $expected_html.= "\t\n";
215 | $expected_html.= "\t\t\n";
216 | $expected_html.= "\t\t\tone | \n";
217 | $expected_html.= "\t\t\ttwo | \n";
218 | $expected_html.= "\t\t\tthree | \n";
219 | $expected_html.= "\t\t
\n";
220 | $expected_html.= "\t\t\n";
221 | $expected_html.= "\t\t\toneOnly | \n";
222 | $expected_html.= "\t\t\t | \n";
223 | $expected_html.= "\t\t\t | \n";
224 | $expected_html.= "\t\t
\n";
225 | $expected_html.= "\t\n";
226 | $expected_html.= "
\n";
227 | $expected_html.= "\n";
228 |
229 | }
230 |
231 | public function testSetTableExtraClassesSuccess()
232 | {
233 | $classes = array(
234 | "first",
235 | "second"
236 | );
237 |
238 | $this->table->setTableExtraClasses($classes);
239 | $this->assertEquals($classes, $this->table->getTableExtraClasses() );
240 | }
241 |
242 | /**
243 | * @expectedException InvalidArgumentException
244 | */
245 | public function testSetTableExtraClassesThrowsInvalidArgumentException()
246 | {
247 | $classes = array(
248 | "first invalid argument",
249 | );
250 |
251 | $this->table->setTableExtraClasses($classes);
252 | }
253 |
254 | public function testGetTableExtraClassesString()
255 | {
256 | $classes = array(
257 | "first",
258 | "second"
259 | );
260 |
261 | $this->table->setTableExtraClasses($classes);
262 | $expected_html = "first second ";
263 | $this->assertEquals($expected_html, $this->table->getTableExtraClassesString() );
264 | }
265 | }
266 |
--------------------------------------------------------------------------------
/tests/TableLineTest.php:
--------------------------------------------------------------------------------
1 | line = m::mock('Jacopo\Bootstrap3Table\TableLine')->makePartial();
17 | }
18 |
19 | public function tearDown()
20 | {
21 | m::close();
22 | }
23 |
24 | public function testGetLenghWorks()
25 | {
26 | $args = array("first","second");
27 | $this->line->data = $args;
28 |
29 | $length = $this->line->getLength();
30 | $this->assertEquals(2, $length);
31 | }
32 |
33 | public function testGetHtmlSuccess()
34 | {
35 | $this->line->shouldReceive('getTagRow')
36 | ->once()
37 | ->andReturn('tag');
38 |
39 | $args = array("first","second");
40 | $this->line->data = $args;
41 | $this->line->css_classes = array("class");
42 | $html = $this->line->getHtml();
43 |
44 | $expected_html=<<\n\t\t\tfirst\n\t\t\tsecond\n
46 | STR;
47 | $this->assertEquals($expected_html, $html);
48 | }
49 |
50 | public function testGetHtmlClasses()
51 | {
52 | $classes = array(
53 | "test-class1",
54 | "test-class2"
55 | );
56 |
57 | $this->line->css_classes = $classes;
58 | $expected_html = " class=\"test-class1 test-class2 \"";
59 | $this->assertEquals($expected_html, $this->line->getHtmlClasses() );
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - 5.3
5 | - 5.4
6 | - 5.5
7 |
8 | before_script:
9 | - curl -s http://getcomposer.org/installer | php
10 | - php composer.phar install --dev
11 |
12 | script: phpunit
--------------------------------------------------------------------------------