How to implement your own Faker provider in Symfony - php

In order to reduce some code duplication in my fixture classes, I would like to create a custom provider for Faker (fakerphp/faker) in my Symfony 5 application. Where can I create a custom faker provider in my application? Please note that I am new to developing in Symfony.
Related to this topic I have found this question for Laravel, but of course Symfony has a different project setup.
Reading up to the documentation of a related library, I would expect I could use $faker->addProvider(), but then again I do not know where this code should live in my Symfony application.
I fantasize I should make a Service for this functionality, or maybe I should just add it to my BaseFixture class? But these are just wild guesses for me.

Where you put your provider is a subjective question. Some use src/Test/ (or namespace App\Test) for test related code that is not actual tests. Others will place it next to the tests somewhere in the tests/ directory, e.g. tests/Faker/ (with namespace App\Tests\Faker). The benefit of the latter is, that the classes will only be picked up by autoload-dev and not in production.
Regarding the second part of your question, how to add the provider to Faker that depends on how you use it in your tests or more precisely, if you want to fetch a shared faker instance from your container or manually create one. Especially if your provide relies on other services like Doctrine you probably want to register it in the container or you should rethink your provider as relying on the database might cause issues, e.g. when you reset your test database, the data pool for your provider might be gone.
I would go for creating a faker instance in your tests as the cost of initiating Faker is relatively low, it should not noticeably slow down your tests if done multiple times, and you will be forced to avoid tying the provider to services from your production code accidentally. You will also have more control over how you create your faker instance and which providers you want to load for specific tests, if that is relevant for you. For instance you could create an instance in the setUp method of your tests:
class MyTest extends TestCase
{
private $faker;
protected function setUp(): void
{
// The Faker\Factory will create a ready to use Faker Generator
$this->faker = Factory::create();
$this->faker->addProvider(new MyCustomProvider());
}
public function testSomething(): void
{
$title = $this->faker->title;
}
}
The Factory will create a new Faker\Generator with all the default providers already registered. You can look at the code if you'd rather only have your own providers in there. Instead of copying this code in each test you can share it, e.g. by creating your own TestCase-class that your tests extend instead of the one provided by PHPUnit or you could use a trait. I think either approach is fine, but probably having your own base TestCase if you are not yet familiar with Traits.
With the setUp method above you can also look at how Symfony's Service Container works, especially how to create services through a factory and then, for example create a config/services_test.yaml where you create a faker instance that you can get in tests extending the WebTestCase or KernelTestCase after booting your Symfony kernel.

Related

How to test a controller that communicate with a microservice?

I have the next scenario: I have to write some unit testings for controllers. In the controllers i have all types of CRUD actions. The problem is that in actions of post/patch/delete my api is communicating with a web service where i pass some data. The communication is done with guzzle.
Question: How to avoid the communication when i call a route? Is there other possibility to test the hall controller?
I assume that your microservice is a class which is registered in the Laravel's service container.
As far as I know, you can't mock classes outside the container in Laravel.
So, In case you're not doing that, register the service in the container in AppServiceProvider class. And get the microservice from the container in the controller See here for details.
In your unit test, you can make a mock object of your microservice according to your needs and inject it in the container (replacing the real one). for eg.
$this->instance(MyMicroService::class, Mockery::mock(MyMicroService::class, function ($mock) {
// here you tell the mock object which method will be called and what to return and how many times it will be called. it's totally customizable.
$mock->shouldReceive('contactRemoteServer')->once()->andReturnTrue();
}));
and it's easier if you use the mock method in your testcase class, but it does the same thing:
$this->mock(MyMicroService::class, function ($mock) {
$mock->shouldReceive('contactRemoteServer')->once()->andReturnTrue();
});
see the docs for more examples.
NB: In case you don't know, A mock class is a class made especially for the test so it doesn't affect the results and to be controllable in accordance to the test needs.
For example if you want to have a case where your microservice will throw an exception and you want to test the controller's response for that you will have to create a mock class for each unit test.
Create a sample class with the same functionality but I have one question if you are creating unit tests and you don't want to make the guzzle work so what will you test.
Please read the documentation carefully you will get the option.
In my suggestion, you can use mocking.

