if (!isset($config['dependencyClass'])) {
$this->dependencyClass = new Dependency();
}
//config/web.php
"components"=>[
//...
"model_deps"=>[
"class"=>"app\components\ModelDependenciesService",
"default_dependency"=>"app\model\model_2",
"dependencies"=>[
"app\model\model_1"=>[
"dep_1"=>"app\model\model_3",
//....
]
]
]
//...
]
// в базовом классе модели
public function getDepClass($dep_name){
retrn Yii::$app->model_deps->depClass(static,$dep_name); // не помню точно как текущий класс брать
}
// где-то в модели
public function getDependencies()
{
return $this->hasMany($this->getDepClass($dep_name), ['modelId' => 'id']);
}
interface PostCommentModelInterface // можно extends ActiveRecordInterface
{
/**
* @return ActiveRecord[]|Comment[]
**/
public function findForPost();
}
class Model extends yii\base\Model
{
protected $commentModel;
public function __construct(PostCommentModelInterface $commentModel, $config = [])
{
$this->commentModel = $commentModel;
parent::__construct($config);
}
]
...
protected $commentModel;
public function __construct(\yii\container\Container $container, $config = [])
{
$this->commentModel = $container->get('\app\modules\posts\PostCommentModelInterface'');
parent::__construct($config);
}
...
<img
src="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw
AAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFz
ByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSp
a/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJl
ZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uis
F81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PH
hhx4dbgYKAAA7"
alt="Larry" />
class BarFabric
{
public function create(array $config = [])
{
return new Bar($config);
}
}
class Foo
{
protected $barFabric;
public function __construct(BarFabric $barFabric)
{
$this->barFabric = $barFabric;
}
public function someMethod()
{
$bar = $this->barFabric->create();
$bar->method1();
$bar->method2();
$blabla = $bar->getResult();
//etc
}
}
class FooTest
{
public function testSomeMethod()
{
$bar = \Mokery::mock(Bar::class);
// ... описание поведения для мока
$factory = \Mokery::mock(BarFactory::class);
$factory->shouldReceive('create')->andReturn($bar);
$foo = new Foo($factory);
$this->assertSomething($foo);
}
}
class FooQuery extend ActiveQuery
{
/**
* @inheritdoc
* @return Foo[]|array
*/
public function all($db = null)
{
return parent::all($db);
}
/**
* @inheritdoc
* @return Foo|array|null
*/
public function one($db = null)
{
return parent::one($db);
}
}
class Bar
{
protected $query;
public function __construct(FooQuery $query)
{
$this->query = $query;
}
public function someMethod()
{
$foo = $this->query->where(...)->one();
$foo->doSomething();
}
}
$queryMock = \Mockery::mock(FooQuery::class);
$queryMock->shouldRecieve('where->one')->andReturn($fooMock);
$fooMock = \Mockery::mock(Foo::class.'[save, update]');
$fooMock->shouldRecieve('save', 'update')->andReturn(true);
$foo = new Foo();
$foo->populateRelation('bar', new Bar());
пожалуйста, не рекомендуйте использовать фреймворки вместо велосипедов
$transaction->rollBack();
foreach ($this->firstErrors as $key => $value) break;
if (empty($value)) $value = 'Неизвестная ошибка';
return array('message' => $value, 'status' => false);
throw new HttpException(500, 'Неизвестная ошибка');