CopernicaMarketingSoftware/PHP-CPP — pull request #67 by valmat (2014-04-05 17:23:39+00:00)
Title: add tests for HashMember

What would finally close the issue with HashMember I wrote for it a few additional tests.
In general now HashMember is stable, but there are a few notes.

**1**	.
```c
Php::Value r;
r["key1"] = "val1-1";
r["key1"] = "val1-2";
r["key1"]["key2"] = "val2";
r["key1"]["key3"] = "val3";
return r;
```
The result of the code execution is:
```
'key1' =>
array (
0 => 'val1-2',
'key2' => 'val2',
'key3' => 'val3',
),
```
Somewhat unexpectedly.

If to consider the similar code in php:
```php
$r = array();
$r["key1"] = "val1-1";
$r["key1"] = "val1-2";
$r["key1"]["key2"] = "val2";
$r["key1"]["key3"] = "val3";
var_export($r);
```
Then get the following output:
```
PHP Warning: Illegal string offset 'key2' in /home/valmat/work/PHP-CPP/tests/php/dbg.php on line 18
PHP Warning: Illegal string offset 'key3' in /home/valmat/work/PHP-CPP/tests/php/dbg.php on line 19
array (
'key1' => 'val1-2',
)
```
**2**	Another example does not work correctly
```c
Php::Value r;
Php::Value tmp(Php::Type::Array);
r.set("str2", "example");
r.get("str2").set("str1", r.get("str2"));
r.set("str1", tmp);
```
This entry is equivalent to the following:
```c
Php::Value r;
r["str2"] = "example";
r["str2"]["str1"] = r["str2"];
r["str2"] = Php::Value(Php::Type::Array)
```
output:
```
array (
'str2' => 'example',
'str1' =>
array (
),
)
```
Both these examples are not errors, so I did not include them in the tests.

The following example, from my point of view, is a mistake.
**3**	.

```c
Php::Value test_HashMember_3()
{

Php::Value r, tmp(Php::Type::Array);
//Php::Value tmp;

tmp.set("key2", "val1-2");
r.set("key1", tmp);
r.get("key1").set("key3", "val1-3");

// expect to receive the same as when recording:
//r["key1"]["key2"] = "val1-2";
//r["key1"]["key3"] = "val1-3";

return r;
}
```
(see test PHP-CPP/tests/cpp/include/variables/021-HashMember-3.h, PHP-CPP/tests/php/phpt/variables/021-HashMember-3.phpt)

021-HashMember-3.log:
```
---- EXPECTED OUTPUT
array (
'key1' =>
array (
'key2' => 'val1-2',
'key3' => 'val1-3',
),
)
---- ACTUAL OUTPUT
array (
'key1' =>
array (
'key2' => 'val1-2',
),
)
---- FAILED
```
I agree that the use of methods get and set is not advisable, but still, I think, they do not work correctly may cause to unexpected problems.

---

*PS*
If you run the tests with the option -m (`$ ./test.sh -m`), then Valgrind shows a memory leak in the following test:
```
LEAK Test Php::Value overloaded operators [php/phpt/variables/007-overloaded-operators.phpt]
```
007-overloaded-operators.mem:
```
==28022== Invalid read of size 1
==28022== at 0xE3AE462: TestVariables::overloaded_op(Php::Parameters&) (in /home/valmat/work/PHP-CPP/tests/cpp/extfortest.so)
==28022== by 0xE664F29: Php::Function::invoke(Php::Parameters&) (function.h:58)
==28022== by 0xE64270C: Php::invoke_callable(int, _zval_struct*, _zval_struct**, _zval_struct*, int) (callable.cpp:44)
==28022== by 0x79BA29: ??? (in /usr/bin/php5)
==28022== by 0x7154D7: execute_ex (in /usr/bin/php5)
==28022== by 0x6ED30F: zend_execute_scripts (in /usr/bin/php5)
==28022== by 0x68D654: php_execute_script (in /usr/bin/php5)
==28022== by 0x79D7DD: ??? (in /usr/bin/php5)
==28022== by 0x461BBF: main (in /usr/bin/php5)
==28022== Address 0xeb6ef90 is 0 bytes inside a block of size 79 free'd
==28022== at 0x4C2B60C: free (vg_replace_malloc.c:446)
==28022== by 0x6DBC9F: _zval_ptr_dtor (in /usr/bin/php5)
==28022== by 0xE66B031: Php::Value::~Value() (value.cpp:291)
==28022== by 0xE66FE4A: Php::Value::rawValue() const (value.cpp:1478)
==28022== by 0xE3AE452: TestVariables::overloaded_op(Php::Parameters&) (in /home/valmat/work/PHP-CPP/tests/cpp/extfortest.so)
==28022== by 0xE664F29: Php::Function::invoke(Php::Parameters&) (function.h:58)
==28022== by 0xE64270C: Php::invoke_callable(int, _zval_struct*, _zval_struct**, _zval_struct*, int) (callable.cpp:44)
==28022== by 0x79BA29: ??? (in /usr/bin/php5)
==28022== by 0x7154D7: execute_ex (in /usr/bin/php5)
==28022== by 0x6ED30F: zend_execute_scripts (in /usr/bin/php5)
==28022== by 0x68D654: php_execute_script (in /usr/bin/php5)
==28022== by 0x79D7DD: ??? (in /usr/bin/php5)
==28022==
```