Skip to content

Commit

Permalink
Merge branch 'master' into dev-mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
cevro authored Jul 31, 2023
2 parents 01964fe + 6db16a6 commit a2b95b8
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php-psr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '7.4', '8.0', '8.1']
php: [ '8.1']
steps:
- uses: actions/checkout@v2
name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '7.4', '8.0', '8.1']
php: [ '8.1']
database: [ 'mysql' ]
steps:
# MariaDB container has to be started in advance to initialize itself before using it.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"license": "GPL-3.0-or-later",
"require": {
"php": ">=7.4",
"php": ">=8.1",
"nette/di": "^3.0",
"nette/database": "^3.1",
"ext-pdo": "*",
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/Exceptions/CannotAccessModelException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class CannotAccessModelException extends RuntimeException
{

public function __construct(string $modelClassName, object $model, int $code = 0, ?Throwable $previous = null)
{
parent::__construct(
Expand Down
1 change: 0 additions & 1 deletion src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class Extension extends CompilerExtension
{

/**
* @throws NotImplementedException
*/
Expand Down
13 changes: 8 additions & 5 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ public function &__get(string $key)
$docs = ModelRelationsParser::parseModelDoc($selfReflection);
if (!is_null($value) && isset($docs[$key])) {
$item = $docs[$key];
if ($value instanceof ActiveRow && $item['type']->isClass()) {
/** @var \ReflectionClass<object> $returnType */
$returnType = $item['reflection'];
if ($returnType->isSubclassOf(self::class)) {
$value = $returnType->newInstance($value->toArray(), $value->getTable());
if ($item['type']->isClass()) {
$returnType = $item['reflection'];
if ($value instanceof ActiveRow) {
if ($returnType->isSubclassOf(self::class)) {
$value = $returnType->newInstance($value->toArray(), $value->getTable());
}
} elseif ($returnType->isSubclassOf(\BackedEnum::class)) {
$value = $returnType->getMethod('tryFrom')->invoke($returnType, $value);
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ final public function __construct(string $tableName, Explorer $explorer, Mapper
}

/**
* @param string|int|null $key
* @phpstan-return M|null
*/
public function findByPrimary($key): ?Model
public function findByPrimary(int|string|null $key): ?Model
{
return isset($key) ? $this->getTable()->get($key) : null;
}
Expand Down Expand Up @@ -119,7 +118,11 @@ protected function filterData(array $data): array
foreach ($this->getColumnMetadata() as $column) {
$name = $column['name'];
if (array_key_exists($name, $data)) {
$result[$name] = $data[$name];
if ($data[$name] instanceof \BackedEnum) {
$result[$name] = $data[$name]->value;
} else {
$result[$name] = $data[$name];
}
}
}
return $result;
Expand Down
3 changes: 3 additions & 0 deletions tests/ORM/EventModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Fykosak\NetteORM\Model;

/**
*
*/
class EventModel extends Model
{
}
3 changes: 2 additions & 1 deletion tests/ORM/ParticipantModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Fykosak\NetteORM\Model;

/**
* @property-read EventModel $event
* @property-read EventModel event
* @property-read ParticipantStatus status
*/
class ParticipantModel extends Model
{
Expand Down
11 changes: 11 additions & 0 deletions tests/ORM/ParticipantStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Fykosak\NetteORM\Tests\ORM;

enum ParticipantStatus: string
{
case Applied = 'applied';
case Cancelled = 'cancelled';
}
34 changes: 34 additions & 0 deletions tests/Tests/ModelTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Fykosak\NetteORM\Tests\Tests;

use Fykosak\NetteORM\Tests\ORM\ParticipantModel;
use Fykosak\NetteORM\Tests\ORM\ParticipantService;
use Fykosak\NetteORM\Tests\ORM\ParticipantStatus;
use Tester\Assert;

require_once __DIR__ . '/TestCase.php';

class ModelTest extends TestCase
{
public function testCreateFromEnum(): void
{
/** @var ParticipantService $serviceParticipant */
$serviceParticipant = $this->container->getByType(ParticipantService::class);
$countBefore = $serviceParticipant->getTable()->count('*');

$newEvent = $serviceParticipant->storeModel(
['event_id' => 1, 'name' => 'Igor', 'status' => ParticipantStatus::Cancelled]
);
$countAfter = $serviceParticipant->getTable()->count('*');

Assert::same($countBefore + 1, $countAfter);
Assert::type(ParticipantModel::class, $newEvent);
Assert::same(ParticipantStatus::Cancelled, $newEvent->status);
}
}

$testCase = new ModelTest();
$testCase->run();
3 changes: 1 addition & 2 deletions tests/Tests/ReferencedAccessTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ namespace Fykosak\NetteORM\Tests\Tests;

use Fykosak\NetteORM\Exceptions\CannotAccessModelException;
use Fykosak\NetteORM\Tests\ORM\EventModel;
use Fykosak\NetteORM\Tests\ORM\ParticipantModel;
use Fykosak\NetteORM\Tests\ORM\EventService;
use Fykosak\NetteORM\Tests\ORM\ParticipantModel;
use Fykosak\NetteORM\Tests\ORM\ParticipantService;
use Tester\Assert;

require_once __DIR__ . '/TestCase.php';

class ReferencedAccessTest extends TestCase
{

public function testSuccess(): void
{
/** @var ParticipantService $serviceParticipant */
Expand Down
8 changes: 6 additions & 2 deletions tests/resource/schema.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
CREATE DATABASE IF NOT EXISTS `nette_orm_test`;

USE `nette_orm_test`;
CREATE TABLE IF NOT EXISTS `event`
DROP TABLE IF EXISTS `participant`;
DROP TABLE IF EXISTS `event`;

CREATE TABLE `event`
(
`event_id` INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
`begin` DATETIME NOT NULL,
`end` DATETIME NOT NULL
) ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `participant`
CREATE TABLE `participant`
(
`participant_id` INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
`event_id` INT NOT NULL,
`status` ENUM ('applied','cancelled') NOT NULL DEFAULT 'applied',
`name` VARCHAR(64),
CONSTRAINT `fk_participant_event`
FOREIGN KEY (`event_id`)
Expand Down

0 comments on commit a2b95b8

Please sign in to comment.