public function getByClassName($classname, array $config, $id = null) { $model = new Model(array_merge(['model' => $classname], $config), $this->modelManager->getAbstractionLayer($classname), $this->relationFactory, $this->fieldFactory, $this->generator); $model->setSlug($this->slugger->slugify((new ReflectionClass($classname))->getShortName())); if (is_null($id)) { $model->setInstance($this->modelManager->getModelInstance($classname)); } else { $repository = $this->modelManager->getRepository($classname); $model->setInstance($repository->findByOrFail($repository->getModel()->getKeyName(), $id)); } return $model; }
public function test_persist() { \App::instance('ANavallaSuiza\\Laravel\\Database\\Contracts\\Manager\\ModelManager', $modelManagerMock = $this->mock('ANavallaSuiza\\Laravel\\Database\\Contracts\\Manager\\ModelManager')); $requestMock = $this->mock('Illuminate\\Http\\Request'); $requestMock->shouldReceive('input')->andReturn('something', ['somethingElse' => ['someValue']]); $modelManagerMock->shouldReceive('getModelInstance')->andReturn($userMock = $this->mock('Anavel\\Crud\\Tests\\Models\\User')); $fieldMock = $this->mock('Anavel\\Crud\\Abstractor\\Eloquent\\Field'); $this->sut->shouldReceive('getEditFields')->times(1)->with(true)->andReturn(['main' => [$fieldMock, $fieldMock, $fieldMock, $fieldMock]]); $this->sut->shouldReceive('getRelations')->andReturn(collect(['group' => $relationMock = $this->mock('\\Anavel\\Crud\\Abstractor\\Eloquent\\Relation\\Relation')])); $userMock->shouldReceive('setAttribute', 'save')->atLeast()->once(); $relationMock->shouldReceive('persist'); $relationMock->shouldReceive('getSecondaryRelations')->andReturn(collect()); $fieldMock->shouldReceive('getName', 'applyFunctions')->andReturn($fieldMock); $fieldMock->shouldReceive('saveIfEmpty')->andReturn(true); $fieldMock->shouldReceive('getFormField')->andReturn($fieldMock); $this->getClassMock->andReturn('nomatch'); $result = $this->sut->persist($requestMock); $this->assertInstanceOf('Anavel\\Crud\\Tests\\Models\\User', $result); }
protected function getNestedRelation(Model $modelAbstractor, $relationName) { $relations = $modelAbstractor->getRelations(); if (!$relations->has($relationName)) { throw new AbstractorException('Relation ' . $relationName . ' not configured on ' . $modelAbstractor->getModel()); } $relation = $relations->get($relationName); if ($relation instanceof Relation) { return $relation; } else { return $relation['relation']; } }