Вход Регистрация
Файл: cobisja/BootHelp/tests/src/TableTest.php
Строк: 341
<?php

/**
 * The MIT License
 *
 * Copyright 2015 cobisja.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

namespace BootHelpTests;

use 
cobisjaBootHelpTable;

class 
TableTest extends PHPUnit_Framework_TestCase
{
    public function 
testWithEmptyContent()
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
         *     <tbody>
         *     </tbody>
         * </table>
         */
        
        
$table = new Table([]);
        
$html $table->getHtml();
        
        
$this->assertTrue($html->isA('table', ['class'=>'table']));
        
$this->assertEquals(1$html->numberOfChildren());
        
$this->assertTrue($html->getChild(0)->isA('tbody'));
        
$this->assertEquals(0$html->getChild(0)->numberOfChildren());
    }
    
    
/**
     * @dataProvider getSimpleTableFixture
     */
    
public function testSimpleTableWithNoOptions($table_content)
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
         *     <tbody>
         *         <tr><td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td></tr>
         *         <tr><td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td></tr>
         *         <tr><td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td></tr>
         *     </tbody>
         * </table>
         */
        
        
$table = new Table($table_content);
        
$html $table->getHtml();
        
$this->validateTableWithNoHeading($html$html->getChild(0), $table_content);
    }

    
/**
     * @dataProvider getSimpleTableFixture
     */
    
public function testSimpleTableWithGlobalOptions($table_content)
    {
        
/**
         * It should generates:
         * 
         * <table class="table table-striped en" id="my-table">
         *     <caption>Optional table caption.</caption>
         *     <tbody>
         *         <tr><td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td></tr>
         *         <tr><td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td></tr>
         *         <tr><td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td></tr>
         *     </tbody>
         * </table>
         */
        
        
$table = new Table(
            
$table_content,
            [
'id'=>'my-table''striped'=>true'class'=>'en''caption'=>'Optional table caption.']
        );
        
$html $table->getHtml();
        
$this->validateTableWithNoHeading($html$html->getChild(1), $table_content);
        
$this->assertTrue($html->hasAttribute('class', ['table-striped''en']));
        
$this->assertTrue($html->hasAttribute('id''my-table'));
        
$this->assertTrue($html->hasAChildOfType('caption'));
    }
    
    
/**
     * @dataProvider getSimpleTableFixture
     */
    
public function testSimpleTableWithResponsiveOption($table_content)
    {
        
/**
         * It should generates:
         * 
         * <div class="table-responsive">
         *     <table class="table">
         *         <tbody>
         *             <tr><td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td></tr>
         *             <tr><td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td></tr>
         *             <tr><td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td></tr>
         *         </tbody>
         *     </table>
         * </div>
         */
        
        
$table = new Table($table_content, ['responsive'=>true]);
        
$html $table->getHtml();
        
        
$this->assertTrue($html->isA('div', ['class'=>'table-responsive']));
        
$this->validateTableWithNoHeading($html->getChild(0), $html->getChild(0)->getChild(0), $table_content);
    }
    
    
/**
     * @dataProvider getSimpleTableFixture
     */
    
public function testTableWithHeader($table_content)
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
         *     <thead>
         *         <tr>
         *             <th>#</th><th>First Name</th><th>Last Name</th><th>Username</th>
         *         </tr>
         *     </thead>
         *     <tbody>
         *         <tr>
         *             <td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td>
         *         </tr>
         *         <tr>
         *             <td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td>
         *         </tr>
         *         <tr>
         *             <td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td>
         *         </tr>
         *     </tbody>
         * </table>
         */
        
        
$table = new Table($table_content, ['header'=>['#''First Name''Last Name''Username']]);
        
$html $table->getHtml();
        
$this->validateTableWithHeading($html$table_content);
    }
    
    
/**
     * @dataProvider getSimpleTableFixture()
     */
    
