Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: FabricatorModel #8770

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -8616,16 +8616,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Test/Fabricator.php',
];
$ignoreErrors[] = [
'message' => '#^Method CodeIgniter\\\\Test\\\\Interfaces\\\\FabricatorModel\\:\\:find\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Test/Interfaces/FabricatorModel.php',
];
$ignoreErrors[] = [
'message' => '#^Method CodeIgniter\\\\Test\\\\Interfaces\\\\FabricatorModel\\:\\:insert\\(\\) has parameter \\$data with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Test/Interfaces/FabricatorModel.php',
];
$ignoreErrors[] = [
'message' => '#^Property CodeIgniter\\\\Test\\\\Mock\\\\MockBuilder\\:\\:\\$supportedIgnoreStatements type has no value type specified in iterable type array\\.$#',
'count' => 1,
Expand Down Expand Up @@ -10401,11 +10391,6 @@
'count' => 1,
'path' => __DIR__ . '/tests/_support/Models/EventModel.php',
];
$ignoreErrors[] = [
'message' => '#^Method Tests\\\\Support\\\\Models\\\\FabricatorModel\\:\\:fake\\(\\) has no return type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/tests/_support/Models/FabricatorModel.php',
];
$ignoreErrors[] = [
'message' => '#^Property Tests\\\\Support\\\\Models\\\\JobModel\\:\\:\\$description has no type specified\\.$#',
'count' => 1,
Expand Down
14 changes: 9 additions & 5 deletions system/Test/Interfaces/FabricatorModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Test\Interfaces;

use CodeIgniter\BaseModel;
use Faker\Generator;
use ReflectionException;

Expand All @@ -27,31 +28,34 @@
* @property string $returnType
* @property string $primaryKey
* @property string $dateFormat
*
* @phpstan-import-type row_array from BaseModel
*/
interface FabricatorModel
{
/**
* Fetches the row of database from $this->table with a primary key
* matching $id.
*
* @param array|mixed|null $id One primary key or an array of primary keys
* @param int|list<int|string>|string|null $id One primary key or an array of primary keys
*
* @return array|object|null The resulting row of data, or null.
* @phpstan-return ($id is int|string ? row_array|object|null : list<row_array|object>)
*/
public function find($id = null);

/**
* Inserts data into the current table. If an object is provided,
* it will attempt to convert it to an array.
*
* @param array|object $data
* @param bool $returnID Whether insert ID should be returned or not.
* @param array|object|null $row
* @phpstan-param row_array|object|null $row
* @param bool $returnID Whether insert ID should be returned or not.
*
* @return bool|int|string
*
* @throws ReflectionException
*/
public function insert($data = null, bool $returnID = true);
public function insert($row = null, bool $returnID = true);

/**
* The following properties and methods are optional, but if present should
Expand Down
6 changes: 4 additions & 2 deletions tests/_support/Models/FabricatorModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ class FabricatorModel extends Model
'description',
];

// Return a faked entity
public function fake(Generator &$faker)
/**
* Return a faked entity
*/
public function fake(Generator &$faker): object
{
return (object) [
'name' => $faker->ipv4(),
Expand Down
10 changes: 10 additions & 0 deletions user_guide_src/source/testing/fabricator/001.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@

class MyModel implements FabricatorModel
{
public function find($id = null)
{
// TODO: Implement find() method.
}

public function insert($row = null, bool $returnID = true)
{
// TODO: Implement insert() method.
}

// ...
}
Loading