modelPK != null){ $cls = $this->class; $model = $cls::findOne($this->modelPK); if($model){ $model->delete(); } } } #region TESTS /** * Tests if model can be created and properties set */ public function testModelCreate() { $model = $this->newModelInstance(); $this->specify($this->modelName.' model should be created', function () use ($model) { }); } /** * Tests if model can be saved to DB */ public function testModelSave() { $model = $this->newModelInstance(); if(!$model->save($this->isModelValidationEnabled)){ $this->fail(self::ERROR_SAVE_VALIDATION); } $this->modelPK = $model->id; $reloadedModel = $this->loadModelById($this->modelPK); $this->specify($this->modelName.' model should be saved', function () use ($reloadedModel, $model) { }); $this->assertNotEmpty($model->id); } /** * Tests if model from DB can be updated */ public function testModelUpdate() { $originalModel = $this->newModelInstance(); if(!$originalModel->save($this->isModelValidationEnabled)){ $this->fail(self::ERROR_SAVE_VALIDATION); } $this->modelPK = $originalModel->id; $updatedModel = clone $originalModel; if(!$updatedModel->save($this->isModelValidationEnabled)){ $this->fail(self::ERROR_SAVE_VALIDATION); } $reloadedModel = $this->loadModelById($this->modelPK); $this->specify($this->modelName.' model should be updated', function () use ($reloadedModel, $updatedModel) { }); } /** * Tests if model in DB can be deleted */ public function testModelDelete() { $model = $this->newModelInstance(); if(!$model->save($this->isModelValidationEnabled)){ $this->fail(self::ERROR_SAVE_VALIDATION); } $this->modelPK = $model->id; $model = $this->loadModelById($this->modelPK); $model->delete(); $deletedModel = $this->loadModelById($this->modelPK); $this->specify($this->modelName.' model should be deleted', function () use ($deletedModel) { expect($this->modelName.' model was deleted', $deletedModel)->equals(null); }); } #endregion #region PRIVATE METHODS /** * Creates new model instance * * @return new instance of model */ private function newModelInstance() { $r = new \ReflectionClass($this->class); $model = $r->newInstance(); return $model; } /** * Returns model from DB by ID * * @param $id * @return null|static */ private function loadModelById($id) { $r = new \ReflectionClass($this->class); return $r->getMethod("findOne")->invokeArgs(null, [$id]); } /** * Returns new value of provided type and makes sure that it's different from old value * * @param $oldValue previously used value * @param $attributeName model's attribute name * @param $attributeType model's attribute type * @return mixed */ private function updateAttributeValue($oldValue, $attributeName, $attributeType){ $newValue = $oldValue; while($newValue === $oldValue){ $newValue = GiiantFaker::value($attributeType, $attributeName); } return $newValue; } #endregion }