/** * https://github.com/studio107/Mindy_Orm/issues/50 */ public function testFixIsLeaf() { // Create root list($root1, $created) = NestedModel::objects()->getOrCreate(['name' => 'root1']); $this->assertTrue(NestedModel::objects()->filter(['name' => 'root1'])->get()->getIsLeaf()); // Create root2 list($root2, $created) = NestedModel::objects()->getOrCreate(['name' => 'root2']); $this->assertTrue(NestedModel::objects()->filter(['name' => 'root2'])->get()->getIsLeaf()); // Create nested record for root2 list($nested, $created) = NestedModel::objects()->getOrCreate(['name' => 'nested', 'parent' => $root2]); // root1 is leaf = true $this->assertTrue(NestedModel::objects()->filter(['name' => 'root1'])->get()->getIsLeaf()); // root2 is leaf = false (nested) $this->assertFalse(NestedModel::objects()->filter(['name' => 'root2'])->get()->getIsLeaf()); // nested is leaf $this->assertTrue(NestedModel::objects()->filter(['name' => 'nested'])->get()->getIsLeaf()); // Remove nested from root2 NestedModel::tree()->filter(['name' => 'nested'])->delete(); // Root1 is leaf - true $this->assertTrue(NestedModel::objects()->filter(['name' => 'root1'])->get()->getIsLeaf()); // Root2 is leaf - true $this->assertTrue(NestedModel::objects()->filter(['name' => 'root2'])->get()->getIsLeaf()); /** * again */ NestedModel::objects()->truncate(); // create root1 list($root1, $created) = NestedModel::objects()->getOrCreate(['name' => 'root1']); // root1 is leaf $this->assertTrue(NestedModel::objects()->filter(['name' => 'root1'])->get()->getIsLeaf()); // create root2 list($root2, $created) = NestedModel::objects()->getOrCreate(['name' => 'root2']); // root2 is leaf $this->assertTrue(NestedModel::objects()->filter(['name' => 'root2'])->get()->getIsLeaf()); // create nested in root2 list($nested, $created) = NestedModel::objects()->getOrCreate(['name' => 'nested', 'parent' => $root2]); // create nested1 in nested list($nested1, $created) = NestedModel::objects()->getOrCreate(['name' => 'nested1', 'parent' => $nested]); // root1 is leaf (empty) $this->assertTrue(NestedModel::objects()->filter(['name' => 'root1'])->get()->getIsLeaf()); // root2 is not leaf (nested, nested1) $this->assertFalse(NestedModel::objects()->filter(['name' => 'root2'])->get()->getIsLeaf()); // nested is not leaf (nested1) $this->assertFalse(NestedModel::objects()->filter(['name' => 'nested'])->get()->getIsLeaf()); // remove nested1 from nested NestedModel::tree()->filter(['name' => 'nested1'])->delete(); $this->assertTrue(NestedModel::objects()->filter(['name' => 'root1'])->get()->getIsLeaf()); // Root2 is not leaf (nested) $this->assertFalse(NestedModel::objects()->filter(['name' => 'root2'])->get()->getIsLeaf()); // Nested is not leaf (nested1) $this->assertTrue(NestedModel::objects()->filter(['name' => 'nested'])->get()->getIsLeaf()); // Root1 is leaf - true $this->assertTrue(NestedModel::objects()->filter(['name' => 'root1'])->get()->getIsLeaf()); // Root2 is not leaf (nested) $this->assertFalse(NestedModel::objects()->filter(['name' => 'root2'])->get()->getIsLeaf()); }
public function testReplace() { $this->testInit(); $this->assertEquals(5, NestedModel::objects()->count()); $model = NestedModel::objects()->filter(['name' => 'test1'])->get(); $model->slug = 'qwe'; $this->assertEquals('test1', $model->getOldAttribute('slug')); $this->assertEquals('qwe', $model->getAttribute('slug')); $this->assertEquals('qwe', $model->slug); $test2 = NestedModel::objects()->filter(['name' => 'test2'])->get(); $this->assertEquals('test1/test2', $test2->slug); $model->save(); $this->assertEquals('qwe', $model->getOldAttribute('slug')); $this->assertEquals('qwe', $model->getAttribute('slug')); $this->assertEquals('qwe', $model->slug); $test2 = NestedModel::objects()->filter(['name' => 'test2'])->get(); $this->assertEquals('qwe/test2', $test2->slug); $test3 = NestedModel::objects()->filter(['name' => 'test3'])->get(); $this->assertEquals('qwe/test3', $test3->slug); $test4 = NestedModel::objects()->filter(['name' => 'test4'])->get(); $this->assertEquals('qwe/test3/test4', $test4->slug); $test3->slug = 'www'; $test3->save(['slug']); $test4 = NestedModel::objects()->filter(['name' => 'test4'])->get(); $this->assertEquals('qwe/www/test4', $test4->slug); }