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.

Data Model Questions /

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

Unit Test for DataObjectDecorator


Go to End


4 Posts   1310 Views

Avatar
juergr

Community Member, 17 Posts

8 February 2012 at 9:04am

How do I write Unit Tests for DataObjectDecorators? I have a MemberDecorator which adds a lot of functionality and information to the member object and now i want to test these functions.

But when i try to call a function of the MemberDecorator i get a error "undefined method" on member.

Is there a way to tell the Saphire test that the Member Object is Decorated with another Object?

Avatar
Willr

Forum Moderator, 5523 Posts

8 February 2012 at 9:12pm

Is there a way to tell the Saphire test that the Member Object is Decorated with another Object?

Decorators are applied transparently so you have to test again member directly (ensuring of course your decorator is added). You can see what extensions are applied to what object using the methods on object - http://api.silverstripe.org/2.4/sapphire/core/Object.html.

Avatar
juergr

Community Member, 17 Posts

9 February 2012 at 8:44am

But how can I call a Method on the Decorator?

If i have a MemberDecorator->doSomethingImportant() function, how do i call this function thru the Member class as i would during normal developement?

Can you make a little example, because i realy don't get it. (Sometimes i'm a little dumb...)

Avatar
Willr

Forum Moderator, 5523 Posts

9 February 2012 at 11:09pm

As I said, they're applied transparently so if you create a function Foo() calling Foo() on a member class would work.

//
class MemberDecorator extends DataObjectDecorator {
function Foo() { return "hi"; }
}
//
DataObject::add_extension('Member', 'MemberDecorator');
//

$member = Member::currentUser();
echo $member->Foo(); // "hi";