baserproject / bcbake Goto Github PK
View Code? Open in Web Editor NEWbaserCMSの管理画面の scaffold を作成するプラグイン
baserCMSの管理画面の scaffold を作成するプラグイン
@ryuring
プラグインのServiceクラスのテストを、baserCMSに倣って書きたいのですが、
以下のようなエラーが出て、コンテナに登録されない(?)、エイリアスが無い(?)と怒られます。
1) DubOgp\Test\TestCase\Service\DubOgpConfigsServiceTest::test__construct
League\Container\Exception\NotFoundException: Alias (DubOgp\Service\DubOgpConfigsServiceInterface) is not being managed by the container or delegates
/var/www/html/vendor/league/container/src/Container.php:208
/var/www/html/vendor/league/container/src/Container.php:118
/var/www/html/plugins/baser-core/src/Utility/BcContainerTrait.php:33
/var/www/html/plugins/DubOgp/tests/TestCase/Service/DubOgpConfigsServiceTest.php:20
/var/www/html/vendor/bin/phpunit:122
ちなみにテストはdockerコンテナに入って、プラグインディレクトリから実行しています。
cd plugins/DubOgp/
../../vendor/bin/phpunit tests/TestCase/Service/DubOgpConfigsServiceTest.php
basercms/plugins/bc-theme-config/tests/TestCase/Service/ThemeConfigsServiceTest.php
この辺を参考にしてるのですが、コンテナ登録はBcTestCaseが勝手にやってくれてるように見えるんですが、
なにか設定なり登録が必要でしょうか?
なにか気になる点や、ヒントだけでもいただけると助かります。
できれば、ServiceTestのサンプルがあると嬉しいです!
問題のテストコードは以下、
DubOgpConfigsServiceTest.php
<?php
namespace DubOgp\Test\TestCase\Service;
// use Cake\TestSuite\TestCase;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Utility\BcContainerTrait;
use DubOgp\Service\DubOgpConfigsService;
use DubOgp\Service\DubOgpConfigsServiceInterface;
class DubOgpConfigsServiceTest extends BcTestCase
{
use BcContainerTrait;
public $DubOgpConfigsService;
public function setUp(): void
{
parent::setUp();
$this->DubOgpConfigsService = $this->getService(DubOgpConfigsServiceInterface::class);
}
public function tearDown(): void
{
unset($this->DubOgpConfigsService);
parent::tearDown();
}
public function test__construct()
{
$this->assertEquals('dub_ogp_configs', $this->DubOgpConfigsService->DubOgpConfigs->getTable());
}
}
テスト対象のServiceは以下、
DubOgpConfigsService.php
<?php
declare(strict_types=1);
namespace DubOgp\Service;
use DubOgp\Model\Entity\DubOgpConfig;
use DubOgp\Model\Table\DubOgpConfigsTable;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
/**
* DubOgpConfigs Service
*/
class DubOgpConfigsService implements DubOgpConfigsServiceInterface
{
public DubOgpConfigsTable|Table $DubOgpConfigs;
/**
* キャッシュ用 Entity
* @var MailConfig
*/
protected $entity;
/**
* constructor
*/
public function __construct()
{
$this->DubOgpConfigs = TableRegistry::getTableLocator()->get('DubOgp.DubOgpConfigs');
}
/**
* get new
* @return \Cake\Datasource\EntityInterface
以下bakeされたコードが続く
*/
}
プロバイダーは登録してあって、以下のテストコードは通りました。
DubOgpServiceProviderTest.php
<?php
namespace DubOgp\Test\TestCase\ServiceProvider;
use BaserCore\TestSuite\BcTestCase;
use DubOgp\Service\DubOgpConfigsServiceInterface;
use DubOgp\ServiceProvider\DubOgpServiceProvider;
use Cake\Core\Container;
class DubOgpServiceProviderTest extends BcTestCase
{
public function setUp(): void
{
parent::setUp();
$this->DubOgpServiceProvider = new DubOgpServiceProvider();
}
public function tearDown(): void
{
unset($this->DubOgpServiceProvider);
parent::tearDown();
}
public function testServices()
{
$container = new Container();
$this->DubOgpServiceProvider->services($container);
$this->assertTrue($container->has(DubOgpConfigsServiceInterface::class));
}
}
対象コードは以下、
DubOgpServiceProvider.php
<?php
declare(strict_types=1);
namespace DubOgp\ServiceProvider;
use DubOgp\Service\DubOgpConfigsService;
use DubOgp\Service\DubOgpConfigsServiceInterface;
use DubOgp\Service\Admin\DubOgpConfigsAdminService;
use DubOgp\Service\Admin\DubOgpConfigsAdminServiceInterface;
use Cake\Core\ServiceProvider;
/**
* DubOgp Service Provider
*/
class DubOgpServiceProvider extends ServiceProvider
{
protected $provides = [
// TableNameAdminServiceInterface::class,
DubOgpConfigsServiceInterface::class,
DubOgpConfigsAdminServiceInterface::class,
];
public function services($container): void
{
$container->defaultToShared(true);
//$container->add(TableNameAdminServiceInterface::class, TableNameAdminService::class);
$container->add(DubOgpConfigsServiceInterface::class, DubOgpConfigsService::class);
$container->add(DubOgpConfigsAdminServiceInterface::class, DubOgpConfigsAdminService::class);
}
}
bin/cake bake bc_plugin Hoge
とした場合、src/HogePlugin.phpが作られますが、画面からインストールしようとすると、
「プラグインに Plugin クラスが存在しません。src ディレクトリ配下に作成してください。」
と、たぶんsrc/Plugin.phpがないと怒られます。
実際は、src/HogePlugin.phpとsrc/Plugin.phpはどちらが正解なのでしょうか?
@ryuring
(フォーラムとどちらが良いんだろうと思いながら、こちらで質問します、)
https://qiita.com/ryuring/items/c0b517fc454ff7f08e4b
こちらを参考に、プラグインのテストを作ろうとしてますが、
プラグインディレクトリからテスト実行すると、プラグイン内のphpunit.xmlとtests/bootstrap.phpは実行されるんですよね?
という前提の元、
bin/cake bake bc_plugin Hoge
プラグインを作って、プラグインのディレクトリから、
../../vendor/bin/phpunit
テストを実行すると、
Warning: Constant ROOT already defined in /var/www/html/config/paths.php on line 31
2重定義してまっせというワーニングが大量に出ます。
config/paths.php
if (!defined('ROOT')) {
define('ROOT', dirname(__DIR__));
}
こんな感じで直すと、ワーニングは出なくはなりますが、
何か使い方間違えてますでしょうか?
もしくは、config/paths.phpを修正してPR出した方が良いでしょうか?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.