Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

PHPUnit testcase does not find data after "write"


Go to End


5 Posts   1310 Views

Avatar
fj

Community Member, 7 Posts

12 February 2011 at 12:04am

hi there,

i use PHPunit for testing my code that extends the SS core like.

i extend the core class

class TvShow extends DataObject

and I have a test case with code that writes the object to the DB and try to read it again,
but reading always fails.
The code in the uniot test case looks like this:

$show = new TvShow();
$show->ID = 12;
$show->ShowTitle = "a title";
$id = $show->write();

$this->assertEquals(12, $id); // test is OK

$newShow = DataObject::get_by_id("TvShow", 12);

$this->assertInstanceOf("TvShow", $newShow); // FAILS

why does the get_by_id-method not get the freshly written object.
how can i get it? does anyone have the same trouble?

btw: the $show->write(); stores the data in the tmpdb used for that
test case, so the data is there.

thanks a lot,
roman

Avatar
swaiba

Forum Moderator, 1899 Posts

12 February 2011 at 12:10am

what do you get if you Debug::show($newShow) ?

Avatar
fj

Community Member, 7 Posts

12 February 2011 at 12:42am

I get a (bool) false

Avatar
swaiba

Forum Moderator, 1899 Posts

12 February 2011 at 12:48am

that's not good no, as it happens I've got a replacement from my automated testing that doesn't use PHPUnit - oh the hassle of setting that up all the time... still use it for selenium but that is only on one machine... anyway...

only way to be sure what is going on is to debug into the dataobject::get_by_id and see what is going on... might be some caching issue... but I really don't know... everything looks ok...

maybe do a dataobject::get() and see what that produces before / after.

also why force the ID? surely just grabbing the id after the write is sufficient?

Avatar
fj

Community Member, 7 Posts

12 February 2011 at 1:38am

I forced the ID because I was importing data with int as primary key being already there.
now I changed that and gave SS control over the ID. I store my PK in a different field and now I
have 2 PK fields (damn). one for business logic and one for SS logic :-(

BUT surprisingly the get_by_id works within the unit tests :-)
and also my new getByIdShow($id) works in the same context.

--> seems that SS/PHPUnit does not work correctly when using custom IDs.