Skip to content

Wrong rgt-value when creating child node on PHP 7.2 #55

Open
@nivv

Description

@nivv

Wrong rgt-value when creating child node on PHP 7.2

I have this test:

  /** @test **/
    public function store_category()
    {
        // Arrange
        $root = factory(\App\Category::class)->create( ['name' => 'Root', 'depth' => 0]);
    
        // Act
        $response = $this->actingAsAdmin('api')
        ->json('POST', $this->endpoint, [
            'name' => 'New Category',
            'parent_id' => $root->id,
        ]);
    
        // Assert
        $response->assertStatus(200);
        $response->assertJson([
            'tree' => [
                'root' => true,
                'name' => 'Root',
                'children' => [
                    [
                    'name'  => 'New Category',
                    'lft'   => 2,
                    'rgt'   => 3
                    ]
                ]
            ]
        ]);

        $this->assertDatabaseHas('categories', [
            'name' =>  'New Category',
            'slug' =>  'new-category',
            'lft'  =>  2,
            'rgt'  =>  3
        ]);
        $this->assertDatabaseHas('categories', [
            'name' =>  'Root',
            'lft'  =>  1,
            'rgt'  =>  4
        ]);
    }

This works on PHP 7.0 and PHP 7.1 but fails with PHP 7.2

To get the test to pass on PHP 7.2 the last assertion (the one checking the root in the database) has to be changed to below

        $this->assertDatabaseHas('categories', [
            'name' =>  'Root',
            'lft'  =>  1,
            'rgt'  =>  3
        ]);

The actual store function in my app

    public function store(CreateCategoryRequest $request)
    {
        $parent = $request->input('parent_id');
        $category = new Category;
        $category->fill($request->all());
        $category->save();
        $category->makeChildOf($parent);

        return $this->categoryTree($request, $category);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions