Skip to content

Commit

Permalink
Merge pull request #1 from fykosak/dev-tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
cevro authored Mar 25, 2021
2 parents 7b9fdc7 + fe551c5 commit 7e4bcf4
Show file tree
Hide file tree
Showing 23 changed files with 1,150 additions and 9 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: PHP (Nette Tester)

on: [push, pull_request]

jobs:
test:
name: PHP ${{ matrix.php }} with ${{ matrix.database }}
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
php: [ '7.4' ]
database: [ 'mysql' ]
steps:
# MariaDB container has to be started in advance to initialize itself before using it.
- uses: actions/checkout@v2
name: Checkout
with:
submodules: recursive
- uses: shivammathur/setup-php@v2
name: Setup PHP
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, mysql, soap, xml
tools: composer
ini-values: session.gc_probability=0, date.timezone="Europe/Prague", display_startup_errors = Off
- name: Composer install
run: composer install --no-progress --prefer-dist
- name: DB configuration
run: "sed -e 's/user:/user: runner/' tests/config.local.neon.example > tests/config.local.neon"
- name: Start and prepare MySQL
run: |
sudo service mysql start
sudo mysql -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED BY ''; CREATE USER 'runner'@'localhost' IDENTIFIED BY ''; GRANT ALL PRIVILEGES ON * . * TO 'runner'@'localhost';"
- run: composer run-script initTestDatabase
name: Init test database
- run: composer run-script testCoverage
name: Test
- name: Archive code coverage results
uses: actions/upload-artifact@v2
with:
name: code-coverage-report
path: coverage.html
- if: failure()
name: Failure output
uses: actions/upload-artifact@v2
with:
name: output
path: tests/**/*.actual
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/vendor/
/tests/config.local.neon
/tests/temp/*
!/tests/temp/.gitignore
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 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-87%25-green" />

## install

### Create ORM model
Expand Down
25 changes: 22 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
"version": "0.1.3",
"require": {
"php": ">=7.4",
"nette/di": "^3.0.8",
"nette/di": "^3.0",
"nette/database": "^3.1",
"ext-pdo": "*"
"ext-pdo": "*",
"ext-gettext": "*"
},
"require-dev": {
"nette/tester": "^2.4.0"
},
"authors": [
{
Expand All @@ -26,7 +30,22 @@
],
"autoload": {
"psr-4": {
"Fykosak\\NetteORM\\": "src/"
"Fykosak\\NetteORM\\": "src/",
"Fykosak\\NetteORM\\Tests\\": "tests/"
}
},
"scripts": {
"initTestDatabase": [
"mysql < tests/resource/schema.sql "
],
"clearCache": [
"rm -r tests/temp/*"
],
"test": [
"vendor/bin/tester tests -p php -s -c tests/php.ini -j 1"
],
"testCoverage": [
"vendor/bin/tester tests -p php --coverage coverage.html --coverage-src src/ -s -c tests/php.ini -j 1"
]
}
}
75 changes: 72 additions & 3 deletions composer.lock

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

2 changes: 1 addition & 1 deletion src/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function dispose(AbstractModel $model): void {
}

public function getTable(): TypedTableSelection {
return new TypedTableSelection($this->getModelClassName(), $this->tableName, $this->explorer, $this->conventions);
return new TypedTableSelection($this->getModelClassName(), $this->tableName, $this->explorer, $this->explorer->getConventions());
}

public function store(?AbstractModel $model, array $data): AbstractModel {
Expand Down
6 changes: 4 additions & 2 deletions src/ORMExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public function loadConfiguration(): void {
}

final protected function registerORMService(string $tableName, array $fieldDefinitions): void {
if (isset($fieldDefinitions['serviceClassName'])) {
$serviceClassName = $fieldDefinitions['serviceClassName'] ?? ($fieldDefinitions['service'] ?? null);
$modelClassName = $fieldDefinitions['modelClassName'] ?? ($fieldDefinitions['model'] ?? null);
if ($serviceClassName) {
$builder = $this->getContainerBuilder();
$factory = $builder->addDefinition($this->prefix($tableName . '.service'));
$factory->setFactory($fieldDefinitions['serviceClassName'], [$tableName, $fieldDefinitions['modelClassName']]);
$factory->setFactory($serviceClassName, [$tableName, $modelClassName]);
}
}
}
9 changes: 9 additions & 0 deletions tests/ORM/ModelEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Fykosak\NetteORM\Tests\ORM;

use Fykosak\NetteORM\AbstractModel;

class ModelEvent extends AbstractModel {

}
12 changes: 12 additions & 0 deletions tests/ORM/ModelParticipant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Fykosak\NetteORM\Tests\ORM;

use Fykosak\NetteORM\AbstractModel;

class ModelParticipant extends AbstractModel {

public function getEvent(): ModelEvent {
return ModelEvent::createFromActiveRow($this->event);
}
}
9 changes: 9 additions & 0 deletions tests/ORM/ServiceEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Fykosak\NetteORM\Tests\ORM;

use Fykosak\NetteORM\AbstractService;

class ServiceEvent extends AbstractService {

}
9 changes: 9 additions & 0 deletions tests/ORM/ServiceParticipant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Fykosak\NetteORM\Tests\ORM;

use Fykosak\NetteORM\AbstractService;

class ServiceParticipant extends AbstractService {

}
60 changes: 60 additions & 0 deletions tests/Tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Fykosak\NetteORM\Tests\Tests;

use Fykosak\NetteORM\ORMExtension;
use Nette\Bridges\DatabaseDI\DatabaseExtension;
use Nette\Database\Explorer;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Tester\Environment;
use Tester\TestCase;

define('__TEMP__DIR__', __DIR__ . '/../temp');

require_once __DIR__ . '/../../vendor/autoload.php';

class AbstractTestCase extends TestCase {

protected Container $container;

public function __construct() {
Environment::setup();
error_reporting(~E_DEPRECATED);
$loader = new ContainerLoader(__TEMP__DIR__, true);

$class = $loader->load(function (Compiler $compiler) {

$compiler->addExtension('orm', new ORMExtension());
$compiler->addExtension('database', new DatabaseExtension());
$compiler->loadConfig(__DIR__ . '/../config.neon');
});

$this->container = new $class();
}

public function setUp() {
Environment::lock('DB', __TEMP__DIR__);
/** @var Explorer $explorer */
$explorer = $this->container->getByType(Explorer::class);
$explorer->query("DELETE FROM `participant`;
DELETE FROM `event`;
INSERT INTO `event` (event_id, begin, end)
VALUES (1, '2010-01-01', '2010-02-01'),
(2, '2010-02-01', '2010-03-01'),
(3, '2010-03-01', '2010-04-01');
INSERT INTO `participant` (participant_id,event_id, name)
VALUES (1,1, 'Adam'),
(2,1, 'Bára'),
(3,1, 'Cecilia'),
(4,2, 'Dano'),
(5,2, 'Emil'),
(6,3, 'Fero'),
(7,3, 'Gustav'),
(8,3, 'Husák');");
parent::setUp();
}

}
26 changes: 26 additions & 0 deletions tests/Tests/ClassLoadTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Fykosak\NetteORM\Tests\Tests;

use Fykosak\NetteORM\AbstractService;
use Fykosak\NetteORM\Tests\ORM\ServiceEvent;
use Fykosak\NetteORM\Tests\ORM\ServiceParticipant;
use Tester\Assert;

require_once __DIR__ . '/AbstractTestCase.php';

class ClassLoadTest extends AbstractTestCase {

public function testAlias(): void {
$serviceEvent = $this->container->getByName('orm.event.service');
Assert::type(AbstractService::class, $serviceEvent);
Assert::type(ServiceEvent::class, $serviceEvent);

$serviceEvent = $this->container->getByName('orm.participant.service');
Assert::type(AbstractService::class, $serviceEvent);
Assert::type(ServiceParticipant::class, $serviceEvent);
}
}

$testCase = new ClassLoadTest();
$testCase->run();
Loading

0 comments on commit 7e4bcf4

Please sign in to comment.