Comments (10)
@dreamingmind which version of the Plugin are you using? Have you tried with the latest?
from cakephp-fixture-factories.
Ach! I'll get back to you on this. Probably my error.
from cakephp-fixture-factories.
O.K., I'll close it for the moment, feel free to re-open!
from cakephp-fixture-factories.
My issue appears to be with associated tables failing to run configured events.
I have a record Items
which hasMany('Skus')
. Skus
are configured to use events in this way:
protected function setDefaultTemplate(): void
{
$this->setDefaultData(function(Generator $faker) {
return [
// 'hash' => $faker->text(20), //this should be set by the event
'name' => $faker->text(15),
'code' => $faker->text(5),
];
})
->listeningToModelEvents(['Model.beforeSave']);
}
This code produces all the required records but does not run the events for Skus
:
//In a test class
public function testItem()
{
ItemFactory::make()->standAloneItem()->persist();
}
//in the ItemFactory
public function standAloneItem($parameter = null, int $n = 1): ItemFactory
{
return $this
->withPrimarySku()
->with('Tenant.Warehouse');
}
public function withPrimarySku($parameter = null): ItemFactory
{
return $this->with(
'PrimarySku',
\App\Test\Factory\SkuFactory::make(['base_sku' => 1])
);
}
If I call the SkuFactory
directly, the configured events will run.
This code for example will produce one sku
without the event running and a second with the event running.
public function testSku() {
$item = ItemFactory::make()->standAloneItem()->persist();
SkuFactory::make(['item_id' => $item->id, 'tenant_id' => $item->tenant->id])->persist();
}
I'm currently in Cake 4.1.6 and FixtureFactory 2.1.0
from cakephp-fixture-factories.
I don't know how to re-open this issue. Should I just make a new one?
from cakephp-fixture-factories.
@dreamingmind I see your issue. The event manager will apply events to the main factory, but not to its associations.
Therefore, in testSku()
, the event is not triggered when Skus are associated, but triggered when called directly.
I agree that this behavior of the package is wrong, since per default the SkuFactory listens to the beforeSave event. The event should be triggered.
I suggest as a turn around that you explicitly enable the event in your ItemFactory as follows
public function withPrimarySku($parameter = null): ItemFactory
{
return $this->with(
'PrimarySku',
\App\Test\Factory\SkuFactory::make(['base_sku' => 1])->listeningToModelEvents('Model.beforeSave')
);
}
Does this trigger the beforeSave event?
I have re-opened the issue and will work in the coming days on a fix.
from cakephp-fixture-factories.
@dreamingmind I have added a few tests to make sure that the model events were triggered in associations too. They are, including when the event is activated in the defautl template.
You can have a look here:
https://github.com/vierge-noire/cakephp-fixture-factories/blob/master/tests/TestCase/Factory/EventCollectorTest.php#L329
You can ignore my previous post. The error seems to be on your side. Which version of the plugin do you use? Do you use the latest v2.1.0 (v1.1.0 on Cake3)?
from cakephp-fixture-factories.
Ok. I'm digging deeper. I've pulled your recent code and see the passing test. I'll see if I can either figure out what's wrong or write a failing test.
from cakephp-fixture-factories.
@dreamingmind can I close this for now?
from cakephp-fixture-factories.
Yes. I got sidetracked on another time sensitive task and will open a new issue if my research turns something up.
The two specific details I will look into is the use of a static callable in the event handler and the triggering of an event that is more than 1 association away.
Thanks
from cakephp-fixture-factories.
Related Issues (20)
- What is the mergeAssociated(array $data) function for? HOT 1
- Testing simple method without persisting data HOT 1
- fakerphp 8.2 warnings HOT 1
- Creating multiple translations using shadowStrategy HOT 1
- Truncate Dirty Tables Slowness HOT 2
- Feature Request: Add back-reference property to sub-entities HOT 5
- Error: Column "id" is an identity column defined as GENERATED ALWAYS HOT 1
- Feature Request: Ability to set nested values using `make` method HOT 2
- Enhance array notation
- Calling `with()` inside `setDefaultTemplate()` occurs unexpected increase of the associated entities HOT 4
- phpdoc @param types of `FactoryAwareTrait::getFactory()` is too narrower than acceptable types HOT 1
- Factory::make ignores null values
- Factory::make ignores null values HOT 2
- Plans on doing a Cake5 release? HOT 2
- Factories use the wrong connection when called from a PHPUnit v10.x static data providers HOT 1
- Plugin class name HOT 1
- v3.1 get result set
- Association names plural vs singular HOT 4
- Some thoughts/improvements HOT 3
- PersistCommand and number argument: default value should be a string
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cakephp-fixture-factories.