Jump to:

23454 Posts in 18904 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » PHPUnit testcase does not find data after "write"

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

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

Page: 1
Go to End
Author Topic: 711 Views
  • fj
    Avatar
    Community Member
    7 Posts

    PHPUnit testcase does not find data after "write" Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: PHPUnit testcase does not find data after "write" Link to this post

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

  • fj
    Avatar
    Community Member
    7 Posts

    Re: PHPUnit testcase does not find data after "write" Link to this post

    I get a (bool) false

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: PHPUnit testcase does not find data after "write" Link to this post

    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?

  • fj
    Avatar
    Community Member
    7 Posts

    Re: PHPUnit testcase does not find data after "write" Link to this post

    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.

    711 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.