サンプルコード
class Mobilelint
{
//略
static function isSoftbankTableTagNestOverflow(SimpleXMLElement $xml, $content)
{
$result = $xml->xpath('//table/descendant::table/descendant::table/descendant::table/descendant::table/descendant::table');
return (count($result) > 0 && $result !== false) ? true : false;
}
static function isSoftbankOlOrUlTagChildOverflow(SimpleXMLElement $xml, $content)
{
$result = $xml->xpath('//ol/descendant::li[100]');
if(count($result) > 0 && $result !== false) return true;
$result = $xml->xpath('//ul/descendant::li[100]');
return (count($result) > 0 && $result !== false) ? true : false;
}
//略
}
- xpath「descendant::」は、子ノードだけでなく、孫ノード以降も取得できる構文
- xpath「li[100]」は、100番目のリスト要素を取得できる構文
- ネストの制限99個 → 100番目の要素があるものはエラー
- 「return (count($result) > 0 && $result !== false)」は、$xml->xpathが「オブジェクト」または「false」を返す場面があったため
テストケース
//ライブラリパスを設定したファイル
require_once 'path.php';
//PHPUnitを呼び出す
require_once 'PHPUnit/Framework.php';
//テスト対象のファイルを呼び出す
require_once LIB_PATH . DS . str_replace('Test.php','.php', basename(__FILE__));
//for < PHP 5.3
if (!function_exists('lcfirst')) {
function lcfirst($s)
{
$s{0} = strtolower($s{0});
return $s;
}
}
class MobilelintTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider isSoftbankTableTagNestOverflowData
*/
public function testIsSoftbankTableTagNestOverflow($r, $html) {
$xml = new SimpleXMLElement($html);
list($class, $method) = explode('::', str_replace('Test::test','::',__METHOD__));
$result = call_user_func(array($class, lcfirst($method)), $xml, $html);
$this->assertEquals($r,$result);
}
public function isSoftbankTableTagNestOverflowData(){
return array(
array(false,'<table><table><table><table></table></table></table></table>'),
array(false,'<table><table><table><table><table></table></table></table></table></table>'),
array(true,'<table><table><table><table><table><table></table></table></table></table></table></table>'),
array(true,'<table><table><table><table><div><table><table></table></table></div></table></table></table></table>'),
);
}
}