Skip to content

Commit

Permalink
ActiveRecord update
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 11, 2023
1 parent 0699b4a commit 2156ddd
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 32 deletions.
5 changes: 4 additions & 1 deletion composer.json
Expand Up @@ -22,7 +22,10 @@
"source": "https://github.com/ICanBoogie/Module"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"olvlvl/composer-attribute-collector": true
}
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
4 changes: 2 additions & 2 deletions lib/Module.php
Expand Up @@ -13,12 +13,12 @@

use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\ModelNotDefined;
use ICanBoogie\ActiveRecord\StaticModelProvider;
use ICanBoogie\Module\Descriptor;
use ICanBoogie\Module\ModuleProvider;
use RuntimeException;
use Throwable;

use function ICanBoogie\ActiveRecord\get_model;
use function is_string;

/**
Expand Down Expand Up @@ -267,7 +267,7 @@ public function model(string $model_id = 'primary'): Model

assert(is_string($model_id));

return get_model($model_id);
return StaticModelProvider::model_for_record($model_id);

Check failure on line 270 in lib/Module.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $activerecord_class of static method ICanBoogie\ActiveRecord\StaticModelProvider::model_for_record() expects class-string<ICanBoogie\ActiveRecord>, string given.

Check failure on line 270 in lib/Module.php

View workflow job for this annotation

GitHub Actions / phpstan

Unable to resolve the template type T in call to method static method ICanBoogie\ActiveRecord\StaticModelProvider::model_for_record()
}

/**
Expand Down
24 changes: 11 additions & 13 deletions tests/app/all/config/activerecord.php
@@ -1,23 +1,21 @@
<?php

use ICanBoogie\ActiveRecord\Schema;
use ICanBoogie\ActiveRecord\SchemaColumn;
use ICanBoogie\Binding\ActiveRecord\Config;
use ICanBoogie\ActiveRecord\Config;
use ICanBoogie\ActiveRecord\SchemaBuilder;
use ICanBoogie\Binding\ActiveRecord\ConfigBuilder;
use Test\ICanBoogie\Acme\Article;
use Test\ICanBoogie\Acme\Node;

return fn(ConfigBuilder $config) => $config
->add_connection(Config::DEFAULT_CONNECTION_ID, 'sqlite::memory:')
->add_model(
'nodes',
new Schema([
'nid' => SchemaColumn::serial(),
'title' => SchemaColumn::varchar(80),
])
activerecord_class: Node::class,
schema_builder: fn(SchemaBuilder $builder) => $builder
->add_serial('nid', primary: true)
->add_character('title', size: 80)
)
->add_model(
'contents',
new Schema([
'date' => SchemaColumn::datetime(),
]),
extends: 'nodes',
activerecord_class: Article::class,
schema_builder: fn(SchemaBuilder $builder) => $builder
->add_date('date')
);
7 changes: 7 additions & 0 deletions tests/lib/Acme/Article.php
@@ -0,0 +1,7 @@
<?php

namespace Test\ICanBoogie\Acme;

class Article extends Node
{
}
9 changes: 9 additions & 0 deletions tests/lib/Acme/Node.php
@@ -0,0 +1,9 @@
<?php

namespace Test\ICanBoogie\Acme;

use ICanBoogie\ActiveRecord;

class Node extends ActiveRecord
{
}
3 changes: 2 additions & 1 deletion tests/lib/Binding/Module/ConfigBuilderTest.php
Expand Up @@ -4,6 +4,7 @@

use ICanBoogie\Binding\Module\Config;
use ICanBoogie\Module\Descriptor;
use modules\a\lib\ArA;
use PHPUnit\Framework\TestCase;
use Test\ICanBoogie\Module\ModulesTest\ModuleD\Module;

Expand All @@ -27,7 +28,7 @@ public function testConfig(): void
'a' => new Descriptor(
id: 'a',
class: \Test\ICanBoogie\Module\ModulesTest\ModuleA\Module::class,
models: [ 'a' ],
models: [ ArA::class ],
path: dirname(__DIR__, 3) . '/modules/a/',
),
'sample' => new Descriptor(
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Module/Console/ListModulesCommandTest.php
Expand Up @@ -4,6 +4,7 @@

use ICanBoogie\Console\Test\CommandTestCase;
use ICanBoogie\Module\Console\ListModulesCommand;
use modules\a\lib\ArA;

final class ListModulesCommandTest extends CommandTestCase
{
Expand All @@ -19,7 +20,7 @@ public static function provideExecute(): array
'a',
'',
'',
'a',
ArA::class,
'modules/a/'
]
],
Expand Down
16 changes: 9 additions & 7 deletions tests/lib/ModuleTest.php
Expand Up @@ -9,12 +9,14 @@
use ICanBoogie\PropertyNotWritable;
use LogicException;
use PHPUnit\Framework\TestCase;
use Test\ICanBoogie\Acme\Article;
use Test\ICanBoogie\Acme\Node;

final class ModuleTest extends TestCase
{
private Descriptor $node_descriptor;
private Module $node_module;
private Module $content_module;
private Module $article_module;

protected function setUp(): void
{
Expand All @@ -24,25 +26,25 @@ protected function setUp(): void
$this->node_descriptor = new Descriptor(
id: 'nodes',
class: Module::class,
models: [ 'nodes' ]
models: [ Node::class ]
),
$provider
);

$this->content_module = new Module(
$this->article_module = new Module(
new Descriptor(
id: 'contents',
id: 'articles',
class: Module::class,
parent: $this->node_module->id,
models: [ 'contents' ]
models: [ Article::class ]
),
$provider
);

$provider->method('module_for_id')
->willReturnCallback(fn(string $id) => match ($id) {
$this->node_module->id => $this->node_module,
$this->content_module->id => $this->content_module,
$this->article_module->id => $this->article_module,
default => throw new LogicException()
});
}
Expand Down Expand Up @@ -97,6 +99,6 @@ public function test_get_model(): void

public function test_get_parent(): void
{
$this->assertSame($this->node_module, $this->content_module->parent);
$this->assertSame($this->node_module, $this->article_module->parent);
}
}
11 changes: 5 additions & 6 deletions tests/modules/a/config/activerecord.php
@@ -1,13 +1,12 @@
<?php

use ICanBoogie\ActiveRecord\Schema;
use ICanBoogie\ActiveRecord\SchemaColumn;
use ICanBoogie\ActiveRecord\SchemaBuilder;
use ICanBoogie\Binding\ActiveRecord\ConfigBuilder;
use modules\a\lib\ArA;

return fn(ConfigBuilder $config) => $config
->add_model(
id: 'a',
schema: new Schema([
'id' => SchemaColumn::serial(),
])
activerecord_class: ArA::class,
schema_builder: fn(SchemaBuilder $builder) => $builder
->add_serial('id', primary: true)
);
4 changes: 3 additions & 1 deletion tests/modules/a/config/module.php
Expand Up @@ -4,12 +4,14 @@

use ICanBoogie\Binding\Module\ConfigBuilder;

Check failure on line 5 in tests/modules/a/config/module.php

View workflow job for this annotation

GitHub Actions / phpcs

Header blocks must not contain blank lines

use modules\a\lib\ArA;

use function dirname;

return fn(ConfigBuilder $config) => $config
->add_module(
id: 'a',
class: Module::class,
models: [ 'a' ],
models: [ ArA::class ],
path: dirname(__DIR__)
);
10 changes: 10 additions & 0 deletions tests/modules/a/lib/ArA.php
@@ -0,0 +1,10 @@
<?php

namespace modules\a\lib;

use ICanBoogie\ActiveRecord;

class ArA extends ActiveRecord
{
public int $id;
}

0 comments on commit 2156ddd

Please sign in to comment.