, (*1)
how-to-bundle
Step-by-step tutorial how to prepare Symfony bundle, (*2)
Prepare repository
Prepare bundle repository in the way you like. You can use Github or local repository.
Be advised that repository need to be accessible by your future project.
With Github wizard you can add README.md and LICENSE
, (*3)
Clone repository
git clone https://github.com/kjonski/how-to-bundle.git
and open with editor., (*4)
Add composer.json
file
{
"name": "kjonski/how-to-bundle",
"type": "symfony-bundle",
"description": "Step-by-step Symfony bundle tutorial",
"keywords": ["php", "symfony", "reusable bundle", "tutorial"],
"license": "MIT",
"authors": [
{
"name": "Karol Jonski",
"email": "kjonski@pgs-soft.com"
}
]
}
commit and push changes., (*5)
If you want develop bundle inside existing project and unless bundle isn't in packagist.org please configure main project's composer.json
.
Add/modify repositories
section:, (*6)
"repositories": [
{
"type": "git",
"url": "https://github.com/kjonski/how-to-bundle.git"
}
],
when using Github repository, or:, (*7)
"repositories": [
{
"type": "git",
"url": "/path/to/how-to-bundle.git"
}
],
when using local repository. For more details @see Composer documentation., (*8)
Install you bundle with composer
$ composer require kjonski/how-to-bundle
Prepare structure
b$
means vendor/yourVendorName/yourBundle (vendor/kjonski/how-to-bundle
) directory., (*9)
b$ mkdir src
b$ mkdir tests
"autoload": {
"psr-4": { "Kjonski\\HowToBundle\\": "src/" }
},
"autoload-dev": {
"psr-4": {
"Kjonski\\HowToBundle\\Tests\\": "tests/"
}
}
Install dependencies
b$ composer require symfony/dependency-injection
b$ composer require --dev symfony/http-kernel
b$ composer require --dev phpunit/phpunit
Add bundle class
assertInstanceOf(KjonskiHowToExtension::class, $bundle->getContainerExtension());
}
}
```
and run from bundle directory:
```console
b$ vendor/bin/phpunit tests
```
At any point of time from now you can:
```console
$ composer remove kjonski/how-to-bundle
```
and
```console
$ composer require kjonski/how-to-bundle
```
and your bundle will be reinstaled.
Be advised that you need to run:
```console
b$ composer install
```
to install your dev dependencies.
## Add tests configuration (`tests/phpunit.xml`)
```xml
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.0/phpunit.xsd"
backupGlobals='false'
backupStaticAttributes='false'
beStrictAboutTestsThatDoNotTestAnything='true'
bootstrap='./../vendor/autoload.php'
colors='true'
convertErrorsToExceptions='true'
convertNoticesToExceptions='true'
convertWarningsToExceptions='true'
stopOnError='false'
stopOnFailure='false'
stopOnIncomplete='false'
stopOnSkipped='false'
verbose='true'
>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="\Kjonski\HowToBundle\Tests\App\AppKernel" />
<ini name='display_errors' value='1' />
<ini name='display_startup_errors' value='1' />
<ini name='error_reporting' value='-1' />
<ini name='memory_limit' value='-1' />
</php>
<testsuites>
<testsuite name="How To Bundle Test Suite">
<directory>.</directory>
</testsuite>
</testsuites>
<logging>
<log type='coverage-clover' target='coverage.xml' />
<log type='coverage-text' target='php://stdout' showOnlySummary='true' />
</logging>
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
- You can see KERNEL_CLASS configured because for some cases you will need own kernel for phpunit.
- Please add
tests/coverage.xml
to ignored files.
Now you are ready to run your test suite:, (*10)
b$ vendor/bin/phpunit -c tests/phpunit.xml
Install:, (*11)
b$ composer require --dev phpstan/phpstan
b$ composer require --dev sebastian/phpcpd
b$ composer require --dev squizlabs/php_codesniffer
b$ composer require --dev friendsofphp/php-cs-fixer
add php-cs-fixer.config.php, phpstan.neon (optional) and run:, (*12)
b$ vendor/bin/php-cs-fixer fix --config=tests/php-cs-fixer.config.php --dry-run --diff src tests
b$ vendor/bin/phpcs --report-full --standard=PSR2 src tests
b$ vendor/bin/phpstan analyse --level=4 src -c tests/phpstan.neon
b$ phpdbg -qrr vendor/bin/phpunit -c tests/phpunit.xml
Build? Oh, yes!
More badges?
- Go to https://scrutinizer-ci.com/ and Sign Up with Github
- Add repository
- Update configuration
yaml
build:
nodes:
analysis:
...
tests:
override:
...
-
command: vendor/bin/phpunit -c tests/phpunit.xml
coverage:
file: 'tests/coverage.xml'
format: 'clover'
...
to enable code coverage.
- Run inspection
- Get badges from summary screen
Go live with packagist
- Go to https://packagist.org and submit your bundle
- Configure GitHub Service Hook to keep your package up to date
- Remember to remove your local/Github repository from project's
composer.json
repositories section
Sources
https://getcomposer.org/doc/05-repositories.md#loading-a-package-from-a-vcs-repository
https://symfony.com/doc/current/bundles.html#creating-a-bundle
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet, (*13)