嘲弄不嘲笑阶级

时间:2019-03-13 03:37:38

标签: php mocking phpunit mockery

我有测试:

class ContacsBLOTest extends TestCase
{    
    public function testsearch()
    {
        $Ctrl= new ContactsBLO;
        $data=['id'=>1,'name'=>'The Manh','phone'=>'123456566','address'=>'180 cao lo','note'=>''];
        $data=[(object)$data];

        $mock_data=\Mockery::mock('DB');
        $mock_data->shouldReceive('all')->andReturn($data);
        $mock_ctrl= new ContactsBLO;
        $mock_ctrl->select=$mock_data;
        $result=$mock_ctrl->search('manh');    
        $this->assertNotNull($result);
    }

这是ContacsBLO类:

class ContactsBLO
{

    public $db,$not_allow,$Validation;
    public function __construct(){
        $this->db=new DB;
        $this->not_allow=['"','\'','%'];
        $this->Validation = new ContactValidation;    
    }

    public function search($request=null){
        $length=strlen($request);
        for ($i=0;$i<$length;$i++) {
            $forbidden=$this->not_allow;
            if(in_array($request[$i],$forbidden)){
                return (['messenger'=>'We are not allow special character in your request','old_input'=>$request]);
            }
            else{
                return $data=$this->db->select('*',$request);
             }
        }
    }
}

DB :: class(我定义连接到数据库并定义选择方法:

class DB
{
    public $obj = null;
    public $table = 'contacts';
    public function __construct(){
         $dsn="mysql:host=".HOST."; dbname=".DB_NAME;
         $this->obj = new \PDO($dsn, DB_USER, DB_PASS);
         $this->obj->query("set names 'utf8' ");
    }
    public function select($row=null,$query=null)    {
        $sql='SELECT '.$row.' FROM '.$this->table.' '.$query;
        $data = $this->obj->prepare($sql);
        $data->execute();
        return $data->fetchAll(\PDO::FETCH_CLASS);
    }
}

但是当我运行xdebug并运行此测试时,$ forbidden为null,这意味着模拟方法返回真实数据,而不是模拟数据。我不知道为什么。 任何人都可以帮助我!拜托!

2 个答案:

答案 0 :(得分:1)

您从来没有将模拟插入到您的类中,除了使用new关键字创建类实例时,还很难模拟。在这种情况下,您唯一的机会就是使用类别名。
为了避免所有这些,您可以通过ContactsBLO构造函数传入数据库实例。

class ContacsBLOTest extends TestCase
{   
    public function testSearch()
    {
        $data = ['id'=>1,'name'=>'The Manh','phone'=>'123456566','address'=>'180 cao lo','note'=>''];
        $data = json_decode(json_encode($data));
        $mock_contact = \Mockery::mock(DB::class);  
        $mock_contact->shouldReceive('select')->andReturn($data);
        $Ctrl = new ContactsBLO($mockDB);
        $result = $Ctrl->search('manh');
        $this->assertNotNull($result);
    }
}

class ContactsBLO
{
    public $db;
    public $not_allow;
    public $Validation;

    public function __construct(DB $db) {
        $this->db = $db;
        $this->not_allow = ['"','\'','%'];
        $this->Validation = new ContactValidation;
    }

    public function search($request=null){
        $length=strlen($request);
        for ($i=0;$i<$length;$i++) {
            $forbidden = $this->not_allow;
            if(in_array($request[$i],$forbidden)){
                return (['messenger'=>'We are not allow special character in your request','old_input'=>$request]);
            }
            else{
                return $data = $this->db->select('*',$request);
             }
        }
    }
}

我使用此代码对其进行了测试,并且效果很好。请检查数据库类是否已导入到测试文件的顶部。您还必须将Test附加到所有测试文件名和类(请参见上文)。

答案 1 :(得分:0)

我将其更改为:

$mock_data=\Mockery::mock('DB');
$mock_data->shouldReceive('select')->andReturn($data);
$mock_ctrl= new ContactsBLO;
$mock_ctrl->db=$mock_data;
$result=$mock_ctrl->search();

它正在为我工​​作,谢谢大家的帮助