thephpleague / factory-muffin

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

Allow Closure generators to access generated attributes #311

Closed pmccarren closed 10 years ago

GrahamCampbell commented 10 years ago

One failing test here:

1) SaveAndDeleteTest::testShouldThrowMultipleDeletionExceptions

Failed asserting that two strings are equal.

--- Expected

+++ Actual

@@ @@

-'OH NOES!'

+'The delete method 'delete' was not found on the model of type: 'ModelWithNoDeleteMethodStub'.'

That confirms what I said inline. This will have to go into 2.2 because of the change in behaviour.

GrahamCampbell commented 10 years ago

@pmccarren Can you send only your "Set the att value on the model once generated." change to the "experimental" (3.0) branch please.

pmccarren commented 10 years ago

Sure, absolutely.

GrahamCampbell commented 10 years ago

Great. :)

pmccarren commented 10 years ago

Good to go! :)

pmccarren commented 10 years ago

@GrahamCampbell One possible way to remove objects in reverse order is once we create an object, we can append its objectHash (what spl_object_hash generates) to a reference array, and then when needed, simply iterate over said array in reverse order. The one check you'd have to have is to ensure that the object still exists before you attempt to delete it. Thoughts? I'm happy to prototype this too

GrahamCampbell commented 10 years ago

Yeh, would be nice, but would lead to poor performance of the isSaved function.

I assume you mean something like this under the hood:

$saved = array(
    0 => array('our hash', object),
    1 => array('our hash', object),
    2 => array('our hash', object),
);
GrahamCampbell commented 10 years ago

Hmmm. What if we were to simply have 2 completely separate arrays. That could be the answer!

GrahamCampbell commented 10 years ago
$saved = array(
    'hash a' => object3,
    'hash b' => object1,
    'hash c' => object2,
);

$map = array(
    0 => 'hash b',
    1 => 'hash c',
    2 => 'hash a',
);
GrahamCampbell commented 10 years ago

I'm going to get something to eat. I'll be back in 30 mins or so, and I'll take a look at this. Feel free to tackle this in a pull to 2.2 @pmccarren.

pmccarren commented 10 years ago

@GrahamCampbell yeah exactly! sorry I wasn't clear there. I believe two separate arrays is the key

pmccarren commented 10 years ago

Prototyped it over here: #316

GrahamCampbell commented 10 years ago

Hmmm. I did want it in 2.2, rather than 3.0, but leave the pull open anyway so we can discuss it.

pmccarren commented 10 years ago

Would you like me to create a PR with this change into 2.2/master?

GrahamCampbell commented 10 years ago

No, don't bother. When I've got time, I'll review your change against 3.0. Thanks for taking the time to contribute - it's much appreciated. :)

pmccarren commented 10 years ago

No problem! Glad to help out. using FactoryMuffin has made my testing experience so much better