Symfony automatic response issue - php

I just created a custom class that will do more commands. This command will be launched by a robot in the future.
But when I do a
$command_fixtures_load = $this->getApplication()->find('doctrine:fixtures:load');
$arguments_fixtures_load = array(
'command' => 'doctrine:fixtures:load',
'--fixtures' => "src/XXX/XXXBundle/Tests/DataFixtures"
);
$input_fixtures_load = new ArrayInput($arguments_fixtures_load);
$command_fixtures_load->run($input_fixtures_load, $output);
I have this :
Careful, database will be purged. Do you want to continue Y/N ?
So the script waits for an answer, or I would give no answer but it makes the load Fixtures fully automatic.
How?

Running
php app/console doctrine:fixtures:load --no-interaction
Will purge your database without asking the question.
To see more options run
php app/console doctrine:fixtures:load --help

Related

Laravel Process can't find Node

I am developing a Laravel (8.83.24) app and testing with Laravel's built in server "php artisan serve". I am using Process and am trying to debug a memory allocation error which has lead me to here:
$process = new Process(['node', '-v']);
$process->run();
print_r($process);
That leads me to a line in the output:
[command] => cmd /V:ON /E:ON /D /C (node -v) 1>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.out" 2>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.err"
and sf_proc_01.err contains:
'node' is not recognized as an internal or external command,
operable program or batch file.
Clearly node can't be found. I am on Windows 10. Node is set in my in System and User PATH. Running "node -v" from cmd.exe works and returns the version number. So Laravel's server doesn't seem to be able to find node. However, I can't check the path as running this:
$process = new Process(['echo', '%PATH%']);
$process->run();
print_r($process);
Just leads to an output of
"%PATH%"
How can I make Node findable by Laravel / Process?
Many thanks for your help.
I eventually solved this by setting the path for node in my Laravel .env file:
NODE_PATH='C:\PROGRA~1\nodejs\node.exe'
NPM_PATH='D:\Work\Project\node_modules'

Codeception & Symfony - run Doctrine migrations before tests

I have a Symfony 4 application and Doctrine with Doctrine migrations. I'm introducing Codeception for running API tests, and need to run migrations before the tests run. Since I'm using the Doctrine2 module I don't really want to be also including the DB module as it's not needed for the tests and would require configuring the test database in two different locations.
I am using the Symfony module currently, and I noticed that the Laravel module has a run_database_migrations configuration option.
What is the best way to handle running the Doctrine migrations command in a Symfony app prior to the tests? (bin/console doctrine:migrations:migrate -n is the specific command).
Edit I've got a solution that, although it works, is nowhere near ideal. By using Codeception Customisation I've created the following extension that basically manually execs the underlying Symfony commands.
class DatabaseMigrationExtension extends Extension
{
public static $events = [
Events::SUITE_BEFORE => 'beforeSuite',
];
public function beforeSuite(SuiteEvent $e)
{
echo(exec('bin/console doctrine:database:drop --force') . PHP_EOL);
echo(exec('bin/console doctrine:database:create') . PHP_EOL);
echo(exec('bin/console doctrine:migrations:migrate -n') . PHP_EOL);
}
}
Edit 2 The goal of this is basically to replicate similar functionality to what the Codeception DB module does, which allows you to provide an SQL dump of a database that it automatically uses in the tests, but instead use Doctrine migrations to handle the DB. - https://codeception.com/docs/modules/Db#sql-data-dump
I spent a while trying a couple of different ways to achieve this. I initially used RunProcess however this seemed to cause sporadic issues with the DB being deleted and not recreated, despite using the sleep configuration. I ended up just updating the existing extension to use the CLI module instead, and it works as desired (without having to create scripts or run multiple commands) and without having to use exec.
Final extension;
class DatabaseMigrationExtension extends Extension
{
public static $events = [
Events::SUITE_BEFORE => 'beforeSuite',
];
public function beforeSuite()
{
try {
/** #var \Codeception\Module\Cli $cli */
$cli = $this->getModule('Cli');
$this->writeln('Recreating the DB...');
$cli->runShellCommand('bin/console doctrine:database:drop --if-exists --force');
$cli->seeResultCodeIs(0);
$cli->runShellCommand('bin/console doctrine:database:create');
$cli->seeResultCodeIs(0);
$this->writeln('Running Doctrine Migrations...');
$cli->runShellCommand('bin/console doctrine:migrations:migrate --no-interaction');
$cli->seeResultCodeIs(0);
$this->writeln('Test database recreated');
} catch (\Exception $e) {
$this->writeln(
sprintf(
'An error occurred whilst rebuilding the test database: %s',
$e->getMessage()
)
);
}
}
}
and registered;
// codeception.yml
extensions:
enabled:
- \DatabaseMigrationExtension
Output (-vv or higher also displays the output of the DB & Migration commands);
I always create a bash script to do this, or a Makefile.
bash command
My ./runtests.sh scripts contains
#!/bin/bash
./bin/console command:for:migrations
./bin/phpunit
Makefile
Same with Makefile
.FOO: testsuite
testsuite:
./runtests.sh
or
.FOO: testsuite
testsuite:
./bin/console command:for:migrations
./bin/phpunit
why I prefer Makefile
Recently I added this script in my .bash_profile that allow me to autocomplete from bash all target made in makefile (very easy because you dont need anymore to remember all commands, but just make and tab).
complete -W "`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' Makefile | sed 's/[^a-zA-Z0-9_.-]*$//'`" make
Thus, .. you can create target like:
runtests
runtests_with_fixtures
migrations
runtests_with_migrations
...
and so on
My suggestion is to create your custom and easy way to run commands.
Here a way to run all or just one command usgin make
.FOO: dropforce
dropforce:
bin/console doctrine:database:drop --force
.FOO: dbcreate
dbcreate:
bin/console doctrine:database:create
.FOO: migrate
migrate:
bin/console doctrine:migrations:migrate
.FOO: suite
suite: dropforce dbcreate migrate
With Codeception 4 you can do it without Cli module:
$symfony = $this->getModule('Symfony');
$symfony->runSymfonyConsoleCommand('doctrine:database:drop',['--if-exists'=>true, '--force'=>true]);
$symfony->runSymfonyConsoleCommand('doctrine:database:create');
$symfony->runSymfonyConsoleCommand('doctrine:migrations:migrate', ['--no-interaction'=>true]);

