Is there any way to override which subject class is used for a spec?

120 views
Skip to first unread message

Andrew Coulton

unread,
Aug 22, 2013, 5:18:03 PM8/22/13
to phpsp...@googlegroups.com

In phpspec2 is there any way in a spec class to change what subject class is created as the target? I'm using kohana framework, where convention is to use underscored class names rather than namespaces to map class files down a directory tree. That's valid under psr-0 but not supported by the way phpspec maps filenames back to class names when loading specs.

If I want to specify Model_User then phpspec will expect the spec file to be in spec/Model_UserSpec.php - so all my spec files would have to be in the top-level spec directory.

If I rename my class Model\User then I can have a spec\Model\User and use nested directories in both. But then other factory classes in modules and core that map strings or filenames to classes will fail to load Model\User as they'll still look for Model_User.

In other words the issue is not that the autoloader can't find the files but that it's being asked for the wrong class names because of the ambiguity in the psr0 spec.

So it should be easiest (though not really neatest) to adopt different conventions for the spec and the class to suit the different class mapping conventions. But I can't see how to indicate that spec\Model\UserSpec.php should wrap \Model_User instead. As far as I can see the spec name(and namespace) is completely tied to the subject name.

In rspec I could just implement the subject method to override the defaults - is there something similar I've missed? Otherwise it looks like I might have to implement an extension with a custom loader to handle this - but it would mean copying a lot of the PSR-0 loader and resource as the couple of bits I'd need to change are buried in private methods or protected methods that use private instance variables.

Any ideas on how I could tackle this would be very welcome, particularly if I've missed something obvious.

Thanks,

Andrew

Jakub Zalas

unread,
Sep 5, 2013, 9:35:26 AM9/5/13
to phpsp...@googlegroups.com
Firstly, I think that's a bug in PhpSpec (it doesn't fully support PSR-0).

Secondly, you can change the class which would be instantiated with:

$this->beAnInstanceOf('Model_User');

That's gonna be a lot of manual work, so you might think of creating a PhpSpec extension for Kohana.

Andrew Coulton

unread,
Sep 9, 2013, 11:56:20 AM9/9/13
to phpsp...@googlegroups.com
Thanks for replying Jakub,

The challenge is there's so many ways to map a file name back to a classname, especially (as with PHPSpec) you allow class names that don't exist yet. So I guess it's more one of the flaws in PSR-0 - so it's basically impossible for something like PHPSpec to support it fully.

But that's really helpful to know how I'd change the class, and I'm thinking about building a Kohana extension when I have a chance.

Cheers,

Andrew


--
You received this message because you are subscribed to a topic in the Google Groups "PHPSpec - Behavior Driven Design (BDD) for PHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/phpspec-dev/KMccpQcC5yE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to phpspec-dev...@googlegroups.com.
To post to this group, send email to phpsp...@googlegroups.com.
Visit this group at http://groups.google.com/group/phpspec-dev.
For more options, visit https://groups.google.com/groups/opt_out.



--
Andrew Coulton
Founder
inGenerator Ltd

Follow us on Twitter @inGenerator
Phone us on 0131 510 0271

inGenerator Ltd is a company registered in Scotland (no SC435161) with its registered office at 8 Craigleith Hill Row, Edinburgh EH4 2JX.
Reply all
Reply to author
Forward
0 new messages