脚本宝典收集整理的这篇文章主要介绍了使用YAML文件作为PHPUnit(CIUnit)中的数据提供程序,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我返回下面的Yaml文件的对象PHPUnit_extensions_Database_DataSet_YamlDataSet,我得到:
如果我循环PHPUnit_Extensions_Database_DataSet_YamlDataSet返回的对象并返回:我收到此错误:
如果我提供一个vanilla PHP数组,那么测试运行得很好.我用来运行测试的命令是:
下面是我的YAML文件的示例.
Clients: 1: client_id: 1 client_information: "info number 1" client_key: 48fb10b15f3d44a09dc82d 2: client_id: 2 client_information: "info number 2" client_key: 48fb10b15f3d44addd
我使用的是PHP 5.3,PHPUnit 3.6.10,DBUnit 1.1.2,CodeIgniter 2.1.0和与CI 2.1.0相关的CI_unit.
/** * test_add_client * @dataPRovider add_client_provider */ public function test_add_client($client_id,$company_id,$Software_id,$client_information,$client_key) { $data = array('software_id' => $software_id,'client_information' => $client_information,'client_key' => $client_key); try { $id = $this->_m->add_client($company_id,$data); $this->assertEquals(true,is_int($id)); } catch (Exception $e){ $this->assertEquals(true,false); } } public function add_client_provider() { $result = new PHPUnit_Extensions_Database_DataSet_YamlDataSet( dirname(__FILE__)."/../fixtures/Clients.yml"); // Case #1 returns this $result //return $result; foreach($result as $key => $value){ if($key == 'Clients'){ $substructure = $value; } } // Case #2 return the inner structure that is the table return $substructure; // Case #3 return an array of arrays $data = array( array(1,1,'test','text 2'),array(1,2,'test 3','test 3')); return $data; }
/** * test_add_client * @dataProvider add_client_provider */ public function test_add_client($data) { $company_id = 0; $id = $this->_m->add_client($company_id,is_int($id)); } public function add_client_provider() { $result = new PHPUnit_Extensions_Database_DataSet_YamlDataSet( dirname(__FILE__)."/../fixtures/Clients.yml"); // Return the Clients data $clients = array(); $tbl = $result->getTable('Clients'); for ($i = 0; $i < $tbl->getrowcount(); $i++) { $clients[] = $tbl->getRow($i); } return $clients; }
似乎PHPUnit应该提供一个函数来将数据集表直接转换为数组数组,但是在快速浏览后我没有看到任何内容.
PHPunit.XMl文件是无关紧要的,据我所知,可以从你的问题中删除.
您还不需要PHPUnit测试方法中的try / catch块–PHPUnit将为您处理这个问题.
请注意,您的$company_id未定义,因此我将其设置为0.您的方法参数&上面的YAML数据似乎也没有完全匹配,但这应该很容易修复.
以上是脚本宝典为你收集整理的使用YAML文件作为PHPUnit(CIUnit)中的数据提供程序全部内容,希望文章能够帮你解决使用YAML文件作为PHPUnit(CIUnit)中的数据提供程序所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。