-
Notifications
You must be signed in to change notification settings - Fork 27
[WIP] Static construction of objects #95
base: develop
Are you sure you want to change the base?
[WIP] Static construction of objects #95
Conversation
👍 |
Perhaps
|
No need to. |
Could you expand on that? How would I use $this->staticCall to spec an object with a private constructor? |
@everzet I disagree, you want to tell phpspec in the |
Ok. Wait. Lets make a couple steps back and you'll try to explain me why do you use this static factory method instead of constructor... What's the point? |
See https://github.com/phpspec/phpspec2/issues/79#issuecomment-11992498 My example is very silly of course, let me try to explain it with a real life example. Imagine an ORM. In DDD, a Repository "mediates between the domain and data mapping layers, acting like an in-memory domain object collection." (http://martinfowler.com/eaaCatalog/repository.html) In any case, I don't think phpspec should force me to only have public constructors. It might be a legacy thing or some other reason. It's up to the user, so phpspec should support it. |
Ok, makes sense now, interesting pattern. Not sure I fully support it though, but it totally have right to exist and i see nothing specifically wrong in it. I would suggest this syntax then: $this->beConstructedThrough('createInstance');
$this->beConstructedWith($arguments); This way you'll still have ability to redefine construction arguments without knowing anything about how object is actually constructed in the examples. |
Keep in mind that default behavior should still be the same - |
Looks like Travis breaks because of a github issue.
|
As discussed in #79.
The code is just a POC and there are no specs yet. I figured I'd first try to get it working before diving in too deep.
This fixes my use case where I want to instantiate an object without using it's constructor directly.
Simplified example:
In production code, you'd instantiate the Payment like this:
In phpspec: