This page explains the steps for developing the basic example package across multiple environments using Git and GitLab. For the complete result of this example, see the GitLab repository. Note that this repository contains content for other examples as well, as the documentation uses a single package for all examples.
On This Page
Development
Unlike the previously created JSON production file, the shared development file is a more flexible PHP script. Besides encapsulating development-related content, this script can execute logic before returning the metadata array, making it useful for various scenarios, such as unit testing.
Let's create a very simple example of unit testing. In the root directory,
alongside the production file, create the shared fusion.dev.php
file with the
following content:
<?php
// optional extra logic
// metadata array
return [
"structure" => [
// directory prefix segments
// relative to package root
"/cache/loadable/tests" => "Valvoid\Package\Tests"
]
];
Note that, similar to the production file's developer input, the order of the
namespace indicators and their corresponding directories is reversed. Additionally,
the directory path is relative to the package root, whereas the
schema representation
is relative to /loadable
. Furthermore, the indicator value can be either a
string or an array of strings.
In addition to the metadata file, in the root directory, create the /tests
directory and within it, create the file Test.php
with the following
lazy code content:
<?php
namespace Valvoid\Package\Tests;
use Valvoid\Package\Package;
class Test
{
public function sayHi(): void
{
// autoloader
$package = new Package;
echo "Hi, lazy unit test here!\n";
$package->sayHi();
}
}
In your terminal, set the working directory to any directory within the package
and execute the following
register
command:
php fusion register
Once done, the cached /loadable
directory will contain, along with the previous
code registry files, the /tests
directory with the lazy.php
file, created by
the inflate
helper task. This exclusion prevents the namespace from being
collected into the common Autoloader.php
because the main register
task binds
only non-nested registry files inside the /loadable
directory.
To run the pseudo unit test, create the test.php
file in the /tests
directory
with the following content:
<?php
use Valvoid\Package\Tests\Test;
$root = dirname(__DIR__);
require "$root/cache/Autoloader.php";
// optional common autoloader and
// excluded individual tests
$autoloader = Autoloader::init(true);
$lazy = require "$root/cache/loadable/tests/lazy.php";
spl_autoload_register(function (string $loadable) use ($root, $lazy)
{
require $root . $lazy[$loadable];
});
$test = new Test;
$test->sayHi();
Running this script should produce the following output:
Hi, lazy unit test here!
Hi, lazy package here!
Then add the script to a GitLab pipeline. Create the .gitlab-ci.yml
file in
the root directory with the following content:
image: php:8.1.0
test:
script:
- php tests/test.php
Finally, to exclude development content from production releases, create the
.gitattributes
file in the root directory with the following content:
* text=auto eol=lf
*.php diff=php
/cache/loadable/tests export-ignore
/tests export-ignore
.gitlab-ci.yml export-ignore
fusion.dev.php export-ignore
.gitattributes export-ignore
.gitignore export-ignore
Local
The local metadata file is similar to
the shared development file described in the previous section, but it is intended
for use on a local machine. To ensure this file is not included in version
control, create a .gitignore
file in the root directory with the following
content:
/cache/*
!/cache/loadable
!/cache/Autoloader.php
fusion.local.php
In addition to the shared development section, both shared and local development files may contain overlay entries - entries that intersect between files.
In the root directory, alongside the production and shared development files,
create the local fusion.local.php
file with the following content:
<?php
return [
"description" => "Basic example package!"
];
Since the local file has the highest priority, it overrides the description
set in
the production fusion.json
file.