Search code examples
phpmockingphpunitmockery

Mockery does not mock propeties of class


i have test:

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);
    }

and this is 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(i define connect to data base and define select method:

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);
    }
}

But when i run xdebug and run this test, $forbidden is null,it mean mock method return real data, not mock data. i dont know why. Anyone can help me! Please!


Solution

  • I was change it to:

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

    And it is working for me, thank for all help