Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing a baum model and boot function #25

Closed
oliverkaiser opened this issue Nov 9, 2013 · 4 comments
Closed

Testing a baum model and boot function #25

oliverkaiser opened this issue Nov 9, 2013 · 4 comments

Comments

@oliverkaiser
Copy link

Hey,

was just fighting with my very simple test function for my CategoryRepository (which extends my category node model) for two hours.

What I was trying to do:
Setup a simple category structure for testing within the setUp() test function. This works on the first test but if it comes to my second test the initialization of my test data it is not!

I found out it belongs to the way of binding the creating event within the boot function of Node.php .

Actually I have no idea if there is a better way for baum. I got it working for me by adding
CategoryRepository::boot();
within the setUp() of my CategoryRepositoryTest ...

This also belongs to this topic: laravel/framework#1181

<?php
use Zizaco\FactoryMuff\Facade\FactoryMuff;

class CategoryRepositoryTest extends TestCase
{
    public function setUp() 
    {
        parent::setUp();

        $this->Repository = new CategoryRepository;

        // This isfixing this problem https://github.com/laravel/framework/issues/1181
        CategoryRepository::boot();

        // Generate a category tree database
        $this->root1 = FactoryMuff::create('CategoryRepository');
        $this->root2 = FactoryMuff::create('CategoryRepository');
        $this->cat1 = FactoryMuff::create('CategoryRepository');
        $this->cat2 = FactoryMuff::create('CategoryRepository');
        $this->cat3 = FactoryMuff::create('CategoryRepository');
        // Build levels
        $this->cat1->makeChildOf($this->root1);
        $this->cat2->makeChildOf($this->root2);
        $this->cat3->makeChildOf($this->cat1);

    }

    public function testSetup() {
        $this->assertTrue( $this->root1->isRoot() ,'root1 is not a root cat');
        $this->assertTrue( $this->root2->isRoot() ,'root2 is not a root cat');

        $this->assertFalse( $this->cat1->isRoot() ,'cat1 is a root cat');
        $this->assertFalse( $this->cat3->isRoot() ,'cat2 is a root cat');

        $this->assertEquals( $this->cat1->parent_id, $this->cat1->parent()->first()->id ,'cat1 is not a child of root1');
        $this->assertEquals( $this->cat3->parent_id, $this->cat1->id ,'cat3 is not a child of cat1');
    }

    public function testGetTree() {

    }
    public function testGetTree2() {

    }
}
?>
@etrepat
Copy link
Owner

etrepat commented Jan 15, 2014

I understand that the EventDispatcher is not firing the events and thus Baum does not work properly because it depends on these events to perform lft, rgt index updates etc.

I'm not sure if I should go down this path but a possible solution could be to override the Model methods for save, update, ... instead of relying on the events to get fired.

Is this still an ongoing problem?

Does it fail outside a testing environment?

@etrepat
Copy link
Owner

etrepat commented Jan 15, 2014

I see from the thread you linked (laravel/framework#1181) that the proposed solution is something similar to what you do but instead of calling the static boot method, just make a public static registerEvents method and call Model::flushEventListeners before...

@etrepat
Copy link
Owner

etrepat commented Mar 11, 2014

I'm closing this issue right now as there are some workarounds to this (as suggested on thread you point out) and I'm quite unsure on how I should be fixing this from our end.

Feel free to reopen if something relating this pops out on your radar or if you think there's something I should be adding to the library regarding this problem.

Thanks.

@etrepat etrepat closed this as completed Mar 11, 2014
@bmitch
Copy link
Contributor

bmitch commented Dec 10, 2014

I ran into the same issue while trying to run tests. Thankfully found this thread. I used the same workaround by running Category::boot() in the setUp() method for any tests having to do with the Category class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants