嘲笑不会嘲笑 class 的属性

Mockery does not mock propeties of class

我有测试:

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:

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(我定义连接到数据库并定义 select 方法:

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,这意味着模拟方法return 真实数据,而不是模拟数据。我不知道为什么。 任何人都可以帮助我!请!

除了使用 new 关键字创建 class 实例之外,您从未将模拟插入到 class 中,因此很难模拟。在这种情况下,您唯一的机会是使用 class 别名。
为了避免这一切,您可以通过 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);
             }
        }
    }
}

我用这段代码测试了它,它运行良好。请检查是否在测试文件的顶部导入了数据库 class。您还必须将 Test 附加到所有测试文件名和 classes(见上文)。

我将其更改为:

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

它对我有用,感谢所有帮助