Skip to content

Commit

Permalink
Merge pull request #4 from fykosak/dev-0.2.1
Browse files Browse the repository at this point in the history
[0.2.2] Add storeModel & updateModel methods
  • Loading branch information
cevro authored Mar 26, 2021
2 parents 19571cd + cf7e10e commit f2aba75
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Nette ORM

![GitHub branch checks state](https://img.shields.io/github/checks-status/fykosak/nette-orm/master)
<img src="https://img.shields.io/badge/coverage-97%25-green" />
<img src="https://img.shields.io/badge/coverage-100%25-green" />

## install

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "fykosak/nette-orm",
"type": "library",
"license": "GPL-3.0-or-later",
"version": "0.2.1",
"version": "0.2.2",
"require": {
"php": ">=7.4",
"nette/di": "^3.0",
Expand Down
35 changes: 29 additions & 6 deletions src/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Fykosak\NetteORM\Exceptions\ModelException;
use InvalidArgumentException;
use Nette\Database\Conventions;
use Nette\Database\Explorer;
use Nette\SmartObject;
use PDOException;
Expand Down Expand Up @@ -42,14 +41,10 @@ public function createNewModel(array $data): AbstractModel {
$data = $this->filterData($data);
try {
$result = $this->getTable()->insert($data);
if ($result !== false) {
return ($modelClassName)::createFromActiveRow($result);
}
return ($modelClassName)::createFromActiveRow($result);
} catch (PDOException $exception) {
throw new ModelException('Error when storing model.', null, $exception);
}
$code = $this->explorer->getConnection()->getPdo()->errorCode();
throw new ModelException("$code: Error when storing a model.");
}

/**
Expand Down Expand Up @@ -80,6 +75,7 @@ public function refresh(AbstractModel $model): AbstractModel {
* @param array $data
* @return bool
* @throws ModelException
* @deprecated use updateModel will be removed in 0.3.0
*/
public function updateModel2(AbstractModel $model, array $data): bool {
try {
Expand All @@ -91,6 +87,23 @@ public function updateModel2(AbstractModel $model, array $data): bool {
}
}

/**
* @param AbstractModel $model
* @param array $data
* @return AbstractModel refreshed model
* @throws ModelException
*/
public function updateModel(AbstractModel $model, array $data): AbstractModel {
try {
$this->checkType($model);
$data = $this->filterData($data);
$model->update($data);
return $this->refresh($model);
} catch (PDOException $exception) {
throw new ModelException('Error when storing model.', null, $exception);
}
}

/**
* Use this method to delete a model!
* (Name chosen not to collide with parent.)
Expand All @@ -112,6 +125,12 @@ public function getTable(): TypedTableSelection {
return new TypedTableSelection($this->getModelClassName(), $this->tableName, $this->explorer, $this->explorer->getConventions());
}

/**
* @param AbstractModel|null $model
* @param array $data
* @return AbstractModel
* @deprecated use storeModel will be removed in 0.3.0
*/
public function store(?AbstractModel $model, array $data): AbstractModel {
if ($model) {
$this->updateModel2($model, $data);
Expand All @@ -121,6 +140,10 @@ public function store(?AbstractModel $model, array $data): AbstractModel {
}
}

public function storeModel(array $data, ?AbstractModel $model = null): AbstractModel {
return isset($model) ? $this->updateModel($model, $data) : $this->createNewModel($data);
}

/** @return string|AbstractModel */
final public function getModelClassName(): string {
return $this->modelClassName;
Expand Down
54 changes: 50 additions & 4 deletions tests/Tests/ServiceOperationTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ class ServiceOperationTest extends AbstractTestCase {
Assert::null($nullParticipant);
}

public function testUpdateSuccess(): void {
public function testFindByPrimaryProtection(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
$participant = $serviceParticipant->findByPrimary(null);

Assert::null($participant);
}

public function testLegacyUpdateSuccess(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
$participant = $serviceParticipant->findByPrimary(2);
Expand All @@ -60,7 +68,7 @@ class ServiceOperationTest extends AbstractTestCase {
Assert::type(ModelParticipant::class, $newParticipant);
}

public function testUpdateError(): void {
public function testLegacyUpdateError(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
$participant = $serviceParticipant->findByPrimary(2);
Expand All @@ -72,7 +80,28 @@ class ServiceOperationTest extends AbstractTestCase {
Assert::same('Bára', $newParticipant->name);
}

public function testStoreExists(): void {
public function testUpdateSuccess(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
$participant = $serviceParticipant->findByPrimary(2);
$newParticipant = $serviceParticipant->updateModel($participant, ['name' => 'Betka']);
Assert::same('Betka', $newParticipant->name);
Assert::type(ModelParticipant::class, $newParticipant);
}

public function testUpdateError(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
$participant = $serviceParticipant->findByPrimary(2);

Assert::exception(function () use ($participant, $serviceParticipant) {
$serviceParticipant->updateModel($participant, ['event_id' => 4]);
}, ModelException::class);
$newParticipant = $serviceParticipant->refresh($participant);
Assert::same('Bára', $newParticipant->name);
}

public function testLegacyStoreExists(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
$participant = $serviceParticipant->findByPrimary(2);
Expand All @@ -81,14 +110,31 @@ class ServiceOperationTest extends AbstractTestCase {
Assert::type(ModelParticipant::class, $newParticipant);
}

public function testStoreNew(): void {
public function testStoreExists(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
$participant = $serviceParticipant->findByPrimary(2);
$newParticipant = $serviceParticipant->storeModel(['name' => 'Betka'], $participant);
Assert::same('Betka', $newParticipant->name);
Assert::type(ModelParticipant::class, $newParticipant);
}

public function testLegacyStoreNew(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
$newParticipant = $serviceParticipant->store(null, ['event_id' => 1, 'name' => 'Igor']);
Assert::same('Igor', $newParticipant->name);
Assert::type(ModelParticipant::class, $newParticipant);
}

public function testStoreNew(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
$newParticipant = $serviceParticipant->storeModel(['event_id' => 1, 'name' => 'Igor'], null);
Assert::same('Igor', $newParticipant->name);
Assert::type(ModelParticipant::class, $newParticipant);
}

public function testDelete(): void {
/** @var ServiceParticipant $serviceParticipant */
$serviceParticipant = $this->container->getByType(ServiceParticipant::class);
Expand Down

0 comments on commit f2aba75

Please sign in to comment.