The petrelli\live-statics
package provides a quick way to generate and integrate fake prototypes into our system. Mocked objects can easily be model to behave as the real ones, so you won't have to spend any time with integration tasks.
Both real and mocked data sources will live together so you will be able you to switch between them at any time by simply changing the subdomain.
A great secondary effect is that because our fake objects behave as real, a fully functional 'live static' version of your web application will be accessible to explore and click around.
Faker content can be parametrized, so your 'live statics' will change by just passing some URL parameters. This will come incredibly handy to perform visual QA, client presentations, and simply just to have a glance on how your site will behave with different types of content.
Let's explore a few links:
Notice the Projects list. It's just a simple loop within an Eloquent model collection.
The second link, is also a collection, but instead of Eloquent it's using our mocked models. Views and controller are the same for both URL's.
- Include the package
You can run the command:
composer require petrelli/live-statics
Or directly add it to your composer.json
"petrelli/live-statics": "^0.0.1"
And run composer update
.
- If you have Package Auto-discovery (Laravel 5.5+ by default) skip to step 3, otherwise you'll have to manually add the service provider to your
config/app.php
file.
'providers' => [
//...
Petrelli\LiveStatics\BaseServiceProvider::class,
//...
]
- Publish configuration files and the Service Provider
php artisan vendor:publish --provider="Petrelli\LiveStatics\BaseServiceProvider"
- Generate a new Mocked class (e.g. Project). Run the command:
php artisan live-statics:class Project
This will use the configuration values inside config/live-statics.php
to generate a base mocked class Project
, plus an interface ProjectInterface
that will allow you to bind it to the real or fake implementation.
- Add binding instructions to
config/live-statics.php
'mocked_classes' => [
\App\Interfaces\ProjectInterface::class => [
\App\Mocks\ProjectMock::class, \App\Project::class
],
],
Convention is the following:
'mocked_classes' => [
INTERFACE1 => [ MOCKED_CLASS1, REAL_CLASS1 ],
//...
INTERFACEn => [ MOCKED_CLASSn, REAL_CLASSn ],
]
This will be enough to use your interfaces to inject them properly!
If your real class is not ready yet and you just want a quick prototype, pass null
as the second element of the array.
'mocked_classes' => [
INTERFACE1 => [ MOCKED_CLASS1, null ],
//...
]
Following our project example:
use App\Interfaces\ProjectInterface;
class Project extends Model implements ProjectInterface
{
#...
}
Let's use a controller as an example:
use \App\Interfaces\ProjectInterface;
class Controller extends BaseController
{
public function index(ProjectInterface $model)
{
# Use your Project instance as normal
# $model->all();
# $model->published()->get();
}
}
Here we inject ProjectInterface
to the controller. This will bind the real, or mocked implementation depending on the subdomain.
If you are not confortable injecting dependencies as formal parameters you can use Laravels app
function:
use \App\Interfaces\ProjectInterface;
class Controller extends BaseController
{
public function index()
{
$model = app(ProjectInterface:class);
# Use your Project instance as normal
# $model->all();
# $model->published()->get();
}
}
That's it!
You can change this subdomain modifying the subdomain
option inside config/live-statics.php
.
This is a special case of a general class.
The package will provide a quick way for you to bind models, as most applications will be mainly mocking Eloquent models.
- Generate the mocked model
php artisan live-statics:model Project
- Add binding instructions to
config/live-statics.php
'mocked_models' => [
'Project',
]
Notice instead of mocked_classes
, we use mocked_models
, and just pass the model's name.
The package will use path configurations for models provided on config/live-statics.php
to bind them properly.
Keep your code organized creating namespaces for your mocked elements. Folders will be generated automatically.
This can be easily done passing by a second parameter to both commands:
# Prepend Api to the new class namespace
php artisan live-statics:class Project Api
# Prepend Api\Version1 to the new class namespace
php artisan live-statics:class Project Api\\Version1 #or
php artisan live-statics:class Project Api/Version1
# Prepend Api to the new model namespace
php artisan live-statics:model Project Api
# Prepend Api\Version1 to the new model namespace
php artisan live-statics:model Project Api\\Version1 #or
php artisan live-statics:model Project Api/Version1
When creating models, the namespace specified within config/live-statics.php
will be added automatically.
Docs to be completed.
Docs to be completed.
Docs to be completed.
Docs to be completed.
Docs to be completed.
Docs to be completed.
Docs to be completed.
The MIT License (MIT). Please see License File for more information.