PHP 7 - Arrays | Undefined index --> Bug or works as designed? - php-7

I was just testing PHP 7 because of the speed improvements and now I have following code which leads to unpredictable results:
<?php
$_SESSION ['test'] [ 't****' ] = 'Hello';
var_dump($_SESSION);
echo "\n" . $_SESSION [ 'test' ] [ 't****' ];
$var = "test2";
for ($i = 4 ; $i > 0 ; $i--){
$var [$i] = "*";
echo "\n" . $_SESSION['test'][$var];
}
?>
In this example I expect 3 times an undefined index error. The last index should be found and return Hello. This works in PHP 5.6, but not in PHP 7.
In PHP 7 I get following results:
array(1) {
["test"]=>
array(1) {
["t****"]=>
string(5) "Hello"
}
}
Hello
Notice: Undefined index: test* in /in/YANSb on line 12
Notice: Undefined index: tes** in /in/YANSb on line 12
Notice: Undefined index: te*** in /in/YANSb on line 12
Notice: Undefined index: t**** in /in/YANSb on line 12
In PHP 5.6 I get the expected result:
array(1) {
["test"]=>
array(1) {
["t****"]=>
string(5) "Hello"
}
}
Hello
Notice: Undefined index: test* in /in/YANSb on line 12
Notice: Undefined index: tes** in /in/YANSb on line 12
Notice: Undefined index: te*** in /in/YANSb on line 12
Hello
I am able to reproduce the error.
The strange thing is that $var === "t****" returns true when $i == 1, but $_SESSION ['test'] [$var] will return an index undefined error although $_SESSION ['test'] ['t****'] will return Hello. So, was I on a bug hunt today?

Related

In Karate, how can I assert that an array does not contain a matching string?

Let's say I'm writing a Karate test for a service whose response might look like tha following...
{
"messages": [
"The blurfl is wop",
"The zog is ipfy",
"The wuxhat is neet"
]
}
Is there some syntax for match to assert that none of those messages starts with "The baz "? Or is there a more gereral way to test that in Karate?
This worked for me:
* def response =
"""
{
"messages": [
"The blurfl is wop",
"The zog is ipfy",
"The wuxhat is neet"
]
}
"""
* match each response.messages != '#regex ^The baz .+'
You could also do this:
* match each response.messages == "#? !_.startsWith('The baz ')"
And for completeness:
* def filtered = response.messages.filter(x => x.startsWith('The baz '))
* assert filtered.length == 0

getting ReferenceError: testData is not defined when using webdriverio Dataprovider

