I’m running phpunit with mockery (without DB/fixtures), but I have a trouble with mocking a model.
$customer = Mockery::mock(CustomerModel::class);
echo $customer->id;
Produces error:
BadMethodCallException: Method Mockery_1_models_Customer::hasAttribute() does not exist on this mock object
Then I tried:
$customer->shouldReceive('hasAttribute')->andReturn(true);
But again, I run in to:
Fatal error: Call to a member function getDb() on a non-object in …\yiisoft\yii2\db\ActiveRecord.php on line 135
Any suggestions? How could I easily inject my own model with values, without the need of a database or writing fixtures?
The method I want to test looks like this:
public function createDraft($storeId)
{
$order = new Order;
$order->customer = $this->customer->id;
$order->status = Order::DRAFT;
$stores = $this->stores->getActiveStores();
if(!$stores){
throw new Exception('Cannot create draft, stores available');
}
// Set store
if ($storeId && isset($stores[$storeId])) {
$order->store = $storeId;
}else {
/** @var Store $store */
$store = reset($stores);
$order->store = $store->id;
}
return $order;
}
I just want to test if exception is thrown, or a model with correct values are set.