I am trying to extend the PHPUnit in Laravel, but I’ve faced a problem - application instance is not loaded. I'll do my best to explain my problem.
I want to implement TestListener interface and TestListenerDefaultImplementation trait to use PHPUnit Listener feature. But also I need to use my models in this listener.
As you can guess I cannot do this because Laravel’s CreateApplication trait is used only in a pair with its BasicTest. Thus the listener does not know anything about the application instance. How can I manage to do this with listeners?
My phpunit.xml file:
https://gist.github.com/andrewgorpenko/1a7d472ab4747f081c7da247261e29d1
This is an intended feature of the framework and what I did is a kind of "bad practice".
Related
Im currently learning how laravel framework builded and came to service provider section. in class DatabaseServiceProvider which extends ServiceProvider class i see this line of code :
$this->app->singleton('db.factory', function ($app)
{
return new ConnectionFactory($app);
});
im confuse what is app in $this->app, whether app is an object from application class instance and if it does when it is instantiated so it can be used from DatabaseServiceProvider class since i dont see its instantiation process. thnks before for answering this newbie question.
In Laravel, the app variable is a service container. Think of it like a heart of the framework. Everything you use from Facades to DB connections is stored in there.
Moreover, you can store your own objects in the container by using the method you specified ($this->app->singleton()). There are actualy many more ways to interact with the container. The best way to research this is to look into Laravel's documentation (Laravel Service Container)
The app object is being instantiated on Laravel bootstrap so you can't see it in the service provider. Though I was curious enough to dig deep into the framework to find it. The class itself is located in Illuminate\Foundation\Application and is instantiated in bootstrap/app.php.
I'm creating a Symfony bundle in isolation, outside of any Symfony installation, to be used across several of my projects. I'm new to this type of workflow in Symfony and I'm a bit confused about how to best approach it.
I know I can write unit tests in order to test the functional side of the bundle, but I've also mapped out about 25 Doctrine entities that I would rather not have to manually define the getters/setters for.
I assumed I'd be able to install the Composer dependancies and then use vendor/bin/doctrine to generate them, but Doctrine throws an error, saying I should define a config-cli.php file, which is intended to instantiate an entity manager, which requires a connection.
That's fine, but given there is no actual database (in theory), I don't want to define a connection. I just want to generate the entities and test my services with PHPUnit, and then load the bundle into an actual Symfony installation later.
Am I going about this wrong? An article explaining the workflow would be very helpful, but I'm not finding anything through Google.
Generation of getters and setters is a common feature of IDEs. You can use the free NetBeans IDE, while some might prefer to dish out some money for PhpStorm. Both IDEs can generate the getters and setters for you.
So it appears that it's not possible to run the generate:* commands without an instance of the entity manager, not even just to generate the getters/setters. Unfortunately that means a connection is required, which is what I'm trying to avoid for this stage of the development.
I've been looking through some of the more popular Symfony bundles out there, trying to figure out how they handle it. From what I can gather the pattern is to define a base, non vendor-specific model in the Model/ namespace using standard DocBlocks for the properties (which any ol' generator can be used to parse and generate the getters/setters for,) then to extend the class in a Doctrine specific entity within the Entity/ namespace.
As Entity\Foo extends Model\Foo, the type hinting would still be valid, and you only need overwrite the property annotations and any methods requiring Doctrine specific code.
Although it's a little more work initially (ignoring the generation process,) I actually like this approach. Not only does it keep the repetitive getter/setter boilerplate code separate from the Doctrine mappings, but it actually follows best practises by de-coupling the models from Doctrine altogether.
This question already has an answer here:
How to use DependencyInjection from symfony in stand alone application with commands?
(1 answer)
Closed last year.
A Symfony novice here. After reading some of the Symfony documentation and some answers here at SO, I am now almost completely confused.
I am trying to use the console application component and create a small db-aware console application.
Many people state that in order to use Symfony's DI features it would be enough to inherit my command class not from Symfony\Component\Console\Command\Command but from ContainerAwareCommand.
However when I try this I get a Method Not Found error on an application::getKernel() call.
I have a feeling that DI features are in fact not available in a console application based on the console component. Is there another kind of Symfony console application, for example, based on the full-blown framework?
I very much like the simple framework provided by the console component Symfony\Component\Console\Application. But the question is then - what to do for dependency injection and DBAL? All examples that I find seem to refer to the full Symfony framework and get me just all the more stuck.
Just a quick update on my progress if anybody stumbles upon the same problems.
I incorporated into my project the PHP-DI dependency injection framework, which seems to be working fairly well with no configuration (so far) - it figures out quite a lot by reflection, actually.
The same way, Doctrine\DBAL is included as a standalone library (I opted against the O/RM part of it, as it is really a tiny project and I'm on a much firmer ground with SQL than anything else) and the connection is simply returned by a connection provider which is injected wherever needed by the DI.
One thing I couldn't figure out is how to have the command classes instantiated by the DI library without my help, so I actually had to inject the container itself into my overridden application class and override the getDefaultCommands() where I then pull the instances out of the container manually. Not ideal but will have to do for now.
If your command extends ContainerAwareCommand
...
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
...
class MyCommand extends ContainerAwareCommand
{
The DI container is available with the getContainer() method. (like in a standard controller), ex:
$this->validator = $this->getContainer()->get('validator');
I don't know if your question is still relevant, but I have an answer as I stumbled across the same problem here.
You just have to create the kernel yourself and give it to the \Symfony\Bundle\FrameworkBundle\Console\Application that extends the basic \Symfony\Component\Console\Application.
<?php
// CronRun.php
require __DIR__.'/../../../../vendor/autoload.php';
require_once __DIR__.'/../../../../app/AppKernel.php';
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$application->add(new \KingdomHall\TaskBundle\Command\CronCommand());
$input = new \Symfony\Component\Console\Input\StringInput('k:c:r');
$application->run($input);
You could use a solution I just pushed it to packagist.org. Includes full working symfony/dependency-injection. You're welcome to give it a shot. use composer to create your own project composer create-project coral-media/crune project_dir or just clone the repository.
https://packagist.org/packages/coral-media/crune
You only need to install DBAL dependencies (I don't suggest ORM if you don't really need it). Configure connection parameters in .env and just define a service to handle connection. That service can be injected in your Commands using public setMyService($myService) method with #required annotation. Also you could create a Connection class and bind is as parameter in your command constructor.The crune boilerplate also supports autowire and autoconfiguring features.
is it possible overwrite/extend Symfony\Component\DependencyInjection\Container::get() method? I want automatic creating service, when it is not contain in container, but class of service exists.
For example:
Name of service is My.MyBundle.Model.FooRepository
Service with this name doesnt exists, but when i call:
$container->get('My.MyBundle.Model.FooRepository');
check class_exists for \My\MyBundle\Model\FooRepository and when its exists, add to container and return it. Dependencies of this new services will be resolve by kutny/autowiring-bundle.
This feature can be extended only for some namespaces or interfaces and in production enviroment can be cached, but for developing will be great helper.
Any idea?
This is not directly answering your question but maybe it's answering your need: if you want to have "auto-wiring" inside your Symfony project, you can use PHP-DI inside Symfony. PHP-DI is an alternative container that can do auto-wiring (which Symfony does not).
Have a look at the Symfony 2 integration documentation to see if it can fit your bill.
Is there a way to use Doctrine using the model classes I've already setup for my Symfony applications without having to call Symfony with all that overhead?
This is more to satisfy a curiosity than anything else. For all the scripts I've used, I've just been able to instantiate Symfony (which typically turns out nice since I have all of the features that I'm used to working with on this particular project. But there has to be a way to load Doctrine and use the Symfony model classes without Symfony... Right?
Doctrine isn't dependet on symfony. Doctrine is a "framework" on its own. It has it's own autoloading and can therefore work with it's classes like a regular PHP app. You can integrate Doctrine with other frameworks if you want (like CodeIgniter or Zend). So you have every freedom you need without the need to do some tedious migration of your models/data/... from one system to another.
I've come to the conclusion there really isn't a way to use the model classes from Symfony elsewhere. With a little work, you can port over the classes to a new Doctrine model (even if you use the generator, since the main model class just extends the base which extends sfDoctrineRecord (from the API docs, you can see which functions will need to be removed).
Otherwise, there isn't a practical way of doing that.
Anytime I need to access the Symfony model, I'm making a task or plugin since I do typically need part of Symfony's functionality.
As far as Symfony2 goes, just looking at the documentation makes me want to run screaming. It's not mature in any form or fashion (but, then again, neither is Symfony "legacy"). So, I'm not sure if the process would be any easier there.