Error when using SimpleTest Mock::generate()

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
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Error when using SimpleTest Mock::generate()

Post by Maugrim_The_Reaper »

Wondering if anyone has any idea what the following error is in relation to...

Code: Select all

Fail: /opt/lampp/htdocs/quantumstar/trunk/Tests/Partholan/Test_DataObject.php -> Bad GroupTest [/opt/lampp/htdocs/quantumstar/trunk/Tests/Partholan/Test_DataObject.php] with error [Non-static method Mock::generate() should not be called statically, assuming $this from incompatible context]
Relevant parts (only started writing the test so not sure if that is a possible cause):

Code: Select all

<?php

// Simple Test files included earlier: unit_tester.php, mock_objects.php, reporter.php
require_once(APPROOT . 'Partholan/Abstracts/Parth_DataObject.php');
require_once(APPROOT . 'Partholan/Classes/Parth_DataAccess.php');
require_once(TESTROOT . 'Other/Item.php'); // simple Parth_DataObject child for testing

Mock::generate('Parth_DataAccess');

class Test_DataObject extends UnitTestCase {

	private $item1;
	private $item2;

	public function __construct() {
		$this->UnitTestCase('Test of DataObject');
		// return values for future mocked DataAccess classes
		$this->item1 = new Item(array('item_id'=>1,'item_name'=>'Test Item','item_text'=>'Test Item 001'), new MockParth_DataAccess());
		$this->item2 = new Item(array('item_id'=>2,'item_name'=>'Test Item','item_text'=>'Test Item 002'), new MockParth_DataAccess());
	}

	public function setUp() {
	}

	public function tearDown() {
	}

	public function testGetByPk() {
		$dao = new MockParth_DataAccess();
		$do = new Item(array(), $dao);
		$dao->expectOnce('getByPk', array($do, 1));
		$dao->setReturnValue('getByPk', $this->item1);
		$do->getByPk(1);
		// DAO overwrites $do reference
		$this->assertIsA($do, 'Parth_DataObject');
		$this->assertEqual($do->getItemId(), 1);
		$this->assertEqual($do->getItemName(), 'Test Item');
		$this->assertEqual($do->getItemText(), 'Test Item 001');
	}

}
This could be a PHP error (not caused by test) but I ran several other test suites using Mocks under the same environment (using PHP 5.1.4) and no similar error occured. This test, like the others which ran without error, had error_reporting() set to E_ALL.

Anyone have any ideas?
Last edited by Maugrim_The_Reaper on Thu Jun 01, 2006 10:26 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you may try to search the SimpleTest sources for the line where the error is thrown...
this could give you ideas on what made the Mock::generate method think it shouldn't be called statically...
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Was trying that.

I altered the function declaration to "static function generate...". That got rid of the error, but the Mocked object does not appear to have been generated:

Code: Select all

Fatal error: Class 'MockParth_DataAccess' not found in /opt/lampp/htdocs/quantumstar/trunk/Tests/Partholan/Test_DataObject.php on line 17
Get the same error when I try to impose a Mock classname manually.

I must be missing something - it seems to work for other tests (completely different app) for the same SimpleTest version (copied it across as a test).
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

That seems like it might be related to the elevated E_STRICT notices with the recent PHP releases (starting 5.1.3 I think)

Can you ensure the E_STRICT error level is not set when running your test?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

error_reporting is set to E_ALL. I'll see if disabling it completely works...
Gaspard
Forum Newbie
Posts: 4
Joined: Sat Jul 01, 2006 10:27 pm

Mock Objects in Simple Test.

Post by Gaspard »

I've had the same problem with Mock Objects in SimpleTest. Has anyone found a solution to this? Turning the errors downs down a notch just doesn't seem like the best solution. Especially since the group I'm working in is all fairly new to PHP.

Also, where is SimpleTest 1.0. At SourceForge there is a SimpleTest Eclipse Plug-in in beta, and SimpleTest 1.bar-alpha. Currently we are using the SimpleTest Eclispse Plug-in to run tests inside web pages, but as you can imagine this doens't seem like the best setup.

Gaspard Out.
lastcraft
Forum Commoner
Posts: 80
Joined: Sat Jul 12, 2003 10:31 pm
Location: London

Post by lastcraft »

Hi...

Could you try an experiment for me? Could you move the mock instantiation out of the constructor and into the setUp() method to see if that makes any difference. This is just a hunch.

Also, is there any executing code in the data object file? That is, code that is not inside a function or class? I am just wondering if either of these could cause problems.

Regarding E_STRICT, I cannot maintain PHP4 compatibility and at the same time make the code E_STRICT clean. It's just not possible. After SimpleTest 1.0.1, I'll be switching to PHP5.1+ only. I don't have the resources to maintain two versions, so I want to get the PHP4 version bug free before I abandon it.

yours, Marcus
Gaspard
Forum Newbie
Posts: 4
Joined: Sat Jul 01, 2006 10:27 pm

Nice to see someone from lastcraft surfing the forums

Post by Gaspard »

Hey Marcus,

I'll try that out, and yes I was trying to instantiate the mock_object outside of setup.

If we gave you some examples of simpletests that you could just copy and paste, or download to get running, rather than having a an imaginary log.php file, would you be willing to post it on your site.

Gaspard Out.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

At the moment the static keyword suppresses the error (of course this is under PHP5). I'll take a look later on if I have time and try what you suggested. The problem with Mock objects not being created has "gone away" on my setup. I was playing around with adding the first Mock static call to the inside of an if(true) statement but I don't see what difference that would possibly make. Since it does work with another set of tests I just copied what was done there, and it magically worked.

I left it alone at that point :).

I'll grab my May 31 revision from subversion and give it another look.
lastcraft
Forum Commoner
Posts: 80
Joined: Sat Jul 12, 2003 10:31 pm
Location: London

Re: Nice to see someone from lastcraft surfing the forums

Post by lastcraft »

Hi...
Gaspard wrote:If we gave you some examples of simpletests that you could just copy and paste, or download to get running, rather than having a an imaginary log.php file, would you be willing to post it on your site.
Of course. The only problem is that I am backed up on site updates and was planning a complete overhaul of the tutorials anyway. SimpleTest has had to make way for paid work and pregnancy, but I'm back on the case now. I'll check back in when I have made some progress.

yours, Marcus
Post Reply