vierge-noire / cakephp-fixture-factories

CakePHP Fixture Factories
https://vierge-noire.github.io/
MIT License
85 stars 20 forks source link

In string method notation #154

Open pabloelcolombiano opened 2 years ago

pabloelcolombiano commented 2 years ago

Often little setters are added to a given factory. E.g.:

ArticleFactory::make()
   ->with('Authors',
      AuthorFactory()->make(10)->published()->active()->with('Address',
         AddressFactory::make()->verified()
      )
   )
   ->getEntity()

These setters could be specified in the in string notation of the with method:

ArticleFactory::make()->with('Authors::published::active[10].Address::verified')->getEntity();

Pros:

Cons:

If the method is not found, possibly due to some refactoring, an exception will be raised specifiying that the provided method does not exist.

Question 1: Do you agree on the utility of the feature?

Question 2: If yes, what is your prefered notation? # or : or :: or -> or something else?

pabloelcolombiano commented 2 years ago

Please vote as follows:


Authors::active::verified 😄 

Authors#active#verified 👍 

Authors->active->verified ❤️ 

Authors@active@verified 👎 
pakacuda commented 2 years ago

If what you propose is feasible (mapping Authors:: and Address:: to the correct factory), wouldn't the following have the best of both worlds. To get Full autocompletion, we would have to set the return type for Authors() and Address(), but maybe worth the additional effort

ArticleFactory::make()->Authors(10)->published()->active()->Address()->verified()->getEntity()

The ucfirst letter would be the convention for a factory method in the chain. There would be a magic getter only on ucfirst method calls. Otherwise, I would vote for 👍 just because two colon in a row might be error prone

LordSimal commented 2 years ago

I personally was never really a fan of "compressing" chained function calls into a string separated by <something>. Therefore this is my vote for "I don't mind/care" because it wouldn't be usefull to me 😄

pakacuda commented 2 years ago

I don't think I would use it either as a string BTW

pabloelcolombiano commented 2 years ago

ArticleFactory::make()->Authors(10)->published()->active()->Address()->verified()->getEntity()

This is a great idea @pakacuda. We ideally want a way to do something like this too:

ArticleFactory::make()
   ->with('Authors#published#active[10].Address#verified')
   ->with('Authors#published#inactive[10].Address#notVerified')
   ->getEntity();

Need to think about how to with the magic getter solution, but I agree that this beats the not-handy string notation solution.

pabloelcolombiano commented 2 years ago
ArticleFactory::make()
   ->Authors(10)->published()->active()->Address()->verified()
   ->andAuthors(10)->published()->inactive()->Address()->notVerified()
   ->getEntity()

would actually work if we introduce a root factory property. The magic getter will know that the prefix and refers to an association of the root factory property.

What do you all think of this syntax?