thephpleague / factory-muffin

Enables the rapid creation of objects for testing
https://factory-muffin.thephpleague.com/
MIT License
531 stars 72 forks source link

How do I assign a literal value? #424

Open ejunker opened 8 years ago

ejunker commented 8 years ago

I want to hard code a literal value. Here is a minimal example:

$fm->define('Resume')->setDefinitions([
    'salary' => 'd',
]);

The problem is I also have a global function d() and it calls the d() function instead of just using the literal value 'd'. Is there a way to assign literal string values?

GrahamCampbell commented 8 years ago

Hmm, that is a bit of a problem. The issue is the string d is callable on your machine.

GrahamCampbell commented 8 years ago

One way to force the string would be to do:

$fm->define('Resume')->setDefinitions([
    'salary' => function () { return 'd'; },
]);
ejunker commented 8 years ago

That would work but it is a bit verbose. As a user I would expect a literal string to assign a literal string. It would be a breaking change but maybe in the future it could be changed so that if you want to call a callable that you would have to do something like like this.

$fm->define('Foo')->setDefinitions([
    'bar' => $fm->call('myCallable'),
]);
GrahamCampbell commented 8 years ago

I wonder if I could refactor the generator stuff so that people could choose to disable callables. Would that work for you. In other words, only support closures rather than callables, as an option.

ejunker commented 8 years ago

That would work. If callables were disabled I still like the idea of having a helper method that you could pass a string and it would return a closure that calls the callable. Like the $fm->call() in my previous example.

GrahamCampbell commented 8 years ago

I'll have a think how I can make this work so it's really nice to use.