public function testTableWithCustomizedHeadAndBody($table_content)
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
         *     <thead class="en-thead" "data-js"=1>
         *         <tr>
         *             <th>#</th><th>First Name</th><th>Last Name</th><th>Username</th>
         *         </tr>
         *     </thead>
         *     <tbody class="en-tbody" "data-js"=2>
         *         <tr>
         *             <td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td>
         *         </tr>
         *         <tr>
         *             <td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td>
         *         </tr>
         *         <tr>
         *             <td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td>
         *         </tr>
         *     </tbody>
         * </table>
         */
        
        
$table = new Table(
            
$table_content,
            [
                
'header'=>['#''First Name''Last Name''Username'],
                
'header_options'=>['class'=>"en-thead"'data-js'=>1],
                
'body_options'=>['class'=>'en-tbody''data-js'=>2]
            ]
        );
        
$html $table->getHtml();
        
$this->validateTableWithHeading($html$table_content);
        
$this->assertTrue($html->getChild(0)->isA('thead', ['class'=>'en-thead''data-js'=>1]));
        
$this->assertTrue($html->getChild(1)->isA('tbody', ['class'=>'en-tbody''data-js'=>2]));
    }

    
/**
     * @dataProvider getSimpleTableFixture
     */
    
public function testTableWithHeaderAsFirstContentRow($table_content)
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
         *     <thead>
         *         <tr>
         *             <th>#</th><th>First Name</th><th>Last Name</th><th>Username</th>
         *         </tr>
         *     </thead>
         *     <tbody>
         *         <tr>
         *             <td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td>
         *         </tr>
         *         <tr>
         *             <td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td>
         *         </tr>
         *         <tr>
         *             <td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td>
         *         </tr>
         *     </tbody>
         * </table>
         */
        
        
$header = ['#''First Name''Last Name''Username'];
        
array_unshift($table_content$header);
        
$table = new Table($table_content, ['header'=>true]);
        
$html $table->getHtml();
        
$this->validateTableWithHeading($html$table_contenttrue);
    }
    
    
/**
     * @dataProvider getSimpleTableFixture
     */
    
public function testTableWithCustomizedGlobalRowOptions($table_content)
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
         *     <tbody>
         *         <tr class="en" data-js=1>
         *             <td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td>
         *         </tr>
         *         <tr class="en" data-js=1>
         *             <td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td>
         *         </tr>
         *         <tr class="en" data-js=1>
         *             <td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td>
         *         </tr>
         *     </tbody>
         * </table>
         */
        
        
$table = new Table(
            
$table_content,
            [
'header'=>['#''First Name''Last Name''Username'], 'rows_options'=>['class'=>'en''data-js'=>1]]
        );
        
$html $table->getHtml();
        
$this->validateTableWithHeading($html$table_content);
        
        foreach (
$html->getChild(1)->getChildren() as $row) {
            
$this->assertTrue($row->isA('tr', ['class'=>'en''data-js'=>1]));
        }
    }
    
    public function 
testTableWithCustomizedLocalRowOptions()
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
        *     <thead>
         *         <tr>
         *             <th>#</th><th>First Name</th><th>Last Name</th><th>Username</th>
         *         </tr>
         *     </thead>
         *     <tbody>
         *         <tr class="en" data-js=1>
         *             <td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td>
         *         </tr>
         *         <tr class="danger">
         *             <td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td>
         *         </tr>
         *         <tr class="en" data-js=1>
         *             <td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td>
         *         </tr>
         *     </tbody>
         * </table>
         */
        
        
$table_content = [
            [
1'Mark''Otto''@mdo''class'=>'en''data-js'=>1],
            [
2'Jacob''Thornton''@fat''context'=>'danger'],
            [
3'Larry''the Bird''@twitter']
        ];
        
        
$table = new Table(
            
$table_content,
            [
'header'=>['#''First Name''Last Name''Username']]
        );
        
        
