CategoryRepositoryTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of ibrand/category.
  4. *
  5. * (c) GuoJiangClub <https://www.ibrand.cc>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace GuoJiangClub\Component\Category\Test;
  11. class CategoryRepositoryTest extends BaseTest
  12. {
  13. public function testGetSubIdsById()
  14. {
  15. //get Sub Ids By Id
  16. $this->assertSame(0, count($this->repository->getSubIdsById(10, true)));
  17. $this->repository->skipCache(true);
  18. $category_id_1 = $this->repository->getSubIdsById(1);
  19. $this->assertSame(8, count($category_id_1));
  20. $this->assertSame(2, $category_id_1[1]);
  21. $category_id_1 = $this->repository->getSubIdsById(1, true);
  22. $this->assertSame(7, count($category_id_1));
  23. $this->assertSame(3, $category_id_1[1]);
  24. }
  25. public function testGetCategories()
  26. {
  27. //get Categories tree
  28. $tree = $this->repository->getCategories();
  29. $this->assertSame(1, count($tree));
  30. $this->assertSame('女装', $tree->first()->name);
  31. $children = $tree->first()->children;
  32. $this->assertSame(3, $children->count());
  33. $this->assertSame('2017卫衣', $children->first()->children->first()->name);
  34. $this->repository->create(['name' => '2017短裤', 'sort' => 1], 5);
  35. $this->repository->create(['name' => '男装', 'sort' => 1]);
  36. $tree = $this->repository->getCategories();
  37. $this->assertSame(2, count($tree));
  38. $this->assertSame('男装', $tree->toArray()[1]['name']);
  39. $this->repository->skipCache(true);
  40. $tree = $this->repository->getCategories();
  41. $this->assertSame('2017短裤', last($tree->first()->children->toArray()[2]['children'])['name']);
  42. $tree = $this->repository->getCategories(1);
  43. $this->assertEquals(2,$tree->count());
  44. }
  45. public function testGetSubCategoriesByNameOrId()
  46. {
  47. //getSubCategoriesByNameOrId
  48. $this->repository->create(['name' => '男装', 'sort' => 1]);
  49. $all = $this->repository->getSubCategoriesByNameOrId(1, 1)->toArray();
  50. $this->repository->skipCache(true);
  51. $this->assertSame(3, count($all));
  52. $this->assertSame([], $all[0]['children']);
  53. $all = $this->repository->getSubCategoriesByNameOrId(2)->toArray();
  54. $this->assertSame(2, count($all));
  55. $all = $this->repository->getSubCategoriesByNameOrId(18)->toArray();
  56. $this->assertSame('男装', last($all)['name']);
  57. }
  58. }