laravel controller shell_exec('composer update');

I would like to execute the following command : composer update in my controller in laravel, however this one does not work.
On the other hand the command composer info works perfectly.
When I execute composer update in a command prompt all my dependencies are correctly updated in the laravel vendor file but when I try to execute composer update in my controller nothing happens.
here is my code :
$data['output'] = shell_exec( 'cd '. base_path() .' && composer update' );
dd($data);
and here is the result :
array:1 [▼
"output" => null
]
Could you help me to understand why composer update does not work in a controller ?
I would like to update my dependencies in a controller without the command prompt.
Thank you.
From the php docs for shell_exec():
The output from the executed command or NULL if an error occurred or the command produces no output.
Sounds like you'd be better off using passthru() instead as that will give you the erroneous output.
Speaking of erroneous, this is idea sounds like a recipe for unmitigated disaster.

Cannot make command line tool for artisan in PHPStorm 10.0.1

I got this error message when I tried to make an alias for artisan:
[Settings | Tools | Command Line Tool Support ] -> add -> tool based on Symfony Console
Problem
Failed to parse output as xml: Error on line 4: Content is not allowed in prolog..
Command
C:\xampp\php\php.exe C:\xampp\htdocs\laratest\artisan list --xml
Output
[Symfony\Component\Console\Exception\RuntimeException]
The "--xml" option does not exist.
Ok, I know, what's the problem but I don't find any solution for this.
Thank you for the tips!
A temporal modification of the "artisan" file under the Laravel folder will do the trick. (Working on PhpStorm 10.0.3)
if( isset($argv[1]) && $argv[1] == 'list' &&
isset($argv[2]) && $argv[2] == '--xml' ) {
$argv[2] = '--format=xml';
$_SERVER['argv'] = $argv;
}
require __DIR__.'/bootstrap/autoload.php';
Now you can add the "artisan" command line tool support based on Symfony and remove the lines if you wish to.
For everybody affected, this is the commit which removed support for –xml: https://github.com/symfony/console/commit/6d6d9031b9148fed0e2aacb98ac23ce6168ba7ac
Just revert changes in ListCommand.php
it work in 2.7 version
There is no --xml option, you are getting this error when you running this command:
The "--xml" option does not exist.
So what you should do in this case is running:
php artisan help list
and you will get list of all available parameters
and now you will know you need to use:
php artisan list --format=xml
instead of:
php artisan list --xml
EDIT
I've verified it in PhpStorm 10.0.3
as Tool path you can paste in your case:
C:\xampp\php\php.exe C:\xampp\htdocs\laratest\artisan list --format=xml
and it will work
Update composer before add command line tool:
composer update

symfony2 application, vagrant & ant: stty: standard input: Invalid argument

I am trying to move my development environment (symfony2 application) from my windows 7 localhost to a virtual machine using vagrant and the default ubuntu 10.04 64 bit machine. Everything is set up and it almost works, but there is one thing bothering me:
When I run ant and it executes phpunit, I get the following error while executing my selfmade bootstrap:
stty: standard input: Invalid argument
I could narrow the problem down to the following line of code, which executes the symfony cache:warmup command:
executeCommand($application, "cache:warmup");
This executes the following command:
php app/console -e test -q cache:warmup
Running phpunit without ant works fine, so does running ant without the executeCommand line.
I read a bit about this stty error and looked up ~/.bashrc, ~./profile, /etc/bash.bashrc, /etc/profile as well as /root/.bashrc and /root/.profile without finding anything like tty or stty. SO I don't know what I could delete to make it work.
I am a bit stuck as I need the cache warmup and cannot figure out what is going wrong.
This took a while to figure out, but I now got it.
For some reason, the options passed to the symfony2 application object causeing the issue only when run by ant.
I don't have any insight on what causes it, but changing the command to this fixes the issue:
php app/console --env=test --quiet cache:warmup
As this is only the long form and does not change anything, I'm very happy. My whole executeCommand function looks like this:
function executeCommand($application, $command, Array $options = array()) {
$options["--env"] = "test";
$options["--quiet"] = true;
$options = array_merge($options, array('command' => $command));
$application->run(new ArrayInput($options));
}
The only lines changes are 2 and 3 where the key for the array was changed from -eand -q. I hope this helps one or another who struggles with an issue like this!

Categories