cakephp-fixture-factories icon indicating copy to clipboard operation
cakephp-fixture-factories copied to clipboard

In string method notation

Open pabloelcolombiano opened this issue 3 years ago • 6 comments

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:

  • this makes it much easier to write

Cons:

  • we yes loose the autocompletion.
  • refactoring by the IDE is not possible

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 avatar Jan 07 '22 14:01 pabloelcolombiano

Please vote as follows:

Authors::active::verified 😄 

Authors#active#verified 👍 

Authors->active->verified ❤️ 

Authors@active@verified 👎 

pabloelcolombiano avatar Jan 11 '22 13:01 pabloelcolombiano

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

pakacuda avatar Jan 11 '22 15:01 pakacuda

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 😄

LordSimal avatar Jan 11 '22 15:01 LordSimal

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

pakacuda avatar Jan 11 '22 15:01 pakacuda

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 avatar Feb 01 '22 16:02 pabloelcolombiano

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?

pabloelcolombiano avatar Feb 01 '22 16:02 pabloelcolombiano