pager_test.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 477 行 · 第 1/2 页

PHP
477
字号
<?php// $Id: pager_test.php,v 1.15 2005/09/27 07:49:14 quipo Exp $require_once 'simple_include.php';require_once 'pager_include.php';class TestOfPager extends UnitTestCase {    var $pager;    var $baseurl;    function TestOfPager($name='Test of Pager') {        $this->UnitTestCase($name);    }    function setUp() {        $options = array(            'itemData' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),            'perPage'  => 5,        );        $this->pager = Pager::factory($options);        $this->baseurl = substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));    }    function tearDown() {        unset($this->pager);    }    function testCurrentPageID () {        $this->assertEqual(1, $this->pager->getCurrentPageID());    }    function testNextPageID () {        $this->assertEqual(2, $this->pager->getNextPageID());    }    function testPrevPageID () {        $this->assertEqual(false, $this->pager->getPreviousPageID());    }    function testNumItems () {        $this->assertEqual(10, $this->pager->numItems());    }    function testNumPages () {        $this->assertEqual(2, $this->pager->numPages());    }    function testFirstPage () {        $this->assertEqual(true, $this->pager->isFirstPage());    }    function testLastPage () {        $this->assertEqual(false, $this->pager->isLastPage());    }    function testLastPageComplete () {        $this->assertEqual(true, $this->pager->isLastPageComplete());    }    function testOffsetByPageId1() {        $this->assertEqual(array(1, 5), $this->pager->getOffsetByPageId(1));    }    function testOffsetByPageId2() {        $this->assertEqual(array(6, 10), $this->pager->getOffsetByPageId(2));    }    function testOffsetByPageId_outOfRange() {        $this->assertEqual(array(0, 0), $this->pager->getOffsetByPageId(20));    }    function testGetPageData() {        $this->assertEqual(array(0=>1, 1=>2, 2=>3, 3=>4, 4=>5), $this->pager->getPageData());    }    function testGetPageData2() {        $this->assertEqual(array(5=>6, 6=>7, 7=>8, 8=>9, 9=>10), $this->pager->getPageData(2));    }    function testGetPageData_OutOfRange() {        $this->assertEqual(false, $this->pager->getPageData(3));    }    function testSelectBox() {        $selectBox  = '<select name="'.$this->pager->_sessionVar.'">';        $selectBox .= '<option value="5" selected="selected">5</option>';        $selectBox .= '<option value="10">10</option>';        $selectBox .= '<option value="15">15</option>';        $selectBox .= '</select>';        $this->assertEqual($selectBox, $this->pager->getPerPageSelectBox(5, 15, 5));    }    function testSelectBoxWithString() {        $selectBox  = '<select name="'.$this->pager->_sessionVar.'">';        $selectBox .= '<option value="5" selected="selected">5 bugs</option>';        $selectBox .= '<option value="10">10 bugs</option>';        $selectBox .= '<option value="15">15 bugs</option>';        $selectBox .= '</select>';        $this->assertEqual($selectBox, $this->pager->getPerPageSelectBox(5, 15, 5, false, '%d bugs'));    }    function testSelectBoxWithShowAll() {        $selectBox  = '<select name="'.$this->pager->_sessionVar.'">';        $selectBox .= '<option value="3">3</option>';        $selectBox .= '<option value="4">4</option>';        $selectBox .= '<option value="5" selected="selected">5</option>';        $selectBox .= '<option value="6">6</option>';        $selectBox .= '<option value="10">10</option>';        $selectBox .= '</select>';        $this->assertEqual($selectBox, $this->pager->getPerPageSelectBox(3, 6, 1, true));    }    function testSelectBoxWithShowAllAndText() {        $this->pager->_showAllText = 'Show All';        $selectBox  = '<select name="'.$this->pager->_sessionVar.'">';        $selectBox .= '<option value="3">3 bugs</option>';        $selectBox .= '<option value="4">4 bugs</option>';        $selectBox .= '<option value="5" selected="selected">5 bugs</option>';        $selectBox .= '<option value="6">6 bugs</option>';        $selectBox .= '<option value="10">Show All</option>';        $selectBox .= '</select>';        $this->assertEqual($selectBox, $this->pager->getPerPageSelectBox(3, 6, 1, true, '%d bugs'));    }    function testSelectBoxWithShowAllWithExtraAttribs() {        $this->pager->_showAllText = 'Show All';        $selectBox  = '<select name="'.$this->pager->_sessionVar.'" onmouseover="doSth">';        $selectBox .= '<option value="3">3 bugs</option>';        $selectBox .= '<option value="4">4 bugs</option>';        $selectBox .= '<option value="5" selected="selected">5 bugs</option>';        $selectBox .= '<option value="6">6 bugs</option>';        $selectBox .= '<option value="10">Show All</option>';        $selectBox .= '</select>';        $params = array('optionText' => '%d bugs', 'attributes' => 'onmouseover="doSth"');        $this->assertEqual($selectBox, $this->pager->getPerPageSelectBox(3, 6, 1, true, $params));    }    function testSelectBoxInvalid() {        $err = $this->pager->getPerPageSelectBox(5, 15, 5, false, '%s bugs');        $this->assertEqual(ERROR_PAGER_INVALID_PLACEHOLDER, $err->getCode());    }    function testAppendInvalid() {        $options = array(            'totalItems' => 10,            'append'     => false,            'fileName'   => 'invalidFileName'        );        $err =& Pager::factory($options);  //ERROR_PAGER_INVALID_USAGE        $this->assertError();    }    function testAppendValid() {        $options = array(            'totalItems' => 10,            'append'     => false,            'fileName'   => 'valid_%d_FileName'        );        $err =& Pager::factory($options);        $this->assertNoErrors();    }    function testEscapeEntities() {        //encode special chars        $options = array(            'extraVars' => array(                'request' => array('aRequest'),                'escape'    => '漩%<>+',            ),            'perPage' => 5,        );        $this->pager =& Pager::factory($options);        //$expected = '?request[]=aRequest&amp;escape=&auml;&ouml;%&lt;&gt;+&amp;pageID=';        //$this->assertEqual($expected, $this->pager->_getLinksUrl());        $expected = 'request%5B0%5D=aRequest&amp;escape=%E4%F6%25%3C%3E%2B';        $rendered = $this->pager->_renderLink('', '');        preg_match('/href="(.*)"/U', $rendered, $matches);        $actual = str_replace($_SERVER['PHP_SELF'].'?', '', $matches[1]);        $this->assertEqual($expected, $actual);        //don't encode slashes        $options = array(            'extraVars' => array(                'request' => 'cat/subcat',            ),            'perPage' => 5,        );        $this->pager =& Pager::factory($options);        //$expected = '?request=cat/subcat&amp;pageID=';        //$this->assertEqual($expected, $this->pager->_getLinksUrl());        $expected = '<a href="'.$_SERVER['PHP_SELF'].'?request=cat/subcat" title=""></a>';        $actual = $this->pager->_renderLink('', '');        $this->assertEqual($expected, $actual);    }    function testMultibyteStrings() {        $options = array(            'extraVars' => array(                'test' => '&#27979;&#35797;',            ),            'perPage' => 5,        );        $this->pager =& Pager::factory($options);        //$expected = '<a href="'.$_SERVER['PHP_SELF'].'?test=&#27979;&#35797;" title=""></a>';        $rendered = $this->pager->_renderLink('', '');        preg_match('/href="(.*)"/U', $rendered, $matches);        $actual = str_replace($_SERVER['PHP_SELF'].'?test=', '', $matches[1]);        $this->assertEqual(urlencode($options['extraVars']['test']), $actual);    }    function testCurrentPage() {        $options = array(            'itemData'    => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),            'perPage'     => 2,            'currentPage' => 2,        );        $this->pager =& Pager::factory($options);        $this->assertEqual(3, $this->pager->getNextPageID());        $this->assertEqual(1, $this->pager->getPreviousPageID());        $this->assertEqual(2, $this->pager->_currentPage);    }    function testArrayExtraVars() {        $arr = array(            'apple',            'orange',        );        $options = array(            'itemData'    => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),            'perPage'     => 5,            'extraVars'   => array('arr' => $arr, 'no' => 'test'),        );        $this->pager =& Pager::factory($options);        /*        //old        $expected = '?arr[0]=apple&amp;arr[1]=orange&amp;pageID=';        $this->assertEqual($expected, $this->pager->_getLinksUrl());        */        $expected = $options['extraVars'];        $this->assertEqual($expected, $this->pager->_getLinksData());        $expected = '<a href="'.$_SERVER['PHP_SELF'].'?arr%5B0%5D=apple&amp;arr%5B1%5D=orange&amp;no=test&amp;pageID=2" title=""></a>';        $actual = $this->pager->_renderLink('', '');        $this->assertEqual($expected, $actual);    }    function testExcludeVars() {        $arr = array(            'apple',            'orange',        );        $options = array(            'itemData'    => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),            'perPage'     => 5,            'extraVars'   => array('arr' => $arr, 'no' => 'test'),            'excludeVars' => array('no'),        );        $this->pager =& Pager::factory($options);        $expected = array(            'arr' => array(                0 => 'apple',                1 => 'orange'            ),        );        $actual = $this->pager->_getLinksData();        $this->assertEqual($expected, $this->pager->_getLinksData());        $expected = '<a href="'.$_SERVER['PHP_SELF'].'?arr%5B0%5D=apple&amp;arr%5B1%5D=orange&amp;pageID=2" title=""></a>';

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?