public function testShouldReturnLazyLoadingCursor() { $parentCategory = new Category(); $parentCategory->setName('Parent category'); $parentCategory->setDesc('Parent category'); $parentCategory->save(); for ($i = 0; $i < 10; $i++) { $category = new Category(); $category->setName('Category ' . $i); $category->setDesc('Category ' . $i . ' desc'); $category->setCategory($parentCategory); $category->save(); } Category::enableLazyLoading(); $categories = Category::find([['category' => ['$ne' => null]]]); $this->assertInstanceOf('\\Vegas\\Odm\\Collection\\LazyLoadingCursor', $categories); foreach ($categories as $category) { $this->assertInstanceOf('\\Fixtures\\Collection\\Category', $category); $this->assertInstanceOf('\\Fixtures\\Collection\\Category', $category->getCategory()); } $categories = Category::find([['category' => ['$ne' => null]]]); $this->assertInstanceOf('\\Vegas\\Odm\\Collection\\LazyLoadingCursor', $categories); foreach ($categories as $category) { $this->assertInstanceOf('\\Fixtures\\Collection\\Category', $category); $reflectionClass = new \ReflectionClass(get_class($category)); $categoryProperty = $reflectionClass->getProperty('category'); $categoryProperty->setAccessible(true); $this->assertTrue(\MongoDBRef::isRef($categoryProperty->getValue($category))); } }
public function testShouldSaveNotLoadedReference() { $category = new Category(); $category->setName('Category 1'); $category->setDesc('Category 1 desc'); $category->save(); $product = new Product(); $product->setCategory($category); $product->setName('Product 1'); $product->setPrice(100); $product->setIsActive(true); $product->setCreatedAt(time()); $product->save(); $product = Product::findById($product->getId()); $product->save(); $product = Product::findById($product->getId()); $category1 = $product->getCategory(); $category2 = $product->getCategory(); $this->assertInstanceOf('Fixtures\\Collection\\Category', $category1); $this->assertInstanceOf('Fixtures\\Collection\\Category', $category2); $this->assertSame($category2->getName(), $category1->getName()); }
private function setupDummyData($mainCategoryNum = 10, $subCategoryNum = 100, $productNum = 40) { for ($i = 0; $i <= $mainCategoryNum; $i++) { /** @var Category $parent */ $parent = new Category(); $parent->setCategory(null); $parent->setName('Category ' . $i); $parent->setDesc('Lorem ipsum dolor ' . $i . 'sit amet'); $parent->save(); for ($k = 0; $k <= $subCategoryNum; $k++) { /** @var Category $category */ $category = new Category(); $category->setCategory($parent); $category->setName('Subcategory' . $k . '-' . $i); $category->setDesc('test ' . uniqid('', true)); $category->save(); for ($j = 0; $j <= $productNum; $j++) { /** @var Product $parent */ $parent = new Product(); $parent->setCategory($category); $parent->setName('Product ' . $j); $parent->setPrice(rand(89, 8899)); $parent->setIsActive(true); $parent->save(); } } } }