Overwrite Environment Variable Params with export BEHAT_PARAMS - php

I am trying to use environment-variable-behat-params to overwrite the following value in my YAML:
my_profile:
suites:
domain:
contexts:
- MyContext:
- my_config:
setting_setting: data
other_setting_setting: other_data
This is the export command:
export BEHAT_PARAMS='{"suites":{"domain":{"contexts":{"MyContext":{"my_config":{"my_setting":"on"}}}}}}'
And this is the PHP code:
/** #BeforeSuite */
public static function prepare(BeforeSuiteScope $scope) {
$my_settings = $scope->getEnvironment()
->getContextClassesWithArguments()['MyContext'][0];
if (isset($my_settings['my_setting']) && $my_settings['my_setting'] == 'on') {
//do something here
}
}
It seems to match the documentation, but my_setting remains off instead of being updated to on.
If I try to use a key which is at the root level of my_profile like this:
my_profile:
my_config:
setting_setting: data
With this is the export command:
export BEHAT_PARAMS='{"my_config":{"my_setting":"on"}}'
Then I get this error:
In ArrayNode.php line 311:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
Unrecognized option "my_settings" under "testwork"
Exception trace:
Symfony\Component\Config\Definition\ArrayNode->normalizeValue() at /sites/scorecards/vendor/symfony/config/Definition/BaseNode.php:368
Symfony\Component\Config\Definition\BaseNode->normalize() at /sites/scorecards/vendor/symfony/config/Definition/Processor.php:35
Symfony\Component\Config\Definition\Processor->process() at /sites/scorecards/vendor/behat/behat/src/Behat/Testwork/ServiceContainer/ContainerLoader.php:81
Behat\Testwork\ServiceContainer\ContainerLoader->processConfig() at /sites/scorecards/vendor/behat/behat/src/Behat/Testwork/ServiceContainer/ContainerLoader.php:65
Behat\Testwork\ServiceContainer\ContainerLoader->load() at /sites/scorecards/vendor/behat/behat/src/Behat/Testwork/Cli/Application.php:185
Behat\Testwork\Cli\Application->createContainer() at /sites/scorecards/vendor/behat/behat/src/Behat/Testwork/Cli/Application.php:161
Behat\Testwork\Cli\Application->createCommand() at /sites/scorecards/vendor/behat/behat/src/Behat/Testwork/Cli/Application.php:122
Behat\Testwork\Cli\Application->doRun() at /sites/scorecards/vendor/symfony/console/Application.php:148
Symfony\Component\Console\Application->run() at /sites/scorecards/vendor/behat/behat/bin/behat:34
.. how can I fix this?

you just need to remove my_setting from your yaml file
because it super-cedes any environment variable
from the documentation link you provided
in order to specify a parameter in an environment variable, the value must not exist in your behat.yml

Related

Passing custom-header arguments to Knp SnappyBundle