Where to put business logic in Lumen?

I am developing my first API with Lumen. Normally I am using services for separating business logic or reused code from the controllers and share it with other controllers.
How to do this with lumen? Where to put the services? I only see ServiceProviders to register these services but for me it's not clear where and how to define them.
Lumen and its big brother Laravel come with a service container, which handles the dependencies injection.
To resolve things out of the container, you may either type-hint the dependency you need on a class that is already automatically resolved by the container, such as a route Closure, controller constructor, controller method, middleware, event listener, or queued job. Or, you may use the app function from anywhere in your application:
$instance = app(Something::class);
That's for "resolving things out". Registering the "things" is what the service providers are for. A service provider is just a class that extends Illuminate\Support\ServiceProvider and binds interfaces or classes to concrete implementations. (Read the docs for a detail on how to write your own.)
Example:
Create some test route:
$app->get('/test', 'TestController#test');
and create the controller method, type-hinting a parameter:
public function test(DatabaseManager $dbm)
{
dd($dbm);
}
You will see that the DatabaseManager interface is resolved to a concrete class, properly instantiated and configured with your DB config. That's because at some point the framework is calling a service provider which takes care of doing that.
Any custom providers you may want to include are set in /bootstrap/app.php like so:
$app->register(App\Providers\AuthServiceProvider::class);
(Otherwise if you ask for a class that hasn't been bound by a provider, the framework just injects a new instance of that class.)
So, for this problem you probably want some repository class where you can encapsulate all database access.
Example:
// app/Repositories/ProductRepository.php
private $db;
public function __construct(DatabaseManager $dbm)
{
$this->db = $dbm->connection();
}
public function findById($id)
{
return $this->db->table('products')->where('id', '=', $id)->get();
}
//routes.php
$app->get('products/{id}', 'ProductsController#show');
//ProductsController.php
public function show(ProductRepository $repo, $id)
{
$product = $repo->findById($id);
dd($product);
}
It's interesting in this example that you call for a ProductRepository injection, and, since it has a DatabaseManager dependency, the framework handles the instantiation of both.
I hope this begins to answer your question about managing business logic in service providers. I guess another typical use case is authorization handling. You can follow the docs on this subject after this intro.
Services as a Service class? Service classes are not part of a framework, it's more like an application architecture problem you are trying to solve here.
Depending on the project you are working on, either a Services folder in the app folder (if you go for the folders by type structure) or the feature folder it belongs to (if you go for app folders by feature style). These are just 2 of the many possible ways for folder structures.
It's different for every project so, it's up to you to decide where to put the service classes and how you are going to structure your app.
Remember to stick to one convention throughout the project development cycle. If you can't think of it right now, structure your classes later in the refactoring sessions. I usually get a lot more ideas when I am working on something else rather than at the start when I am thinking about it.

Testing behaviour of classes with deep nested dependencies

I am starting using Behat and I was thinking what is the correct way to do something like this:
I am testing system behaviour with Behat (the domain, not web) and i want to test behaviour that uses UserService class. UserService takes UserRepository (Interface) as an argument. Should i create the UserService object in the context or should i take it from some dependency injection container (which i currently don't have since i want to model the domain first)? Or should i maybe create a factory to construct the UserService? Should I mock those dependencies?
The problem here is, that there may be multiple implementations of UserRepository and i want to test the one the system uses (hence taking UserService from DI container). I do not know which implementation i am going to use at the time of writing the feature file/context. Probably just some dummy FilesystemUserRepository just to pass the tests.
If I used the FilesystemUserRepository in contexts, then after i decide to move to DatabaseUserRepository later in development i would have to rewrite all the contexts that use UserService class.
Is there some best practice how to do this?
I usually start with creating everything in my context's constructor, as it's a phase I move back and forward a lot and I don't want to be distracted by having to create service definitions. At this phase code might change a lot, so it would be rather inefficient anyway.
Later on, I create service definitions in the application's container. With the Symfony container it is very easy to inject those services directly into your context. Another advantage is that with Symfony environments you can actually replace some services in test environment. I actually use this a lot, as I prefer to use in-memory repositories in acceptance tests, rather than those based on a database.
On some projects I didn't take the second step, and left all the service initialization in Behat's context class. It worked well too.

Dependency resolving design pattern

I am looking for a php implementation or a design pattern something like this (just a very basic skeleton example):
namespace Contract {
interface Application {...}
interface EntryPoint {...}
}
namespace RestApi {
class Module {
/** #return Contract\EntryPoint */
public function getEntryPoint(Contract\Application $application){...}
}
class EntryPoint implements Contract\EntryPoint {...}
}
namespace BusinessLogic {
class Module {
/** #return Contract\Application */
public function getApplication(){...}
}
class Application implements Contract\Application {...}
}
$dependencyResolver = new DependencyResolver();
$dependencyResolver->parse(new RestApi\Module());
$dependencyResolver->parse(new BusinessLogic\Module());
$dependencyResolver->invoke(function (Contract\EntryPoint $entryPoint){
$entrypoint->handleRequest();
});
I want to loose couple every modules I am using in my application, so I intend to design interfaces, maybe abstract classes with validation to make a well defined interface for every module type. I haven't found a solution for this problem, ppl usually inject things with IoC container, which does not tell anything about the common interface between two modules...
Do you know a design pattern which solves this problem, or a de factor standard php implementation/framework for this?
note: IoC container is not a solution, I want to inject the dependencies with well defined interfaces, not to pull them from a DI container or service locator... I don't want my modules to know anything about how they get their dependencies...
edit:
I updated my question because I don't think my code was obvious for everyone. A created a class diagram to ease the understanding of it:
Okay, so this diagram contains a description of a single environment. We usually use different environments for example: test, development, production, etc... Each of these environment contains different modules, for example by testing and developing we usually turn off email sending, so one of the modules contains a mock php mailer in those environments... As you see by cross-module dependency the classes depend on contracts, not directly on each other. This way the code of the modules is loose coupled...
I intend to describe this dependencies (for example with annotations) and inject them on an automated way somehow. This is a difficult task, because by PHP I have to load only the classes which are necessary to handle the request. For example:
So I have to use lazy load somehow, for example I could inject factories, but I don't like that idea, I want to inject the dependency itself, not a factory...
Be aware that the contract interface does not know anything about its implementations, so I have to publish somehow those implementations by every module and find them from the other modules... Most of the DI container implementations solve that problem, but I don't want to inject factories or DI containers into my modules. I want them to depend on the contracts only and nothing else...
Matthieu Napoli recommended to use just a single IoC container with different config by each of the environments, but I don't see how this would solve my problem. That DI container would instantiate every class with cross-module dependency, so I would move the whole config of every modules into a huge main config file. Even by a simple project I'd have about 20 classes (languages, users, identification-factors, user-identification-factors, contacts, user-contacts, roles, user-roles, permissions, role-permissions, articles, comments, etc...) and at least 3 modules (presentation layer, business logic layer, data access layer). So that main config would contain the instantiation of at least 60 classes from different modules... That would be pretty hard to maintain and I am almost certain that it would result a lot of code repetition by configs of different environments... Yes maybe splitting the config files can reduce the pain, but I guess I wont know the advantages and drawbacks of this solution until I start to use it on a complicated project. Another problem with this approach that how should I implement the lazy load without injecting the IoC container itself into every class with cross-module dependency? I think I need proof or example code, that this approach really works well by this problem domain...
Currently I am thinking on something similar than require.js does with AMD javascript modules, but that injects the IoC container as well by lazy load. You have to use the require("moduleName") from inside the modules if you want to load a dependency which you don't want to use in your code, just in case when it's really necessary. Ofc that require is just a sugar syntax of container.get("moduleName")... Currently I don't see how to solve that problem. I think every of my modules should have one or more DI containers, call them module containers. Those module containers can handle the cross-module dependencies. By lazy load the module containers would pull down the cross-module dependencies of each class from a main container, which would automatically register every module container and every class instantiated by them. In this scenario only the module containers would know about the main container if I cannot solve lazy load without injecting a factory or a DI container or a service locator... Ofc this is the last resort, I think I can do the lazy load somehow without injecting the main container into the module containers. Or at least I can do it somehow with a sugar syntax, for example:
class ModuleContainer {
public function setCrossDomainDependency(Contract\Dep $crossDomainDependency){
//...
}
/** #return Contract\Dep */
public function getCrossDomainDependency(){
//...
}
}
My purpose to use the same ModuleContainer instance regardless of having a MainContainer. I'll check the reflection api, maybe I am able to override the getCrossDomainDependency somehow at runtime. If not then I think the only solution to inject factories or the main container... Nah but this is my solution...
How would you solve this problem?
A IoC container is exactly the solution.
I want to inject the dependencies with well defined interfaces, not to pull them from a DI container or service locator
Then don't use the container as a service locator. That's exactly what a DI container is meant for.
Here is your example using PHP-DI:
$containerBuilder = new ContainerBuilder();
$container = containerBuilder->build();
// Configure RestApi\EntryPoint to use for Contract\EntryPoint
$container->set(Contract\EntryPoint::class, \DI\link(RestApi\EntryPoint::class));
// Configure BusinessLogic\Application to use for Contract\Application
$container->set(Contract\Application::class, \DI\link(BusinessLogic\Application::class));
// Run
$entryPoint = $container->get(Contract\EntryPoint::class);
$entryPoint->handleRequest();
Of course my example here is not perfect. I would suggest to separate the configuration part from the execution part. I would put the configuration in a configuration file (look here for how).
Important: Yes, in that example I fetch something from the container. That is exceptional. That should happen only once or twice in your application.
You have to use the container at some point, at the root of your application (i.e. the entry point of the app), to construct the object graph (or dependency graph).
I am not suggesting that you call get() on the container everytime you need a dependency. I am suggesting that you use dependency injection instead, and that you call get() only at the entry point of the application.
Example of defining with a factory:
$container->set(Contract\EntryPoint::class, \DI\factory(function (Container $c) {
return new RestApi\EntryPoint($c->get('some.parameter'));
}));

Symfony 2 architecture in the case of a model class (Services,Dependencie injection)

Recently i moved to Symfony 2 and i have a litte question.
Let's say i have the following Model:
"catalog" which contains "catalogs". The model gets its data from files but also needs a database connection to verify things.
In the Zend framework or other past projects i loaded the dependencies by a static object which forms a kind of "registry".
As i understand, Symfony 2 uses their service pattern (dependencie injection) instead. But how does this apply to my case.
Must i create a service for every model class i use to auto inject all dependencies? Or is it perfectly valid when i create a instance from my object myself and set for example the database connection in my constructor?
To create a service for every class which needs dependencies, seems a little bit overkill to me.
You can certainly create classes and inject dependencies the old-fashion way but take the time to learn the details of creating services. I think you will find:
Adding a new service is trivial. Copy/paste a few lines of configuration, adjust the class, id and maybe some parameters and you are done. Takes much less time than creating the actual class.
Very soon you will progress from just injecting a database connection to injecting other services as well as perhaps some parameters. Do you really want to have to remember to do all that stuff each time you need to new an object?
Using service id's can divorce your controllers from the exact location/name of a class. The first time you need to do some refactoring and maybe move some services into their own bundle or perhaps swap out a service with another you will be glad that you won't need to hunt down all your code and make changes.
S2 is not really focused on "Models". Instead, think in terms of a service named CatalogManager which wraps access to assorted catalog functionality.

Categories