getSelectedModels(); // if (!is_dir('tests/Feature/' . ucfirst($model))) { // mkdir('tests/Feature/' . ucfirst($model), 0777, true); // } else { // $confirm = $this->confirm("$model tests before created, are you sure delete them and regenerate test?", true); // if ($confirm) { // system("rm -rf " . escapeshellarg('tests/Feature/' . ucfirst($model))); // mkdir('tests/Feature/' . ucfirst($model), 0777, true); // } // } foreach ($models as $model) { $className = str_replace(self::MODELS_NAMESPACE, '', $model); foreach (['store', 'update', 'show', 'delete'] as $test_name) { $options = (new $model())->factory()->testGeneratorConfig()[$test_name] ?? null; if ($options === null) { continue; } //todo:need work file_put_contents( 'tests/Feature/' . ucfirst($className) . '/' . ucfirst($className) . ucfirst($test_name) . 'Test.php', view( 'tests.' . $test_name, [ 'model' => new $model(), 'sol' => " null, 'auth' => null, ])->render() ); $this->info("Generate " . ucfirst($className) . ucfirst($test_name) . 'Test.php successfully!'); } } $this->info("\nGo enjoy it :) Powered with ❤️ from Haj Mehdi And Masoud Productions (registered trademark)."); } /** * @return \Illuminate\Support\Collection * @throws \Throwable */ private function getSelectedModels() { $models = $this->argument('models'); $models = empty($models) ? $this->getAllModels() : collect(explode(',', $models)); return $models->map(fn($m) => self::MODELS_NAMESPACE . $m)->each(function ($model) { $className = str_replace(self::MODELS_NAMESPACE, '', $model); throw_if(!class_exists($model), $model . ' Not exists!'); throw_if(!$this->passRequirements(new $model), $className . 'Factory must have list, testGeneratorConfig and dependencyProvider functions'); throw_if(!$this->hasBaseFactoryTrait(new $model), $className . 'Factory use BaseFactory trait.'); })->toArray(); } private function getAllModels() { $models = []; $path = app_path('Models'); $files = File::files($path); foreach ($files as $file) { $models [] = str_replace('.php', '', $file->getFileName()); } return collect($models); } public function passRequirements($model) { return method_exists($model->factory(), 'list') && method_exists($model->factory(), 'testGeneratorConfig') && method_exists($model->factory(), 'dependencyProvider'); } /** * @param $model * @return false * @throws \ReflectionException */ public function hasBaseFactoryTrait($model) { return in_array( BaseFactory::class, array_keys((new \ReflectionClass(($model)->factory()))->getTraits()) ); } }