Giter VIP home page Giter VIP logo

Comments (10)

pabloelcolombiano avatar pabloelcolombiano commented on May 14, 2024

@dreamingmind which version of the Plugin are you using? Have you tried with the latest?

from cakephp-fixture-factories.

dreamingmind avatar dreamingmind commented on May 14, 2024

Ach! I'll get back to you on this. Probably my error.

from cakephp-fixture-factories.

pabloelcolombiano avatar pabloelcolombiano commented on May 14, 2024

O.K., I'll close it for the moment, feel free to re-open!

from cakephp-fixture-factories.

dreamingmind avatar dreamingmind commented on May 14, 2024

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.

dreamingmind avatar dreamingmind commented on May 14, 2024

I don't know how to re-open this issue. Should I just make a new one?

from cakephp-fixture-factories.

pabloelcolombiano avatar pabloelcolombiano commented on May 14, 2024

@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.

pabloelcolombiano avatar pabloelcolombiano commented on May 14, 2024

@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.

dreamingmind avatar dreamingmind commented on May 14, 2024

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.

pabloelcolombiano avatar pabloelcolombiano commented on May 14, 2024

@dreamingmind can I close this for now?

from cakephp-fixture-factories.

dreamingmind avatar dreamingmind commented on May 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.