I'm working on a pull request for attribute casting in eloquent and am running into a scenario where a unit test can't resolve a class out of app() and I'm unsure how to mock it. How can I mock app()?
Fatal error: Call to a member function getBindings() on null in /Users/bkuhl/Personal/framework/src/Illuminate/Database/Eloquent/Model.php on line 2825
protected function castAttribute($key, $value)
{
$castType = $this->getCastType($key);
// Some objects aren't stored as json so we'll skip those
// here so they'll get handled accordingly later
if (!in_array($castType, ['date', 'datetime'])) {
if (class_exists($this->casts[$key])) {
return $this->resolveCastedAttribute($this->casts[$key], $value);
}
if (in_array($key, app()->getBindings())) {
return $this->resolveCastedAttribute($key, $value);
}
}
if (is_null($value)) {
return $value;
}
switch ($castType) {
case 'int':
case 'integer':
return (int) $value;
case 'real':
case 'float':
case 'double':
return (float) $value;
case 'string':
return (string) $value;
case 'bool':
case 'boolean':
return (bool) $value;
case 'object':
return $this->fromJson($value, true);
case 'array':
case 'json':
return $this->fromJson($value);
case 'collection':
return new BaseCollection($this->fromJson($value));
case 'date':
case 'datetime':
return $this->asDateTime($value);
case 'timestamp':
return $this->asTimeStamp($value);
}
}
The source is here: https://github.com/bkuhl/framework/commit/b57d8ebbe3da80e49be6d310b08cc09c565b7ecc