I'm using Knp SnappyBundle in Symfony 4.
I'm having trouble passing the custom-header argument (--custom-header <name> <value>) to wkhtmltopdf through yaml or as option in request.
Here is what I'm trying, which seems to fail:
knp_snappy:
temporary_folder: "%kernel.cache_dir%/snappy"
pdf:
enabled: true
binary: xvfb-run wkhtmltopdf
options:
- { name: 'custom-header', value: '%app_auth_header_name%' '%app_auth_header_token%' }
I've also tried passing the values as an array, but that also fails.
I have solved it partially, unfortunately not by yaml, but direcly in code example:
public function getPdfBinary($url, Pdf $pdfService): PdfResponse
{
$url = urldecode($url);
$res = new PdfResponse($pdfService->getOutput($url, ['custom-header' =>
[ 'X-Authorization' => 'mytoken' ] ]),'output.pdf');
return $res;
yaml expect scalar value of custom-header...

How to get a function name when I hooked it in a php extension

I tried to write a PHP module which is used to detect zend internal function which is called in a php cgi file. Liked code shown below,I want get its name -- 'printf' in my code.
<?php printf("Hello SO!");?>
Now I hooked this function with a function named 'zend_set_user_opcode_handler'.However,I am not able to get the function name which was hooked.(It is 'printf' in this example.) So, what should I do if I want achieve that 'printf' in Function hook_handler()?
Codes here.
int shellhook_handler(ZEND_OPCODE_HANDLER_ARGS){
/* What should I do to catch function name here*/
return ZEND_USER_OPCODE_DISPATCH;
}
PHP_MINIT_FUNCTION(shellhook)
{
REGISTER_INI_ENTRIES();
zend_set_user_opcode_handler(ZEND_DO_FCALL, hook_handler);
return SUCCESS;
}
Hey guys I have got the answer. There are two different methods to achieve hooked function's name.
First, if PHP5 is used, a defining of macro is necessary,because the method depend on the PHP minor version(less than 4 or not).
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
# define OP1_CONSTANT_PTR(n) (&(n)->op1.u.constant)
#else
# define OP1_CONSTANT_PTR(n) ((n)->op1.zv)
#endif
zend_op *opline = execute_data->opline;
zval *fname = OP1_CONSTANT_PTR(opline);
php_printf("FunctionName:%s\n",Z_STRVAL_P(fname));
Second, if PHP7 is used, parameters of shellhook() are not ZEND_OPCODE_HANDLER_ARGS any more. It is replaced by zend_execute_data *execute_data.
zend_execute_data *call = execute_data->call;
zend_function *fbc = call->func;
zend_string *fname = fbc->common.function_name;
php_printf("FunctionName:%s\n",ZSTR_VAL(fname));

Symfony + Doctrine Oracle DateTime format issue

I have a problem with Oracle DateTime type in Symfony 2.7 + Doctrine.
I have a table with a DateTime column which is mapped in Symfony through Doctrine.
When I try to persiste the relative Entity I got the following error:
Could not convert database value "31-MAY-16 03.56.49.000000 PM" to Doctrine Type datetime. Expected format: Y-m-d H:i:s File: .../vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ConversionException.php Line: 63
I cannot change the default format in the Oracle Database.
I previously fixed the problem by modifing the method getDateTimeFormatString() in the vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/OraclePlatform.php and it worked.
However, since I have to use Git and "composer install ..." in order to deploy my application in production environment, all vendors are installed from the Symfony repository; in that way I lose the changes I made in OraclePlatform.php
To solve the problem without touching the verdors libraries, i tried to set the following Oracle ENV variables on httpd init start script but it doesn't work
export NLS_TIME_FORMAT="HH24:MI:SS"
export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
export NLS_TIMESTAMP_FORMAT="YYYY-MM-DD HH24:MI:SS"
export NLS_TIMESTAMP_TZ_FORMAT="YYYY-MM-DD HH24:MI:SS TZH:TZM"
I found a possible solution in the Known Vendor Issues in the Doctrine documentation about PostgreSQL where they suggest to use the VarDateTimeType by overriding the type like this:
use Doctrine\DBAL\Types\Type;
Type::overrideType('datetime', 'Doctrine\DBAL\Types\VarDateTimeType');
Type::overrideType('datetimetz', 'Doctrine\DBAL\Types\VarDateTimeType');
Type::overrideType('time', 'Doctrine\DBAL\Types\VarDateTimeType');
This seams to be the solution, however I have no idea on how to override the type with the code above but mostly where to put the above code.
Do someone have any idea?
Thanks
P.S. I'm using DateTime without timezone
Create a custom DBAL Type extending the DateTimeType and override the convertToPHPValue function (I copied the VarDateTimeType class, which couldn't successfully convert the Date type my Oracle installation was using):
<?php
namespace YourCompany\SomeBundle\Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeType;
class SillyDateTimeType extends DateTimeType
{
/**
* {#inheritdoc}
* #throws \Doctrine\DBAL\Types\ConversionException
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value instanceof \DateTime) {
return $value;
}
$val = \DateTime::createFromFormat('d-M-y H.i.s.u A', $value);
if ( ! $val instanceof \DateTime) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
}
}
Replace $val = \DateTime::createFromFormat('d-M-y H.i.s.u A', $value); with whatever format your installation is returning for those columns.
Then just register it under dbal in config.yml:
# app/config/config.yml
doctrine:
dbal:
types:
sillydatetime: YourCompany\SomeBundle\Doctrine\DBAL\Types\SillyDateTimeType
Now you can use sillydatetime (or whatever you call it) anywhere in your column type specifications.
I fixed the problem following the proposal of this comment in Github. In app/config/config.yml (Symfony 2.3.1) I added the following block:
services:
oracle.listener:
class: Doctrine\DBAL\Event\Listeners\OracleSessionInit
tags:
- { name: doctrine.event_listener, event: postConnect }
I don't know about Symfony/Doctrine, but this is simple to fix with the regular OCI functions:
$conn = oci_connect('username', 'password', 'connection_string');
// get the sysdate...
$select = oci_parse($conn, 'select sysdate from dual');
oci_execute($select);
print_r(oci_fetch_row($select));
// alter the session date format...
$alter = oci_parse($conn, 'alter session set NLS_DATE_FORMAT=\'YYYY-MM-DD HH24:MI:SS\'');
oci_execute($alter);
// get the sysdate again...
oci_execute($select);
print_r(oci_fetch_row($select));
This gives the output:
Array
(
[0] => 10-JUN-16
)
Array
(
[0] => 2016-06-10 13:39:34
)

Symfony write to parameters.yaml from controller

I want to write an integer value from a controller to parameters.yaml. Is that even possible?
Example:
parameters.yaml
parameters:
# ...
counter: 13
SomeController.php
class SomeController
{
public function indexAction()
{
$counter = $this->getParameter('counter');
$counter++;
// now save new counter value to parameters.yaml !??
}
}
Parameters are generally fixed values.
So A better approach is probably writing into an individual yaml file:
http://symfony.com/doc/current/components/yaml/introduction.html
use Symfony\Component\Yaml\Dumper;
const MY_PARAM=13;
//manipulate and do your thing....
$array=['my_param'=>self::MY_PARAM++];
$dumper = new Dumper();
$yaml = $dumper->dump($array);
file_put_contents('/path/to/file.yml', $yaml);
Then you read the file wherever you need it in your application.
use Symfony\Component\Yaml\Parser;
$yaml = new Parser();
$value = $yaml->parse(file_get_contents('/path/to/file.yml'));
Parameters.yml must contain only fixed configuration values ! You should store your counter in database or (i don't like this) in txt file.
But if you really want to edit it. You have to parse the file and search / replace the line ... It's really a bad practice !

Class not found when creating a class with class name being a string

I need to create classes based on the parameter passed to a function. I do it this way:
public function index($source)
{
if(in_array($source, ModuleManager::getAllModules()))
{
$provider = new $source();
if($image)
{
return $provider->getAll(true);
}
else
{
return $provider->getAll(false);
}
}
}
Notice that on line 5 I'm trying to create an object of class $source which will definitely be available. I understand that the above code is actually an eval call. I'm using Laravel 5.2 and the above code returns:
FatalThrowableError in ProcReqController.php line 19:
Fatal error: Class 'Example' not found
In the above error Example can be any class that I made. Now if I hard code the value of $source then it works just fine.
What am I getting that error?
I believe what's happening is PHP gets confused when you try to instantiate a class whose class name is in a variable and it has to do with imports.
Solution 1
Set your $class variable to the fully qualified class name including the namespace and it should work.
In this way, new $class() should work even while including parenthesis.
Solution 2
After further testing, it seems when you instantiate a variable class, it always assumes global namespace.
With this in mind, you can use class_alias to alias each of your classes. In config/app.php, you can add each class to the aliases array.
'aliases' => [
....
'Example' => App\Example::class
]
The autoloader allows you to use classes without fully qualifying them... in the php interactive shell you'll have to manually include classes AND fully qualify them.
if you have a composer project, go to it's directory and do the following to load the Primal color classes:
set_include_path(getcwd().'/vendor/primal/color/lib/Primal/Color/');
include 'Color.php';
include 'Parser.php';
include 'RGBColor.php';
include 'HSVColor.php';
$hello = Primal\Color\Parser::parse('#666');
var_export($hello->toHSV());
/*
returns
Primal\Color\HSVColor::__set_state(array(
'hue' => 0,
'saturation' => 0,
'value' => 37.647058823529413,
'alpha' => 1,
))
*/
Remove the parentheses at the end of the instantiation call, I think.
Check out this php interactive shell session:
php > class Foo { };
php > $fooname = 'Foo';
php > $bar = new $fooname;
php > var_dump($bar);
object(Foo)#2 (0) {
}
src: https://stackoverflow.com/a/4578350/2694851

Categories