PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Aug 20, 2018 6:30 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Dec 21, 2009 8:37 am 
Offline
Forum Newbie

Joined: Mon Dec 21, 2009 8:20 am
Posts: 2
Hi there,

We've started unit testing our application with PHPUnit 1 month ago (meaning we're doing "test after", unfortunately).
The last trouble I'm running into is that there doesn't seem to be a way to mock values returned via
parameters passed by-reference.

For example, I can't mock the $user returned by getUser(&$user, $userId), where the first parameter is used
to return the user (the function's return type being a status code).
You can't do this with returnCallback() since values assigned to the parameters passed by ref. are lost...

Has anybody ran into this trouble and already found a workaround ?
(for now mine is using hand crafted stubs, but this becomes cumbersome very quickly)

Thanks in advance!


Top
 Profile  
 
PostPosted: Mon Dec 21, 2009 10:09 am 
Offline
DevNet Master
User avatar

Joined: Mon Sep 19, 2005 6:24 am
Posts: 3587
Location: London
Can you post the test that is problematic? I'm not a user of phpunit but I reckon I can come up with a solution :)


Top
 Profile  
 
PostPosted: Mon Dec 21, 2009 10:28 am 
Offline
Forum Newbie

Joined: Mon Dec 21, 2009 8:20 am
Posts: 2
Hehe ok, I'll try to put up a simple example that will isolate the problem clearly... and post it here.


Top
 Profile  
 
PostPosted: Thu Dec 24, 2009 1:09 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
Does this isolate the problem?

Syntax: [ Download ] [ Hide ]
 
<?php
class myMock
{
    function returnReference( $a )
    {
        return $a;
    }
}
class TestPHPUnit extends PHPUnit_Framework_TestCase
{
    function testReal()
    {
        $a = new stdClass;
        $b = new stdClass;
        $this->assertFalse( $a === $b, 'we understand how references work' );
        $this->assertTrue( $a === $a, 'we understand how references work' );
       
        $mockObject = $this->getMock( 'myMock' );
        $mockObject->expects( $this->any() )->method( 'returnReference' )->will( $this->returnArgument(0) );
       
        $c = $mockObject->returnReference( $a );
        $this->assertTrue( $a == $c, 'equals should work' );
        $this->assertTrue( $a === $c, 'references should work' );
    }
   
    function testMock()
    {
        $a = new stdClass;
        $b = new stdClass;
        $this->assertFalse( $a === $b, 'we understand how references work' );
        $this->assertTrue( $a === $a, 'we understand how references work' );
       
        $mockObject = new myMock; // use a real object
        $c = $mockObject->returnReference( $a );
        $this->assertTrue( $a == $c, 'equals should work' );
        $this->assertTrue( $a === $c, 'references should work' );
    }
}


Both of these tests should pass. I am submitting a feature enhancement request. http://www.phpunit.de/ticket/976


Top
 Profile  
 
PostPosted: Thu Dec 24, 2009 8:49 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
Mine was a duplicate and someone already wrote this patch. http://www.phpunit.de/ticket/940


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group