I am getting the following error when I am trying to use a feature webdriverio comes with for dataprovider:
I have put the following in the wdio.conf.js:
dataProviders: ['./test/dataproviders/dataObject.js'],
and created dataObject.js as the following:
const dataObject = [
{
a: 0,
b: 1,
expected: 1,
description: 'returns 1 when 0 is added 1',
},
{
a: 1,
b: 2,
expected: 3,
description: 'returns 3 when 1 is added 2',
}
];
dataProvider("../specs/smoke/add.spec.js", dataObject);
and in add.spec.js, I have:
describe(verify addition - ${testData.description}, () => {
...
can you tell me what might have been wrong? will I have to import testData from somewhere? but I have read from the following file that testData is a global variable:
https://github.com/webdriverio/webdriverio/pull/4452/files#diff-65fd20fda801d80cbe511bf165b5adac8e324d3897f6bc7090aa13c943eebf8bR76
Can you please help?
[0-0] (node:54912) UnhandledPromiseRejectionWarning: ReferenceError: testData is not defined

Issue with json datasource and datatables

I am trying to use a json retrieved from an external API and saved as a variable.
I keep getting the error:
DataTables warning: table id=pageTable - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
I am initializing the table after the table is built and I have 4 columns both in the head and in the body.
The paging works but the table itself comes out empty.
What am I missing?
function build_table(feed_items){
console.log(feed_items)
var list = "";
for(i=0; i< feed_items.length; i++){
list += '<tr class="text-center">';
list += '<td>'+feed_items[i]["ns2:feedDate"]+'</td>';
list += '<td>'+feed_items[i]["ns2:feedStatus"]+'</td>';
list += '<td>'+feed_items[i]["ns2:feedType"]+'</td>';
list += '<td>'+feed_items[i]["ns2:itemsReceived"]+'</td>';
list += '</tr>';
}
$("tbody").html(list);
$('#pageTable').DataTable({
"pageLength": 10,
data: feed_items,
columns: [
{title: "ns2:feedDate"},
{title: "ns2:feedStatus"},
{title: "ns2:feedType"},
{title: "ns2:itemsReceived"}
],
stateSave: true,
dom: 'lBfrtip',
buttons: [
'excelHtml5',
'csvHtml5',
'pdfHtml5'
]
})
}

Undefined index: attribute_price error after update from 1.7.7.5 to 1.7.7.7

After the update from 1.7.7.5 to 1.7.7.7 the error 'Undefined index: attribute_price' appears.
It fails on line 3418 (1.7.7.7) in Product.php.
'attribute_price' => $row['attribute_price'] ?: null,
Bevore line 3408 (1.7.7.5) there was this code that works:
'attribute_price' => (isset($row['attribute_price']) ? $row['attribute_price'] : null),
It fails only if the feature Combination is disabled.
I don't understand because this code works in the same environment:
$array2 = [
"foo" => "bar",
"bar" => "foo",
];
echo $array2['foos'] ?: "gugus";
PrestaShop version: 1.7.7.7
PHP version: 7.2.24 on Ubuntu
Looks like a core bug to me..
They changed the previous correct statement to the shorthand operator ?: (that states for if...else) without considering that if Combination::isFeatureActive() is not active, $row['attribute_price'] is not set in the query :
if (Combination::isFeatureActive()) {
$sql->select('IFNULL(product_attribute_shop.id_product_attribute,0) id_product_attribute, product_attribute_shop.`price` AS attribute_price, product_attribute_shop.default_on, product_attribute_shop.`ecotax` AS attribute_ecotax');
$sql->leftJoin('product_attribute_shop', 'product_attribute_shop', '(product_attribute_shop.id_product = p.id_product AND product_attribute_shop.id_shop = ' . (int) $id_shop . ')');
} else {
$sql->select('0 as id_product_attribute');
}
You can edit ?: to ?? or restore the 1.7.7.5 code to avoid the notice.

How to turn string output from sql to integer?

I am generating charts using google chart api. I have done it locally and successfully produce the charts. However, when I transfer them to server (cpanel) the charts is ruined. I have detected that the output of sql query for generating the charts value is produced in string format with "6".
This is the code to get the charts' value:
public function gender()
{
$data = DB::table('results')
->select(
DB::raw('gender as gender'),
DB::raw('count(*) as number'))
->groupBy('gender')
->get();
//dd($data);
$array[] = ['Gender', 'Number'];
foreach($data as $key => $value)
{
$array[++$key] = [$value->gender, $value->number];
}
//dd($array);
return view('gender')->with('gender', json_encode($array));
}
In my local, I try to access the data of the sql using dd($data); producing:
Collection {#263 ▼
#items: array:2 [▼
0 => {#264 ▼
+"gender": "female"
+"number": 6
}
1 => {#266 ▼
+"gender": "male"
+"number": 6
}
]
}
I tried accessing in server using same dd($data);
Collection {#260 ▼
#items: array:2 [▼
0 => {#261 ▼
+"gender": "female"
+"number": "6"
}
1 => {#263 ▼
+"gender": "male"
+"number": "6"
}
]
The difference is the number value from server code is in string.
Why this happened and how to fix the problem?
I had a similar issue. It seems it's a PHP issue. Are you running the same PHP versions on local and production?
In any case, you could also explicitly cast it as an UNSIGNED integer. I was able to do that with the following:
$data = DB::table('results')
->selectRaw('gender as gender, CAST(count(*) AS UNSIGNED) as number')
->groupBy('gender')
->get();