What's wrong here...

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

What's wrong here...

Post by nielsene »

This test passes:

Code: Select all

function TestRegistrySingeleton() {
    $this->assertReference(Registry::instance(),
			   Registry::instance());
  }
this one fails

Code: Select all

function setUp() {
    $this->testReg = Registry::instance();
  }

  function TestRegistrySingeleton() {
    $this->assertReference($this->testReg,
			   Registry::instance());
  }
with

Code: Select all

Fail: /usr/local/CIB/main-dev/compinabox/tests/unit-tests/classes/All.php -> 
  All Class Tests -> /usr/local/CIB/main-dev/compinabox/tests/unit-tests/classes/Registry/All.php -> 
    All Registry Tests -> /usr/local/CIB/main-dev/compinabox/tests/unit-tests/classes/Registry/BasicRegistryTests.php -> 
        Registry -- Interface -> 
            testregistrysingeleton -> 
                       їObject: of registry] and 
                       їObject: of registry] should reference the same object at line ї33]

The relevant bit of production code

Code: Select all

function &instance() {
    static $theInstance=false;
    if (!$theInstance) $theInstance = new Registry();
    return $theInstance;
  }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm going to guess you are running php4, in which case the assignment coming out of Registry::Instance() is turned into the value and not a reference.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

[Solved]

Post by nielsene »

Yup, I had thought tought that ampersand in the function prototype would have taken care of that. Adding the ampersand on the receiving end fixed it.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

There's a problem with storing references in static variables - you'll need to use an array http://www.sitepoint.com/forums/showthread.php?t=265748
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

That's weird. I got it to work without using the array, and I think I ran the same test cases as Jason shows that should cause it to fail, but it didn't. But I'll switch it over anyways, just in case.
Post Reply