lazychaser / laravel-nestedset

Effective tree structures in Laravel 4-8

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bypassing nestedset functionality for PHPUnit tests

jonnott opened this issue · comments

We have a large dataset based around an Eloquent model using the NodeTrait trait, with many thousands of records (both in production, and locally, for testing). Project current runs with Laravel 8.x and laravel-nestedset 6.x on PHP 7.4.

As part of our PHPUnit tests setUp process, an instance of this model is created and persisted to the database. Each test uses Laravel's DatabaseTransactions system, whereby the transaction is rolled back each time.

However, due to the way nestedset works, the entry created during setUp gets assigned a lft or rgt value which means another entry in the table (not actually related to the entry created for the test) gets deleted during the teardown process in which we delete the entry which has been created for the test. This is something we want to avoid happening.

The entry created for the tests is totally 'autonomous' for the purposes of the tests, and doesn't need to 'exist' within the tree structure at all.

So we're wondering - is there a way to completely 'bypass' the listeners that are set up in NodeTrait (in bootNodeTrait()), or some other way entirely, such that none of those processes are triggered when a particular entry is created or deleted - therefore the entry can temporarily exist in the table without any lft/rgt values set at all, and nothing effecting other entries happening when it's deleted. e.g. a completely 'stealth' entry.

Something similar to how Laravel's WithoutModelEvents for seeders works, maybe?

@lazychaser

Could these methods help? laravel/framework@e9cd94c