diff --git a/CHANGLOG.md b/CHANGLOG.md index 8344b42..89f8020 100755 --- a/CHANGLOG.md +++ b/CHANGLOG.md @@ -1,5 +1,9 @@ # generator-craftplugin Changelog +## 1.6.0 - 2020.04.26 +### Added +* Added support for the Codeception testing framework, thanks to a PR from Giel + ## 1.5.1 - 2020.04.15 ### Changed * Updated to use the new Twig Extension class namespace to avoid deprecation errors diff --git a/README.md b/README.md index 47e6939..81a359d 100755 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This assumes you have `nodejs`, `npm`, and `yeoman` installed already. 4. -OR- do an `npm -g install generator-craftplugin` to install it via npm (and thus skip the `npm link` step) 5. The generator folder should be named `generator-craftplugin`. GitHub recently started appending `-master` (the branch name) to the name of the folder for zip file downloads. -Requires Node version 4.0.0 or later. +Requires Node version 8.0.0 or later. ## Usage @@ -34,7 +34,7 @@ generator-craftplugin will ask you a few questions: * **Plugin author GitHub.com name:** - enter the GitHub.com handle of the author of the plugin. If you don't have one, just leave it blank * **Select what components your plugin will have:** - select the components you want included in your plugin, using the arrow keys to change the component, and `` to select them. -If you selected `Controllers`, `ElementTypes`, `FieldTypes`, `Models`, `Purchasables`. `Records`, `Services`, `Tasks`, or `Widgets` components, it will also ask you for a name for each one, respectively. If you want multiple components, just separate them with a , in the name input. +If you selected `Controllers`, `ElementTypes`, `FieldTypes`, `Models`, `Purchasables`, `Records`, `Services`, `Tasks`, or `Widgets` components, it will also ask you for a name for each one, respectively. If you want multiple components, just separate them with a , in the name input. generator-craftplugin will then do the following for you: @@ -52,6 +52,7 @@ Here's an example of the output from a `yo craftplugin` generator: ? Select what Craft CMS API to target: (Use arrow keys) Version 2.5.x ❯ Version 3.0.x + Module Version 3.0.x [ Prompting ] ? Plugin name: Some Plugin ? Short description of the plugin: Some Description @@ -75,6 +76,7 @@ e selection) ◉ Services ◉ Settings ◉ Tasks + ◉ Tests ◉ TwigExtensions ◉ Utilities ◉ Variables @@ -108,6 +110,7 @@ e selection) 'services', 'settings', 'tasks', + 'tests', 'twigextensions', 'utilities', 'variables', @@ -155,6 +158,7 @@ e selection) 'services', 'settings', 'tasks', + 'tests', 'twigextensions', 'utilities', 'variables', @@ -183,6 +187,8 @@ e selection) pluginChangelogUrl: 'https://raw.githubusercontent.com/SomeGithub/someplugin/master/CHANGELOG.md', pluginCloneUrl: 'https://github.com/SomeGithub/someplugin.git' } > Writing template files ++ templates/_codeception.yml wrote to someplugin/codeception.yml ++ templates/tests/ wrote to someplugin/tests/ + templates/src/_Plugin.php wrote to someplugin/src/SomePlugin.php + templates/src/models/_Settings.php wrote to someplugin/src/models/Settings.php + templates/_README.md wrote to someplugin/README.md @@ -265,6 +271,25 @@ e selection) + templates/src/resources/images/plugin.png copied to someplugin/src/resources/images/plugin.png + templates/src/resources/screenshots/plugin_logo.png copied to someplugin/src/resources/screenshots/plugin_logo.png > Sync to file system + create someplugin/codeception.yml + create someplugin/tests/_bootstrap.php + create someplugin/tests/_craft/config/db.php + create someplugin/tests/_craft/config/general.php + create someplugin/tests/_craft/config/routes.php + create someplugin/tests/_craft/config/test.php + create someplugin/tests/_craft/storage/example-file.txt + create someplugin/tests/_craft/templates/example.twig + create someplugin/tests/_support/FunctionalTester.php + create someplugin/tests/_support/Helper/Functional.php + create someplugin/tests/_support/Helper/Unit.php + create someplugin/tests/_support/UnitTester.php + create someplugin/tests/example-env + create someplugin/tests/functional.suite.yml + create someplugin/tests/functional/_bootstrap.php + create someplugin/tests/functional/ExampleFunctionalCest.php + create someplugin/tests/unit.suite.yml + create someplugin/tests/unit/_bootstrap.php + create someplugin/tests/unit/ExampleUnitTest.php create someplugin/src/SomePlugin.php create someplugin/src/models/Settings.php create someplugin/README.md @@ -358,7 +383,7 @@ The default LICENSE.txt is the MIT license; feel free to change it as you see fi The `craftplugin` generator can also be passed arguments via the command line, bypassing the interactive prompts. So it's possible do do something like this: - yo craftplugin --pluginComponents="controllers,consolecommands,elementtypes,fieldtypes,models,records,services,settings,tasks,twigextensions,utilities,variables,widgets" --apiVersion="api_version_3_0" --pluginName="Some Plugin" --pluginDescription="Some Description" --pluginVersion="1.0.0" --pluginVendorName="Some Vendor" --pluginAuthorName="Some Author" --pluginAuthorUrl="https://SomeDomain.com" --pluginAuthorGithub="SomeGithub" --codeComments="yes" --consolecommandName="Een,Twee,Drie" --controllerName="One,Two,Three" --elementName="Neung,Song,Sam" --fieldName="Ichi,Ni,San" --modelName="Uno,Dos,Tres" --purchasableName="" --recordName="Satu,Dua,Tiga" --serviceName="Yi,Er,San" --utilityName="Eins,Zwei,Drei" --taskName="Hana,Dul,Set" --widgetName="Un,Deux,Trois" + yo craftplugin --pluginComponents="controllers,consolecommands,elementtypes,fieldtypes,models,records,services,settings,tasks,tests,twigextensions,utilities,variables,widgets" --apiVersion="api_version_3_0" --pluginName="Some Plugin" --pluginDescription="Some Description" --pluginVersion="1.0.0" --pluginVendorName="Some Vendor" --pluginAuthorName="Some Author" --pluginAuthorUrl="https://SomeDomain.com" --pluginAuthorGithub="SomeGithub" --codeComments="yes" --consolecommandName="Een,Twee,Drie" --controllerName="One,Two,Three" --elementName="Neung,Song,Sam" --fieldName="Ichi,Ni,San" --modelName="Uno,Dos,Tres" --purchasableName="" --recordName="Satu,Dua,Tiga" --serviceName="Yi,Er,San" --utilityName="Eins,Zwei,Drei" --taskName="Hana,Dul,Set" --widgetName="Un,Deux,Trois" ## Adding to an existing plugin @@ -387,6 +412,7 @@ Here's a list of the `pluginComponents`; if you want to add more than one, just services settings tasks + tests twigextensions utilities variables @@ -418,4 +444,4 @@ Here's a list of meta parameters that you can also pass in, to override what is --pluginAuthorGithub="" --codeComments="" -Brought to you by [nystudio107](http://nystudio107.com) \ No newline at end of file +Brought to you by [nystudio107](http://nystudio107.com) diff --git a/app/templates/api_version_3_0/_codeception.yml b/app/templates/api_version_3_0/_codeception.yml new file mode 100644 index 0000000..3bb9446 --- /dev/null +++ b/app/templates/api_version_3_0/_codeception.yml @@ -0,0 +1,40 @@ +actor: Tester +paths: + tests: tests + log: tests/_output + data: tests/_data + support: tests/_support + envs: tests/_envs +settings: + bootstrap: _bootstrap.php +coverage: + enabled: true + include: + - src/* + exclude: + - src/etc/* + - src/migrations/* + - src/templates/* + - src/translations/* + - src/web/assets/* + - docs/* + - templates/* + - tests/* + - vendor/* +params: + - tests/.env +modules: + config: + \craft\test\Craft: + configFile: 'tests/_craft/config/test.php' + entryUrl: 'https://test.craftcms.test/index.php' + projectConfig: {} + migrations: [] + plugins: + <%= pluginKebabHandle %>: + class: '\<%= pluginVendorName %>\<%= pluginDirName %>\<%= pluginHandle %>' + handle: <%= pluginKebabHandle %> + cleanup: true + transaction: true + dbSetup: {clean: true, setupCraft: true} + fullMock: false diff --git a/app/templates/api_version_3_0/_composer.json b/app/templates/api_version_3_0/_composer.json index 166aacc..57b2f1b 100755 --- a/app/templates/api_version_3_0/_composer.json +++ b/app/templates/api_version_3_0/_composer.json @@ -22,8 +22,15 @@ } ], "require": { - "craftcms/cms": "^3.0.0-RC1" + "craftcms/cms": "^3.0.0" }, +<% if (pluginComponents.indexOf('tests') >= 0){ -%> + "require-dev": { + "craftcms/cms": "^3.2.0", + "codeception/codeception": "^3.0", + "vlucas/phpdotenv": "^3.0" + }, +<% } -%> "autoload": { "psr-4": { "<%= pluginVendorName %>\\<%= pluginDirName %>\\": "src/" diff --git a/app/templates/api_version_3_0/tests/.gitignore b/app/templates/api_version_3_0/tests/.gitignore new file mode 100644 index 0000000..03bd412 --- /dev/null +++ b/app/templates/api_version_3_0/tests/.gitignore @@ -0,0 +1 @@ +*.env diff --git a/app/templates/api_version_3_0/tests/_bootstrap.php b/app/templates/api_version_3_0/tests/_bootstrap.php new file mode 100644 index 0000000..ff80ad1 --- /dev/null +++ b/app/templates/api_version_3_0/tests/_bootstrap.php @@ -0,0 +1,17 @@ + getenv('DB_PASSWORD'), + 'user' => getenv('DB_USER'), + 'database' => getenv('DB_DATABASE'), + 'tablePrefix' => getenv('DB_TABLE_PREFIX'), + 'driver' => getenv('DB_DRIVER'), + 'port' => getenv('DB_PORT'), + 'schema' => getenv('DB_SCHEMA'), + 'server' => getenv('DB_SERVER'), +]; diff --git a/app/templates/api_version_3_0/tests/_craft/config/general.php b/app/templates/api_version_3_0/tests/_craft/config/general.php new file mode 100644 index 0000000..ccaba95 --- /dev/null +++ b/app/templates/api_version_3_0/tests/_craft/config/general.php @@ -0,0 +1,5 @@ + true, +]; diff --git a/app/templates/api_version_3_0/tests/_craft/config/routes.php b/app/templates/api_version_3_0/tests/_craft/config/routes.php new file mode 100644 index 0000000..ca5d8ed --- /dev/null +++ b/app/templates/api_version_3_0/tests/_craft/config/routes.php @@ -0,0 +1,5 @@ + plugin for Craft CMS 3.x + * + * <%- pluginDescription %> + * + * @link <%= pluginAuthorUrl %> + * @copyright <%- copyrightNotice %> + */ + +use Codeception\Actor; +use Codeception\Lib\Friend; + +/** + * Inherited Methods + * + * @method void wantToTest($text) + * @method void wantTo($text) + * @method void execute($callable) + * @method void expectTo($prediction) + * @method void expect($prediction) + * @method void amGoingTo($argumentation) + * @method void am($role) + * @method void lookForwardTo($achieveValue) + * @method void comment($description) + * @method Friend haveFriend($name, $actorClass = null) + * + * @SuppressWarnings(PHPMD) + * + */ +class FunctionalTester extends Actor +{ + use _generated\FunctionalTesterActions; + +} diff --git a/app/templates/api_version_3_0/tests/_support/Helper/Functional.php b/app/templates/api_version_3_0/tests/_support/Helper/Functional.php new file mode 100644 index 0000000..eda9165 --- /dev/null +++ b/app/templates/api_version_3_0/tests/_support/Helper/Functional.php @@ -0,0 +1,24 @@ + plugin for Craft CMS 3.x + * + * <%- pluginDescription %> + * + * @link <%= pluginAuthorUrl %> + * @copyright <%- copyrightNotice %> + */ + +namespace Helper; + +use Codeception\Module; + +/** + * Class Functional + * + * Here you can define custom actions. + * All public methods declared in helper class will be available in $I + */ +class Functional extends Module +{ + +} diff --git a/app/templates/api_version_3_0/tests/_support/Helper/Unit.php b/app/templates/api_version_3_0/tests/_support/Helper/Unit.php new file mode 100644 index 0000000..20e5d04 --- /dev/null +++ b/app/templates/api_version_3_0/tests/_support/Helper/Unit.php @@ -0,0 +1,25 @@ + plugin for Craft CMS 3.x + * + * <%- pluginDescription %> + * + * @link <%= pluginAuthorUrl %> + * @copyright <%- copyrightNotice %> + */ + +namespace Helper; + +use Codeception\Module; + +/** + * Class Unit + * + * Here you can define custom actions. + * All public methods declared in helper class will be available in $I + * + */ +class Unit extends Module +{ + +} diff --git a/app/templates/api_version_3_0/tests/_support/UnitTester.php b/app/templates/api_version_3_0/tests/_support/UnitTester.php new file mode 100644 index 0000000..6be8113 --- /dev/null +++ b/app/templates/api_version_3_0/tests/_support/UnitTester.php @@ -0,0 +1,35 @@ + plugin for Craft CMS 3.x + * + * <%- pluginDescription %> + * + * @link <%= pluginAuthorUrl %> + * @copyright <%- copyrightNotice %> + */ + +use Codeception\Actor; +use Codeception\Lib\Friend; + +/** + * Inherited Methods + * + * @method void wantToTest($text) + * @method void wantTo($text) + * @method void execute($callable) + * @method void expectTo($prediction) + * @method void expect($prediction) + * @method void amGoingTo($argumentation) + * @method void am($role) + * @method void lookForwardTo($achieveValue) + * @method void comment($description) + * @method Friend haveFriend($name, $actorClass = null) + * + * @SuppressWarnings(PHPMD) + * + */ +class UnitTester extends Actor +{ + use _generated\UnitTesterActions; + +} diff --git a/app/templates/api_version_3_0/tests/example-env b/app/templates/api_version_3_0/tests/example-env new file mode 100644 index 0000000..bda549b --- /dev/null +++ b/app/templates/api_version_3_0/tests/example-env @@ -0,0 +1,11 @@ +# Set in accordance to your environment + +DB_DRIVER="mysql" +DB_SERVER="localhost" +DB_USER="root" +DB_PASSWORD="" +DB_DATABASE="craft-test" +DB_SCHEMA="" +DB_TABLE_PREFIX="craft" +DB_PORT="3306" +SECURITY_KEY="abcde12345" diff --git a/app/templates/api_version_3_0/tests/functional.suite.yml b/app/templates/api_version_3_0/tests/functional.suite.yml new file mode 100644 index 0000000..11e3c08 --- /dev/null +++ b/app/templates/api_version_3_0/tests/functional.suite.yml @@ -0,0 +1,5 @@ +actor: FunctionalTester +modules: + enabled: + - \craft\test\Craft + - \Helper\Functional diff --git a/app/templates/api_version_3_0/tests/functional/ExampleFunctionalCest.php b/app/templates/api_version_3_0/tests/functional/ExampleFunctionalCest.php new file mode 100644 index 0000000..6037f0e --- /dev/null +++ b/app/templates/api_version_3_0/tests/functional/ExampleFunctionalCest.php @@ -0,0 +1,24 @@ +\<%= pluginDirName %>tests\acceptance; + +use Craft; +use FunctionalTester; + +class ExampleFunctionalCest +{ + // Public methods + // ========================================================================= + + // Tests + // ========================================================================= + + /** + * + */ + public function testCraftEdition(FunctionalTester $I) + { + $I->amOnPage('?p=/'); + $I->seeResponseCodeIs(200); + } +} diff --git a/app/templates/api_version_3_0/tests/functional/_bootstrap.php b/app/templates/api_version_3_0/tests/functional/_bootstrap.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/app/templates/api_version_3_0/tests/functional/_bootstrap.php @@ -0,0 +1 @@ + plugin for Craft CMS 3.x + * + * <%- pluginDescription %> + * + * @link <%= pluginAuthorUrl %> + * @copyright <%- copyrightNotice %> + */ + +namespace <%= pluginVendorName %>\<%= pluginDirName %>tests\unit; + +use Codeception\Test\Unit; +use UnitTester; +use Craft; +use <%= pluginVendorName %>\<%= pluginDirName %>\<%= pluginHandle %>; + +/** + * ExampleUnitTest + * + * + * @author <%- pluginAuthorName %> + * @package <%= pluginHandle %> + * @since <%= pluginVersion %> + */ +class ExampleUnitTest extends Unit +{ + // Properties + // ========================================================================= + + /** + * @var UnitTester + */ + protected $tester; + + // Public methods + // ========================================================================= + + // Tests + // ========================================================================= + + /** + * + */ + public function testPluginInstance() + { + $this->assertInstanceOf( + <%= pluginHandle %>::class, + <%= pluginHandle %>::$plugin + ); + } + + /** + * + */ + public function testCraftEdition() + { + Craft::$app->setEdition(Craft::Pro); + + $this->assertSame( + Craft::Pro, + Craft::$app->getEdition() + ); + } +} diff --git a/app/templates/api_version_3_0/tests/unit/_bootstrap.php b/app/templates/api_version_3_0/tests/unit/_bootstrap.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/app/templates/api_version_3_0/tests/unit/_bootstrap.php @@ -0,0 +1 @@ +