datatable.test.php
来自「一款可以和GOOGLE媲美的开源统计系统,运用AJAX.功能强大. 无色提示:」· PHP 代码 · 共 904 行 · 第 1/3 页
PHP
904 行
<?phpif(!defined("PATH_TEST_TO_ROOT")) { define('PATH_TEST_TO_ROOT', '..');}if(!defined('CONFIG_TEST_INCLUDED')){ require_once PATH_TEST_TO_ROOT ."/../tests/config_test.php";}require_once 'DataTable.php';class Test_Piwik_DataTable extends UnitTestCase{ function __construct( $title = '') { parent::__construct( $title ); } public function setUp() { } public function tearDown() { } /** * General tests that tries to test the normal behaviour of DataTable * * We create some tables, add rows, some of the rows link to sub tables * * Then we serialize everything, and we check that the unserialize give the same object back */ function test_general() { /* * create some fake tables to make sure that the serialized array of the first TABLE * does not take in consideration those tables */ $useless1 = new Piwik_DataTable; $useless1->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 13,),)); /* * end fake tables */ /* * MAIN TABLE */ $table = new Piwik_DataTable; $subtable = new Piwik_DataTable; $idtable = $table->getId(); $idsubtable = $subtable->getId(); /* * create some fake tables to make sure * that the serialized array of the first TABLE * does not take in consideration those tables * (yes theres a story of an ID given by some DataTable_Manager * we check this module is not messing around) */ $useless2 = new Piwik_DataTable; $useless1->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 8487,),)); $useless3 = new Piwik_DataTable; $useless3->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 8487,),)); /* * end fake tables */ $row = array(Piwik_DataTable_Row::COLUMNS => array( 0 => 1554, 1 => 42, 2 => 657,3 => 155744,), Piwik_DataTable_Row::METADATA => array('logo' => 'test.png')); $row = new Piwik_DataTable_Row($row); $table->addRow($row); $table->addRowFromArray(array( Piwik_DataTable_Row::COLUMNS => array( 0 => 1554,1 => 42,), Piwik_DataTable_Row::METADATA => array('url' => 'piwik.org'))); $table->addRowFromArray(array( Piwik_DataTable_Row::COLUMNS => array( 0 => 787877888787,), Piwik_DataTable_Row::METADATA => array('url' => 'OUPLA ADDED'), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subtable)); /* * SUB TABLE */ $row = array( Piwik_DataTable_Row::COLUMNS => array( 0 => 1554,), Piwik_DataTable_Row::METADATA => array('searchengine' => 'google'), ); $subtable->addRowFromArray($row); $row = array( Piwik_DataTable_Row::COLUMNS => array( 0 => 84894,), Piwik_DataTable_Row::METADATA => array('searchengine' => 'yahoo'), ); $subtable->addRowFromArray($row); $row = array( Piwik_DataTable_Row::COLUMNS => array( 0 => 4898978989,), Piwik_DataTable_Row::METADATA => array('searchengine' => 'ask'), ); $subtable->addRowFromArray($row); /* * SUB SUB TABLE */ $subsubtable = new Piwik_DataTable; $subsubtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 245), Piwik_DataTable_Row::METADATA => array('yes' => 'subsubmetadata1'),) ); $subsubtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array( 13,), Piwik_DataTable_Row::METADATA => array('yes' => 'subsubmetadata2'),) ); $row = array( Piwik_DataTable_Row::COLUMNS => array( 0 => 666666666666666,), Piwik_DataTable_Row::METADATA => array('url' => 'NEW ROW ADDED'), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subsubtable); $subtable->addRowFromArray($row); $idsubsubtable = $subsubtable->getId(); $serialized = ($table->getSerialized()); $this->assertEqual(array_keys($serialized), array($idsubsubtable,$idsubtable,0)); $tableAfter = new Piwik_DataTable; $tableAfter->loadFromSerialized($serialized[0]); $this->assertEqual($table->getRows(),$tableAfter->getRows()); $subsubtableAfter = new Piwik_DataTable; $subsubtableAfter->loadFromSerialized($serialized[$idsubsubtable]); $this->assertEqual($subsubtable->getRows(),$subsubtableAfter->getRows()); $this->assertEqual($table, Piwik_DataTable_Manager::getInstance()->getTable($idtable)); $this->assertEqual($subsubtable, Piwik_DataTable_Manager::getInstance()->getTable($idsubsubtable)); } /** * we test the count rows and the count rows recursive version * on a Simple array (1 level only) */ function test_countRowsSimple() { $table = new Piwik_DataTable; $idcol = Piwik_DataTable_Row::COLUMNS; $rows = array( array( $idcol => array('label'=>'google')), array( $idcol => array('label'=>'ask')), array( $idcol => array('label'=>'piwik')), array( $idcol => array('label'=>'yahoo')), array( $idcol => array('label'=>'amazon')), array( $idcol => array('label'=>'238975247578949')), array( $idcol => array('label'=>'Q*(%&*("$&%*(&"$*")"))'))); $table->loadFromArray( $rows ); $this->assertEqual( $table->getRowsCount(), count($rows)); $this->assertEqual( $table->getRowsCountRecursive(), count($rows)); } /** * we test the count rows and the count rows recursive version * on a Complex array (rows with 2 and 3 levels only) * * the recursive count returns * the sum of the number of rows of all the subtables * + the number of rows in the parent table */ function test_countRowsComplex() { $idcol = Piwik_DataTable_Row::COLUMNS; $idsubtable = Piwik_DataTable_Row::DATATABLE_ASSOCIATED; // table to go in the SUB table of RoW1 $tableSubOfSubOfRow1 = new Piwik_DataTable; $rows1sub = array( array( $idcol => array('label'=>'google')), array( $idcol => array('label'=>'google78')), array( $idcol => array('label'=>'googlaegge')), array( $idcol => array('label'=>'gogeoggle')), array( $idcol => array('label'=>'goaegaegaogle')), array( $idcol => array('label'=>'ask')), array( $idcol => array('label'=>'238975247578949')), ); $tableSubOfSubOfRow1->loadFromArray( $rows1sub ); // table to go in row1 $tableSubOfRow1 = new Piwik_DataTable; $rows1 = array( array( $idcol => array('label'=>'google'), $idsubtable =>$tableSubOfSubOfRow1), array( $idcol => array('label'=>'ask')), array( $idcol => array('label'=>'238975247578949')), ); $tableSubOfRow1->loadFromArray( $rows1 ); // table to go in row2 $tableSubOfRow2 = new Piwik_DataTable; $rows2 = array( array( $idcol => array('label'=>'google')), array( $idcol => array('label'=>'ask')), array( $idcol => array('label'=>'238975247578949')), array( $idcol => array('label'=>'agaegaesk')), array( $idcol => array('label'=>'23g 8975247578949')), ); $tableSubOfRow2->loadFromArray( $rows2 ); // main parent table $table = new Piwik_DataTable; $rows = array( array( $idcol => array('label'=>'row1')), array( $idcol => array('label'=>'row2'), $idsubtable => $tableSubOfRow1), array( $idcol => array('label'=>'row3'), $idsubtable => $tableSubOfRow2), ); $table->loadFromArray( $rows ); $this->assertEqual( $table->getRowsCount(), count($rows)); $countAllRows = count($rows)+count($rows1)+count($rows2) + count($rows1sub); $this->assertEqual( $table->getRowsCountRecursive(),$countAllRows); } /** * Simple test of the DataTable_Row */ function test_Row() { $columns = array('test_column'=> 145, 092582495 => new Piwik_Timer, 'super'=>array('this column has an array value, amazing')); $metadata = array('logo'=> 'piwik.png', 'super'=>array('this column has an array value, amazing')); $arrayRow = array( Piwik_DataTable_Row::COLUMNS => $columns, Piwik_DataTable_Row::METADATA => $metadata, 'fake useless key'=>38959, 43905724897=>'value'); $row = new Piwik_DataTable_Row($arrayRow); $this->assertEqual($row->getColumns(), $columns); $this->assertEqual($row->getMetadata(), $metadata); $this->assertEqual($row->getIdSubDataTable(), null); } /** * Simple test of the DataTable_Row */ function test_sumRow() { $columns = array('test_int'=> 145, 'test_float'=> 145.5, 'test_float3'=> 1.5, 'test_stringint'=> "145", "test" => 'string fake', 'super'=>array('this column has an array value, amazing') ); $metadata = array('logo'=> 'piwik.png', 'super'=>array('this column has an array value, amazing')); $arrayRow = array( Piwik_DataTable_Row::COLUMNS => $columns, Piwik_DataTable_Row::METADATA => $metadata, 'fake useless key'=>38959, 43905724897=>'value'); $row1 = new Piwik_DataTable_Row($arrayRow); $columns2 = array('test_int'=> 5, 'test_float'=> 4.5, 'test_float2'=> 14.5, 'test_stringint'=> "5", 0925824 => 'toto', 'super'=>array('this column has geagaean array value, amazing')); $finalRow = new Piwik_DataTable_Row( array(Piwik_DataTable_Row::COLUMNS => $columns2)); $finalRow->sumRow($row1); $columnsWanted = array('test_int'=> 150, 'test_float'=> 150.0, 'test_float2'=> 14.5, 'test_float3'=> 1.5, 'test_stringint'=> "150", //add also strings!! 'super'=>array('this column has geagaean array value, amazing'), 0925824 => 'toto', ); $rowWanted = new Piwik_DataTable_Row( array(Piwik_DataTable_Row::COLUMNS => $columnsWanted)); $this->assertTrue( Piwik_DataTable_Row::isEqual($rowWanted, $finalRow)); } /** * Test serialize with an infinite recursion (a row linked to a table in the parent hierarchy) * After 100 recursion must throw an exception */ function test_serializeWithInfiniteRecursion() { $table = new Piwik_DataTable;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?