$html $table->getHtml();
        unset(
$table_content[0]['class']);
        unset(
$table_content[0]['data-js']);
        unset(
$table_content[1]['context']);
        
$this->validateTableWithHeading($html$table_content);
        
$this->assertTrue($html->getChild(1)->getChild(0)->isA('tr', ['class'=>'en''data-js'=>1]));
        
$this->assertTrue($html->getChild(1)->getChild(1)->isA('tr', ['class'=>'danger']));
    }
    
    public function 
testTableWithLocalRowOptionsOverwritesGlobalRowOptions()
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
        *     <thead>
         *         <tr>
         *             <th>#</th><th>First Name</th><th>Last Name</th><th>Username</th>
         *         </tr>
         *     </thead>
         *     <tbody>
         *         <tr data-js=1>
         *             <td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td>
         *         </tr>
         *         <tr data-js=2">
         *             <td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td>
         *         </tr>
         *         <tr data-js=1>
         *             <td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td>
         *         </tr>
         *     </tbody>
         * </table>
         */
        
        
$table_content = [
            [
1'Mark''Otto''@mdo'],
            [
2'Jacob''Thornton''@fat''data-js'=>2],
            [
3'Larry''the Bird''@twitter']
        ];
        
        
$table = new Table(
            
$table_content,
            [
'header'=>['#''First Name''Last Name''Username'], 'rows_options'=>['data-js'=>1]]
        );
        
        
$html $table->getHtml();
        unset(
$table_content[1]['data-js']);
        
$this->validateTableWithHeading($html$table_content);
        
$this->assertTrue($html->getChild(1)->getChild(0)->isA('tr', ['data-js'=>1]));
        
$this->assertTrue($html->getChild(1)->getChild(1)->isA('tr', ['data-js'=>2]));
        
$this->assertTrue($html->getChild(1)->getChild(2)->isA('tr', ['data-js'=>1]));
    }

    public function 
testTableCustomizedCellOptions()
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
         *     <thead>
         *         <tr>
         *             <th>#</th><th>First Name</th><th>Last Name</th><th>Username</th>
         *         </tr>
         *     </thead>
         *     <tbody>
         *         <tr>
         *             <th scope="row">1</th>
         *             <td>Mark</td>
         *             <td>Otto</td>
         *             <td>@mdo</td>
         *         </tr>
         *         <tr>
         *             <td>2</td>
         *             <td>Jacob</td>
         *             <td>Thornton</td>
         *             <td>@fat</td>
         *         </tr>
         *         <tr>
         *             <td>3</td>
         *             <td>Larry</td>
         *             <td>the Bird</td>
         *             <td>@twitter</td>
         *         </tr>
         *     </tbody>
         * </table>
         */
        
        
$table_content = [
            [[
1'scope'=>'row'], 'Mark''Otto''@mdo'],
            [
2'Jacob''Thornton''@fat''data-js'=>2],
            [
3'Larry''the Bird''@twitter']
        ];
        
$table = new Table(
            
$table_content,
            [
'header'=>['#''First Name''Last Name''Username'], 'rows_options'=>['data-js'=>1]]
        );
        
        
$html $table->getHtml();
        
$this->validateTableWithHeading($html$table_content);
        
$this->assertNotTrue($html->getChild(1)->getChild(0)->hasAttribute('scope''row'));
        
$this->assertTrue($html->getChild(1)->getChild(0)->getChild(0)->isA('th', ['scope'=>'row']));
    }
    
    public function 
