Why is this PHPUnit comparison wrong? ( .. $mock->with($this->identicalTo($foo) ) - php

I have a problem with PhpUnit. I want to use the isIdentical method of PHPUnit to ensure that a method is called with a specific object as a parameter.
Given is a mock object with method "setBar":
$bar = new Bar();
$someMock
->expects($this->any())
->method('setBar')
->with($this->identicalTo($bar));
$someMock->setBar($bar);
This is of course only an example. It should work, but it doesn't. To get some hints, i wrote some echo code in the PHPUnit_Framework_Constraint_IsIdentical method:
public function evaluate($other, $description = '', $returnResult = FALSE)
{
//...
echo "\n";
echo spl_object_hash($other) . "/". spl_object_hash($this->value). " - ". get_class($this->value) ."/". get_class($other);
echo " ->> ";
var_dump($other === $this->value);
}
The object hash of $other is not the same as $this->value (while the hash of $this->value is actually the correct one)
In the meantime, i found the reason for this error here. It's an issue of PHPUnit. Objects getting cloned by PHPUnit. I leave the question If someone knows a good workaround for this issue.
https://github.com/sebastianbergmann/phpunit-mock-objects/issues/41

Related

Testing a simple function in a model with Mockery

I'm totally new with Mockery which is embedded in Laravel. I have the pain to test a simple model function which increments a portion of a reference, whatever the value I'm passing to test the result is ok even when it should fail.
I think I made an error somewhere or I don't understand the documentation.
Thanks for your help.
Here is the simple function to test
public function incrementRefFormation(string $value):string
{
$split = str_split($value);
$group1 = '';
for ($i=0;$i<11;$i++) {
$group1 .= $split[$i];
}
$group2 = $split[11].$split[12];
$group2 = (int)$group2;
$group2++;
return $group1.$group2.$split[13];
}
Here is the test which should fail
public function testIncrementRefFormation()
{
//$testValue = '1 332 8100 20S';
$testValue = '123456';
$expectedValue = '1332810021S';
$mock = Mockery::mock('App\Models\Formation');
$mock->shouldReceive(['incrementRefFormation' => $expectedValue])
->once();
var_dump($mock->incrementRefFormation($testValue));
}
Many thanks!
Mockery is used for creating 'mocks', which are dumb objects doing only what you tell them to do (e.g. method x will return y if supplied with parameter z). Usually it's used for mocking dependencies of the class you want to test. In your case you probably won't need it.
So your test would probably look something like this
$formation = new App\Models\Formation();
$actualvalue = $formation->incrementRefFormation($testValue);
$this->assertEquals($expectedValue, $actualvalue);

Reference to PHP method without using a string

Suppose I have the following PHP code:
class Foo {
function getBar() {
return 1;
}
}
function check( Foo $foo ) {
if ( $foo->getBar() == 1 ) {
// here could be more code ...
return 'Oh no, there was an error in class' .
get_class( $foo ) . ', method ' .
'getBar';
}
}
The last string in check bothers me because if Foo::bar gets renamed by a refactoring tool, the error message will be wrong. Is there any way to get around this without using a string somewhere?
You can use __METHOD__ to get the name of the current method.
But to get reference to other method that would allow you some kind of automatic refactoring - no, it's not possible in php.
Can be done by using method_exists()
class Foo {
function getBar() {
return 1;
}
}
function check( Foo $foo , $method = 'getBar') {
if (!method_exists($foo, $method) ) {
// here could be more code ...
return 'Oh no, there was an error in class' .
get_class( $foo ) . ', method ' .
$method;
}
}
It is not possible in PHP per se, but you can implement such a feature. One possible implementation would work as follows: somewhere the file path, class name, method name and some kind of a description of where and what should match what. Your new feature whenever triggered would check the given files, check whether some values changed, fix whatever needs to be fixed and log a report about the task. It would not be simple to implement something like this, but, important to note is that there is a solution.

Does setting $this = &$otherObject change anything?

The comments indicate that I have to clarify my point: I'm a developer and consultant working on foreign code here. My current assignment is to help migrating an existing (very large) system from PHP4 to Java. While reviewing the code I stumbled across a piece of code that quite confuses me.
Some class (let's call it TestClass) in the system I'm working on does something like this:
function reload(){
$newobject = new TestClass();
$this = $newobject;
}
I gave it a quick try, expecting some inconsistent behavior between accessing members of the object directly or via some getter in case this somehow works. I thought that at worst the external pointers to the existing object and the internal one (aka $this) would target different portions of the heap.
Quick tests seem to indicate that simply nothing happens.
Any idea if this has any (side) effect whatsoever?
Here is my complete test:
<?php
ini_set('display_errors', true);
class TestClass{
var $var1;
function TestClass(){
$this->var1 = 0;
}
function getVar1(){
return $this->var1;
}
function setVar1($value){
$this->var1 = $value;
}
function reload(){
$newobject = new TestClass();
$this = &$newobject;
}
}
echo "<h3>Creating TestObject</h3>";
$testObject = new TestClass();
echo "Accessing member directly: " . $testObject->var1 . "<br>";
echo "Accessing member via get: " . $testObject->getVar1() . "<br>";
echo "Setting member directly to 1<br>";
$testObject->var1 = 1;
echo "Accessing member directly: " . $testObject->var1 . "<br>";
echo "Accessing member via get: " . $testObject->getVar1() . "<br>";
echo "<h3>Calling reload</h3>";
$testObject->reload();
echo "Accessing member directly: " . $testObject->var1 . "<br>";
echo "Accessing member via get: " . $testObject->getVar1() . "<br>";
?>
I expected to get 1 and 0 at last two calls if $this now pointed towards the new object while $testObject would still point to the original one, but 1 is returned in both cases. So I need to know whether the reassignment of $this is doing anything but being a bad idea. If not I can dump that part for good. So if anyone knows of any side effects please tell.
PS: I am fully aware that the above code is not using visibility and such. The original system is written in PHP4 so why bother :-|
Okay, with the hint of a colleague I got it. If the reload function is changed to
function reload(){
$newobject = new TestClass();
$this = $newobject;
}
the object is overwritten with the content of the newly created one. This is due to the copy on write mentioned by "i put on my robe an wizard hat" in the comment above. If used with the forced call-by-reference nothing happens which is good.

Do all variables in methods need to be object properties? (PHP)

I'm learning OO PHP and am trying to get some of the coding practices straight. Here is a pared down version of some code I'm using for error (and exception) handling:
final class MyErrorExceptionHandler {
private $level = array(); // error levels to be handled as standard errors
private $path = array(); // full path to file
private $path_short; // filename plus working dir
public function myErrorHandler($severity, $message, $file, $line) {
if (error_reporting() & $severity) { // error code is included in error_reporting
$this->level = array(E_WARNING => 'warning',
E_NOTICE => 'notice',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice');
if (array_key_exists($severity, $this->level)) { // handle as standard error
/*$this->severity = $severity;
$this->message = $message;
$this->file = $file;
$this->line = $line;*/
$this->printMessage($severity, $message, $file, $line);
} else { // fatal: E_USER_ERROR or E_RECOVERABLE_ERROR use php's ErrorException converter
throw new ErrorException($message, 0, $severity, $file, $line);
}
}
} // fn myErrorHandler
private function printMessage($severity, $message, $file, $line) {
echo ucfirst($this->level[$severity]) . ': ' . $message;
$this->shortenPath($file);
echo ' in ' . $this->path_short . ' on line ' . $line;
} // fn printMessage
private function shortenPath($file) {
$this->path_short = $file;
$this->path = explode(DIRECTORY_SEPARATOR, $file);
if (count($this->path) > 2) { // shorten path to one dir, if more than one dir
$this->path_short = array_pop($this->path); // filename
$this->path_short = end($this->path) . DIRECTORY_SEPARATOR . $this->path_short; // dir+file
}
} // fn shortenPath
} // cl MyErrorExceptionHandler
The title of this question is probably a little bit off because I'm not 100% on the terminology. Basically I'm trying to figure out a few things.
Is it right to explicitly declare $level and $path as arrays?
Should $level be declared as it is (and made $this->level)? If so, have I assigned its value (E_WARNING etc.) in a wise place? Would the constructor (not shown here) be a smarter choice?
Note the commented block in myErrorHandler(). Originally I had declared all of these properties at the top of the class, and then called $this->printMessage() without any parameters. Which is the more correct way? If I keep the code as is, would I want to then use $this->severity = $severity etc. inside printMessage()?
So, would it be better to:
replace
$this->shortenPath($file);
echo ' in ' . $this->path_short . ' on line ' . $line;
with
$path_short = $this->shortenPath($file);
echo ' in ' . $path_short . ' on line ' . $line;
ultimately, and give a return value in shortenPath()?
I realize this is a mishmash of several different questions, but what I'm trying to get at is a common inquiry about the proper style of declaring/using variables/properties, specifically when dealing with methods.
To summarize: When should I use $this->foo = $foo?
EDIT: sorry, I have assumed below that you would create a new instance of the 'object' with each error which obviously you are not doing. Just edited my answer to reflect this.
"When should I use $this->foo = $foo?"
There can be several cases in which you would do this, but it's usually if you create $foo within a method and wish to have that then accessed by the entire object.
For example, if you wanted to call on an object and use that within this particular object (if it doesn't make sense to extend). You would do something like:
$foo = new DataModel();
$this->foo = $foo;
OR
$this->foo = new DataModel();
That object may be a decorator or something else related to error handling and the above code would usually feature in your constructor. You could then access the methods of that object any time by using:
$this->foo->objectMethod();
..and to express something noted in the comments to this answer:
"would you assign $file to the object as that is used in several methods?"
I wouldn't assign $file to the object,
here's why. The semantics of the word
"property" means "belongs to". In your
case, your class is a error handler.
$file doesn't belong to the error
handler, it belongs to an error
instance. If your class was
MyErrorHandler_Error (created for each
instance of a triggered error), then
$file would be a property of that
class, along with $line and $level.
To answer what I can from your other questions:
It's neither. I would consider it preference.
Yes - any variables or values which should be available to your entire object and required for the object to run properly, should probably be set within your constructor, if not within your variable declarations (not sure of terminology there) at the top of the class.
read the comments below. Because this particular class deals with multiple instances of errors - assigning the properties of those errors to the object wouldn't be best practice as you will be overwriting them with each new error. However, it does make sense to store all of your errors and error properties within an array assigned to the object if you require to access historical data. For example, at the moment, if you create a new error - that is all you are doing. You have no way of accessing any old errors this object has created.
see above
You should also think about conflicts when assigning properties to objects. Are you likely to reassign, because if so, the old property will be gone. Fairly simple but still something you have to consider.

Weird problem with dynamic method invocation

this time, I'm facing a really weird problem. I've the following code:
$xml = simplexml_load_file($this->interception_file);
foreach($xml->children() as $class) {
$path = str_replace('__CLASS_DIR__',CLASS_DIR,$class['path']);
if(!is_file($path)) {
throw new Exception('Bad configuration: file '.$path.' not found');
}
$className = pathinfo($path,PATHINFO_FILENAME);
foreach($class as $method) {
$method_name = $method['name'];
$obj = new $className();
var_dump(in_array($method_name,get_class_methods($className)));exit;
echo $obj->$method_name();### not a method ???
}
}
As you can see, I get the class name and method name from an XML file.
I can create an instance of the class without any problem. The var_dump at the end returns true, that means $method_name (which has 2 optional parameters) is a method of $className.
BUT, and I am pretty sure the syntax is correct, when I try: $obj->$method_name() I get:
Fatal error: Method name must be a string
If you have any ideas, pleaaaaase tell me :)
Thanks in advance,
Rolf
The issue you are having is probably that $method_name is not a string, but it contains a method to convert it to a string (__toString()).
As in_array by default don't do strict type comparisons you will find that $method_name is probably coveted to a string and then compared with the method names, which would explain why the var_dump outputs true.
You should be able to confirm this by checking the type of $method_name
echo gettype($method_name);
If it isn't a string the solution is to case the variable to a string and then use that to call the function.
$obj->{(string)$method_name}();
It's better to use the call_user_func function instead of $obj->$method_name() to call the method.
echo call_user_func(array($className, $method_name));

Categories