InteractsWithModelFactory.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Testing\Concerns;
  12. use Faker\Factory as FakerFactory;
  13. use Hyperf\Database\Model\Factory;
  14. use Hyperf\Testing\ModelFactory;
  15. trait InteractsWithModelFactory
  16. {
  17. protected ?ModelFactory $modelFactory = null;
  18. /**
  19. * @var string|string[]
  20. */
  21. protected $factoryPath = BASE_PATH . '/database/factories';
  22. protected function setUpInteractsWithModelFactory()
  23. {
  24. if (! class_exists(Factory::class) || ! class_exists(FakerFactory::class)) {
  25. return;
  26. }
  27. $this->modelFactory = ModelFactory::create(
  28. FakerFactory::create('en_US')
  29. );
  30. foreach ((array) $this->factoryPath as $path) {
  31. if (is_dir($path)) {
  32. $this->modelFactory->load($path);
  33. }
  34. }
  35. }
  36. protected function tearDownInteractsWithModelFactory()
  37. {
  38. $this->modelFactory = null;
  39. }
  40. }