testTableWithLocalCellOptionsOverwritesLocalRowOptions()
    {
        
/**
         * It should generates:
         * 
         * <table class="table">
         *     <thead>
         *         <tr>
         *             <th>#</th><th>First Name</th><th>Last Name</th><th>Username</th>
         *         </tr>
         *     </thead>
         *     <tbody>
         *         <tr class="success">
         *             <td>1</td>
         *             <td>Mark</td>
         *             <td>Otto</td>
         *             <td class="danger">@mdo</td>
         *         </tr>
         *         <tr>
         *             <td>2</td>
         *             <td>Jacob</td>
         *             <td>Thornton</td>
         *             <td>@fat</td>
         *         </tr>
         *         <tr>
         *             <td>3</td>
         *             <td>Larry</td>
         *             <td>the Bird</td>
         *             <td>@twitter</td>
         *         </tr>
         *     </tbody>
         * </table>
         */
        
        
$table_content = [
            [
1'Mark''Otto', ['@mdo''context'=>'danger'], 'context'=>'success'],
            [
2'Jacob''Thornton''@fat''data-js'=>2],
            [
3'Larry''the Bird''@twitter']
        ];
        
$table = new Table(
            
$table_content,
            [
'header'=>['#''First Name''Last Name''Username']]
        );
        
        
$html $table->getHtml();
        unset(
$table_content[0]['context']);
        
$this->validateTableWithHeading($html$table_content);
        
$this->assertTrue($html->getChild(1)->getChild(0)->hasAttribute('class''success'));
        
$this->assertTrue($html->getChild(1)->getChild(0)->getChild(3)->isA('td', ['class'=>'danger']));
    }
    
    public function 
testTableWithContentViaClosure()
    {
        
/**
         * It should generates:
         * 
         * <table class="table" id="my-table">
         *     <tbody>
         *         <tr><td>1</td><td>Mark</td><td>Otto</td><td>@mdo</td></tr>
         *         <tr><td>2</td><td>Jacob</td><td>Thornton</td><td>@fat</td></tr>
         *         <tr><td>3</td><td>Larry</td><td>the Bird</td><td>@twitter</td></tr>
         *     </tbody>
         * </table>
         */
 
        
$table = new Table(['id'=>'my-table'], function () {
            return [
                [
1'Mark''Otto''@mdo'],
                [
2'Jacob''Thornton''@fat'],
                [
3'Larry''the Bird''@twitter']
            ];
        });

        
$html $table->getHtml();
        
        
$this->assertTrue($html->isA('table'));
        
$this->assertTrue($html->hasAttribute('class''table'));
        
$this->assertTrue($html->hasAttribute('id''my-table'));
        
$this->assertNotTrue($html->hasAChildOfType('thead'));
        
$this->assertTrue($html->hasAChildOfType('tbody'));
        
$this->assertEquals(3$html->getChild(0)->numberOfChildren());
    }
    
    private function 
validateTableWithNoHeading($html$body$content)
    {
        
$this->assertTrue($html->isA('table'));
        
$this->assertTrue($html->hasAttribute('class''table'));
        
$this->assertNotTrue($html->hasAChildOfType('thead'));
        
$this->assertTrue($html->hasAChildOfType('tbody'));
        
$this->assertEquals(count($content), $body->numberOfChildren());
    }
    
    private function 
validateTableWithHeading($html$content$first_row_as_heading false)
    {
        
$this->assertTrue($html->isA('table'));
        
$this->assertTrue($html->hasAttribute('class''table'));
        
$this->assertEquals(2$html->numberOfChildren());
        
$this->assertTrue($html->hasAChildOfType('thead'));
        
$this->assertTrue($html->hasAChildOfType('tbody'));
        
$this->assertEquals(count($content[0]), $html->getChild(0)->getChild(0)->numberOfChildren());
        
$this->assertEquals(
            !
$first_row_as_heading count($content) : count($content) - 1,
            
$html->getChild(1)->numberOfChildren()
        );
    }
    
    public function 
getSimpleTableFixture()
    {
        return [
            [
                [
                    [
1'Mark''Otto''@mdo'],
                    [
2'Jacob''Thornton''@fat'],
                    [
3'Larry''the Bird''@twitter']
                ]
            ]
        ];
    }
}
Онлайн: 1
Реклама