Skip to content

Commit e17344e

Browse files
authored
Fix methods that don't work properly (#14)
1 parent c960c97 commit e17344e

File tree

1 file changed

+40
-46
lines changed

1 file changed

+40
-46
lines changed

src/Codeception/Module/Laravel.php

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Illuminate\Foundation\Application;
3232
use Illuminate\Http\Request;
3333
use Illuminate\Routing\Route;
34+
use Illuminate\Routing\Router;
3435
use Illuminate\Support\Collection;
3536
use Illuminate\Support\ViewErrorBag;
3637
use ReflectionClass;
@@ -462,14 +463,16 @@ public function callArtisan(string $command, $parameters = [], OutputInterface $
462463
* ```
463464
*
464465
* @param string $routeName
465-
* @param array $params
466+
* @param mixed $params
466467
*/
467-
public function amOnRoute(string $routeName, array $params = []): void
468+
public function amOnRoute(string $routeName, $params = []): void
468469
{
469470
$route = $this->getRouteByName($routeName);
470471

471472
$absolute = !is_null($route->domain());
472-
$url = $this->app['url']->route($routeName, $params, $absolute);
473+
/** @var UrlGenerator $urlGenerator */
474+
$urlGenerator = $this->app['url'];
475+
$url = $urlGenerator->route($routeName, $params, $absolute);
473476
$this->amOnPage($url);
474477
}
475478

@@ -562,7 +565,10 @@ public function seeCurrentActionIs(string $action): void
562565
*/
563566
protected function getRouteByName(string $routeName)
564567
{
565-
if (!$route = $this->app['routes']->getByName($routeName)) {
568+
/** @var Router $router */
569+
$router = $this->app['router'];
570+
$routes = $router->getRoutes();
571+
if (!$route = $routes->getByName($routeName)) {
566572
$this->fail("Route with name '$routeName' does not exist");
567573
}
568574

@@ -1126,17 +1132,16 @@ protected function getQueryBuilderFromTable(string $table)
11261132
}
11271133

11281134
/**
1129-
* Use Laravel's model factory to create a model.
1130-
* Can only be used with Laravel 5.1 and later.
1135+
* Use Laravel model factory to create a model.
11311136
*
11321137
* ``` php
11331138
* <?php
1134-
* $I->have('App\User');
1135-
* $I->have('App\User', ['name' => 'John Doe']);
1136-
* $I->have('App\User', [], 'admin');
1139+
* $I->have('App\Models\User');
1140+
* $I->have('App\Models\User', ['name' => 'John Doe']);
1141+
* $I->have('App\Models\User', [], 'admin');
11371142
* ```
11381143
*
1139-
* @see http://laravel.com/docs/5.1/testing#model-factories
1144+
* @see https://laravel.com/docs/6.x/database-testing#using-factories
11401145
* @param string $model
11411146
* @param array $attributes
11421147
* @param string $name
@@ -1146,31 +1151,30 @@ protected function getQueryBuilderFromTable(string $table)
11461151
public function have(string $model, array $attributes = [], string $name = 'default')
11471152
{
11481153
try {
1149-
$result = $this->modelFactory($model, $name)->create($attributes);
1154+
$model = $this->modelFactory($model, $name)->create($attributes);
11501155

1151-
// Since Laravel 5.4 the model factory returns a collection instead of a single object
1152-
if ($result instanceof Collection) {
1153-
$result = $result[0];
1156+
// In Laravel 6 the model factory returns a collection instead of a single object
1157+
if ($model instanceof Collection) {
1158+
$model = $model[0];
11541159
}
11551160

1156-
return $result;
1161+
return $model;
11571162
} catch (Exception $e) {
1158-
$this->fail("Could not create model: \n\n" . get_class($e) . "\n\n" . $e->getMessage());
1163+
$this->fail('Could not create model: \n\n' . get_class($e) . '\n\n' . $e->getMessage());
11591164
}
11601165
}
11611166

11621167
/**
1163-
* Use Laravel's model factory to create multiple models.
1164-
* Can only be used with Laravel 5.1 and later.
1168+
* Use Laravel model factory to create multiple models.
11651169
*
11661170
* ``` php
11671171
* <?php
1168-
* $I->haveMultiple('App\User', 10);
1169-
* $I->haveMultiple('App\User', 10, ['name' => 'John Doe']);
1170-
* $I->haveMultiple('App\User', 10, [], 'admin');
1172+
* $I->haveMultiple('App\Models\User', 10);
1173+
* $I->haveMultiple('App\Models\User', 10, ['name' => 'John Doe']);
1174+
* $I->haveMultiple('App\Models\User', 10, [], 'admin');
11711175
* ```
11721176
*
1173-
* @see http://laravel.com/docs/5.1/testing#model-factories
1177+
* @see https://laravel.com/docs/6.x/database-testing#using-factories
11741178
* @param string $model
11751179
* @param int $times
11761180
* @param array $attributes
@@ -1188,17 +1192,16 @@ public function haveMultiple(string $model, int $times, array $attributes = [],
11881192
}
11891193

11901194
/**
1191-
* Use Laravel's model factory to make a model instance.
1192-
* Can only be used with Laravel 5.1 and later.
1195+
* Use Laravel model factory to make a model instance.
11931196
*
11941197
* ``` php
11951198
* <?php
1196-
* $I->make('App\User');
1197-
* $I->make('App\User', ['name' => 'John Doe']);
1198-
* $I->make('App\User', [], 'admin');
1199+
* $I->make('App\Models\User');
1200+
* $I->make('App\Models\User', ['name' => 'John Doe']);
1201+
* $I->make('App\Models\User', [], 'admin');
11991202
* ```
12001203
*
1201-
* @see http://laravel.com/docs/5.1/testing#model-factories
1204+
* @see https://laravel.com/docs/6.x/database-testing#using-factories
12021205
* @param string $model
12031206
* @param array $attributes
12041207
* @param string $name
@@ -1215,17 +1218,16 @@ public function make(string $model, array $attributes = [], string $name = 'defa
12151218
}
12161219

12171220
/**
1218-
* Use Laravel's model factory to make multiple model instances.
1219-
* Can only be used with Laravel 5.1 and later.
1221+
* Use Laravel model factory to make multiple model instances.
12201222
*
12211223
* ``` php
12221224
* <?php
1223-
* $I->makeMultiple('App\User', 10);
1224-
* $I->makeMultiple('App\User', 10, ['name' => 'John Doe']);
1225-
* $I->makeMultiple('App\User', 10, [], 'admin');
1225+
* $I->makeMultiple('App\Models\User', 10);
1226+
* $I->makeMultiple('App\Models\User', 10, ['name' => 'John Doe']);
1227+
* $I->makeMultiple('App\Models\User', 10, [], 'admin');
12261228
* ```
12271229
*
1228-
* @see http://laravel.com/docs/5.1/testing#model-factories
1230+
* @see https://laravel.com/docs/6.x/database-testing#using-factories
12291231
* @param string $model
12301232
* @param int $times
12311233
* @param array $attributes
@@ -1246,23 +1248,15 @@ public function makeMultiple(string $model, int $times, array $attributes = [],
12461248
* @param string $model
12471249
* @param string $name
12481250
* @param int $times
1249-
* @return FactoryBuilder
1250-
* @throws ModuleException
1251+
* @return FactoryBuilder|\Illuminate\Database\Eloquent\Factories\Factory
12511252
*/
1252-
protected function modelFactory(string $model, string $name, $times = 1): FactoryBuilder
1253+
protected function modelFactory(string $model, string $name, $times = 1)
12531254
{
1254-
if (! function_exists('factory')) {
1255-
throw new ModuleException($this, 'The factory() method does not exist. ' .
1256-
'This functionality relies on Laravel model factories, which were introduced in Laravel 5.1.');
1257-
}
1258-
12591255
if (version_compare(Application::VERSION, '7.0.0', '<')) {
1260-
$factory = factory($model, $name, $times);
1261-
} else {
1262-
$factory = factory($model, $times);
1256+
return factory($model, $name, $times);
12631257
}
12641258

1265-
return $factory;
1259+
return $model::factory()->count($times);
12661260
}
12671261

12681262
/**

0 commit comments

Comments
 (0)