Skip to content

Commit

Permalink
fixup! test: add additional type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jun 21, 2024
1 parent 9d9bf88 commit 22e2b14
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 43 deletions.
18 changes: 10 additions & 8 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait BuildsQueries
* Chunk the results of the query.
*
* @param int $count
* @param callable $callback
* @param callable(\Illuminate\Support\Collection<int, TValue>, int): mixed $callback
* @return bool
*/
public function chunk($count, callable $callback)
Expand Down Expand Up @@ -71,9 +71,11 @@ public function chunk($count, callable $callback)
/**
* Run a map over each item while chunking.
*
* @param callable $callback
* @template TReturn
*
* @param callable(TValue): TReturn $callback
* @param int $count
* @return \Illuminate\Support\Collection
* @return \Illuminate\Support\Collection<int, TReturn>
*/
public function chunkMap(callable $callback, $count = 1000)
{
Expand All @@ -91,7 +93,7 @@ public function chunkMap(callable $callback, $count = 1000)
/**
* Execute a callback over each item while chunking.
*
* @param callable $callback
* @param callable(TValue, int): mixed $callback
* @param int $count
* @return bool
*
Expand All @@ -112,7 +114,7 @@ public function each(callable $callback, $count = 1000)
* Chunk the results of a query by comparing IDs.
*
* @param int $count
* @param callable $callback
* @param callable(\Illuminate\Support\Collection<int, TValue>, int): mixed $callback
* @param string|null $column
* @param string|null $alias
* @return bool
Expand All @@ -126,7 +128,7 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
* Chunk the results of a query by comparing IDs in descending order.
*
* @param int $count
* @param callable $callback
* @param callable(\Illuminate\Support\Collection<int, TValue>, int): mixed $callback
* @param string|null $column
* @param string|null $alias
* @return bool
Expand All @@ -140,7 +142,7 @@ public function chunkByIdDesc($count, callable $callback, $column = null, $alias
* Chunk the results of a query by comparing IDs in a given order.
*
* @param int $count
* @param callable $callback
* @param callable(\Illuminate\Support\Collection<int, TValue>, int): mixed $callback
* @param string|null $column
* @param string|null $alias
* @param bool $descending
Expand Down Expand Up @@ -200,7 +202,7 @@ public function orderedChunkById($count, callable $callback, $column = null, $al
/**
* Execute a callback over each item while chunking by ID.
*
* @param callable $callback
* @param callable(TValue, int): mixed $callback
* @param int $count
* @param string|null $column
* @param string|null $alias
Expand Down
22 changes: 11 additions & 11 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public function whereKeyNot($id)
/**
* Add a basic where clause to the query.
*
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(static): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand All @@ -319,7 +319,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
/**
* Add a basic where clause to the query, and return the first result.
*
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(static): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand All @@ -333,7 +333,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean =
/**
* Add an "or where" clause to the query.
*
* @param \Closure|array|string|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(static): mixed)|array|string|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return $this
Expand All @@ -350,7 +350,7 @@ public function orWhere($column, $operator = null, $value = null)
/**
* Add a basic "where not" clause to the query.
*
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(static): mixed)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand All @@ -364,7 +364,7 @@ public function whereNot($column, $operator = null, $value = null, $boolean = 'a
/**
* Add an "or where not" clause to the query.
*
* @param \Closure|array|string|\Illuminate\Contracts\Database\Query\Expression $column
* @param (\Closure(static): mixed)|array|string|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return $this
Expand Down Expand Up @@ -448,7 +448,7 @@ public function fromQuery($query, $bindings = [])
*
* @param mixed $id
* @param array|string $columns
* @return TModel|\Illuminate\Database\Eloquent\Collection<int, TModel>|null
* @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TModel> : TModel|null)
*/
public function find($id, $columns = ['*'])
{
Expand Down Expand Up @@ -482,7 +482,7 @@ public function findMany($ids, $columns = ['*'])
*
* @param mixed $id
* @param array|string $columns
* @return TModel|\Illuminate\Database\Eloquent\Collection<int, TModel>
* @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TModel> : TModel)
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<TModel>
*/
Expand Down Expand Up @@ -516,7 +516,7 @@ public function findOrFail($id, $columns = ['*'])
*
* @param mixed $id
* @param array|string $columns
* @return TModel
* @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TModel> : TModel)
*/
public function findOrNew($id, $columns = ['*'])
{
Expand All @@ -536,9 +536,9 @@ public function findOrNew($id, $columns = ['*'])
* @param \Closure|array|string $columns
* @param (\Closure(): TFindOrValue)|null $callback
* @return (
* $callback is null
* ? TModel|\Illuminate\Database\Eloquent\Collection<int, TModel>|null
* : TModel|\Illuminate\Database\Eloquent\Collection<int, TModel>|TFindOrValue
* $id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
* ? \Illuminate\Database\Eloquent\Collection<int, TModel>
* : ($callback is null ? TModel|null : TModel|TFindOrValue)
* )
*/
public function findOr($id, $columns = ['*'], ?Closure $callback = null)
Expand Down
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public function orderByPivot($column, $direction = 'asc')
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>|TRelatedModel
* @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> : TRelatedModel)
*/
public function findOrNew($id, $columns = ['*'])
{
Expand Down Expand Up @@ -675,7 +675,7 @@ public function updateOrCreate(array $attributes, array $values = [], array $joi
*
* @param mixed $id
* @param array $columns
* @return TRelatedModel|\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>|null
* @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> : TRelatedModel|null)
*/
public function find($id, $columns = ['*'])
{
Expand Down Expand Up @@ -713,7 +713,7 @@ public function findMany($ids, $columns = ['*'])
*
* @param mixed $id
* @param array $columns
* @return TRelatedModel|\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>
* @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> : TRelatedModel)
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
*/
Expand Down Expand Up @@ -743,9 +743,9 @@ public function findOrFail($id, $columns = ['*'])
* @param \Closure|array $columns
* @param (\Closure(): TFindOrValue)|null $callback
* @return (
* $callback is null
* ? TRelatedModel|\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>|null
* : TRelatedModel|\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>|TFindOrValue
* $id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
* ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>
* : ($callback is null ? TRelatedModel|null : TRelatedModel|TFindOrValue)
* )
*/
public function findOr($id, $columns = ['*'], ?Closure $callback = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ protected function buildDictionary(Collection $results)
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>|TRelatedModel
* @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> : TRelatedModel)
*/
public function findOrNew($id, $columns = ['*'])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function firstOr($columns = ['*'], ?Closure $callback = null)
*
* @param mixed $id
* @param array $columns
* @return TRelatedModel|\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>|null
* @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> : TRelatedModel|null)
*/
public function find($id, $columns = ['*'])
{
Expand Down Expand Up @@ -367,7 +367,7 @@ public function findMany($ids, $columns = ['*'])
*
* @param mixed $id
* @param array $columns
* @return TRelatedModel|\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>
* @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> : TRelatedModel)
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
*/
Expand Down Expand Up @@ -397,9 +397,9 @@ public function findOrFail($id, $columns = ['*'])
* @param \Closure|array $columns
* @param (\Closure(): TFindOrValue)|null $callback
* @return (
* $callback is null
* ? TRelatedModel|\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>|null
* : TRelatedModel|\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>|TFindOrValue
* $id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
* ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>
* : ($callback is null ? TRelatedModel|null : TRelatedModel|TFindOrValue)
* )
*/
public function findOr($id, $columns = ['*'], ?Closure $callback = null)
Expand Down
51 changes: 47 additions & 4 deletions types/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ function test(
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $query->hydrate([]));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $query->fromQuery('foo', []));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $query->findMany([1, 2, 3]));
assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $query->findOrFail(1));
assertType('Illuminate\Database\Eloquent\Collection<int, User>|User|null', $query->find(1));
assertType('Illuminate\Database\Eloquent\Collection<int, User>|User|null', $query->findOr(1));
assertType('Illuminate\Database\Eloquent\Collection<int, User>|int|User', $query->findOr(1, callback: fn () => 42));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $query->findOrFail([1]));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $query->findOrNew([1]));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $query->find([1]));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $query->findOr([1], callback: fn () => 42));
assertType('User', $query->findOrFail(1));
assertType('User|null', $query->find(1));
assertType('User|null', $query->findOr(1));
assertType('int|User', $query->findOr(1, callback: fn () => 42));
assertType('User|null', $query->first());
assertType('User|null', $query->firstOr());
assertType('int|User', $query->firstOr(callback: fn () => 42));
Expand Down Expand Up @@ -82,6 +86,30 @@ function test(
assertType('Illuminate\Database\Eloquent\Builder<User>', $query->orWhereMorphedTo($post->taggable(), new Post()));
assertType('Illuminate\Database\Eloquent\Builder<User>', $query->orWhereNotMorphedTo($post->taggable(), new Post()));

$query->chunk(1, function ($users, $page) {
assertType('Illuminate\Support\Collection<int, User>', $users);
assertType('int', $page);
});
$query->chunkById(1, function ($users, $page) {
assertType('Illuminate\Support\Collection<int, User>', $users);
assertType('int', $page);
});
$query->chunkMap(function ($users) {
assertType('User', $users);
});
$query->chunkByIdDesc(1, function ($users, $page) {
assertType('Illuminate\Support\Collection<int, User>', $users);
assertType('int', $page);
});
$query->each(function ($users, $page) {
assertType('User', $users);
assertType('int', $page);
});
$query->eachById(function ($users, $page) {
assertType('User', $users);
assertType('int', $page);
});

assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\Post>', Post::query());
assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\Post>', Post::on());
assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\Post>', Post::onWriteConnection());
Expand Down Expand Up @@ -111,6 +139,21 @@ function test(
assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\ChildPost>', $childPost->newQuery()->where('foo', 'bar'));
assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\ChildPost>', $childPost->newQuery()->foo());
assertType('Illuminate\Types\Builder\ChildPost', $childPost->newQuery()->create(['name' => 'John']));
$childPost->newQuery()->where(function ($query) {
assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\ChildPost>', $query);
});
$childPost->newQuery()->firstWhere(function ($query) {
assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\ChildPost>', $query);
});
$childPost->newQuery()->orWhere(function ($query) {
assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\ChildPost>', $query);
});
$childPost->newQuery()->whereNot(function ($query) {
assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\ChildPost>', $query);
});
$childPost->newQuery()->orWhereNot(function ($query) {
assertType('Illuminate\Types\Builder\CommonBuilder<Illuminate\Types\Builder\ChildPost>', $query);
});

assertType('Illuminate\Types\Builder\CommentBuilder', Comment::query());
assertType('Illuminate\Types\Builder\CommentBuilder', Comment::on());
Expand Down
22 changes: 14 additions & 8 deletions types/Database/Eloquent/Relations.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ function test(User $user, Post $post, Comment $comment, ChildUser $child): void

assertType('Illuminate\Database\Eloquent\Relations\BelongsToMany<Illuminate\Types\Relations\Role, Illuminate\Types\Relations\User>', $user->roles());
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>', $user->roles()->getResults());
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>', $user->roles()->find([1]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>', $user->roles()->findMany([1, 2, 3]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>|Illuminate\Types\Relations\Role', $user->roles()->findOrNew(1));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>|Illuminate\Types\Relations\Role', $user->roles()->findOrFail(1));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>|Illuminate\Types\Relations\Role|null', $user->roles()->find(1));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>|Illuminate\Types\Relations\Role|null', $user->roles()->findOr(1));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>|Illuminate\Types\Relations\Role|int', $user->roles()->findOr(1, callback: fn () => 42));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>', $user->roles()->findOrNew([1]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>', $user->roles()->findOrFail([1]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>', $user->roles()->findOr([1], callback: fn () => 42));
assertType('Illuminate\Types\Relations\Role', $user->roles()->findOrNew(1));
assertType('Illuminate\Types\Relations\Role', $user->roles()->findOrFail(1));
assertType('Illuminate\Types\Relations\Role|null', $user->roles()->find(1));
assertType('Illuminate\Types\Relations\Role|null', $user->roles()->findOr(1));
assertType('Illuminate\Types\Relations\Role|int', $user->roles()->findOr(1, callback: fn () => 42));
assertType('Illuminate\Types\Relations\Role|null', $user->roles()->first());
assertType('Illuminate\Types\Relations\Role|null', $user->roles()->firstOr());
assertType('Illuminate\Types\Relations\Role|int', $user->roles()->firstOr(callback: fn () => 42));
Expand All @@ -72,9 +76,11 @@ function test(User $user, Post $post, Comment $comment, ChildUser $child): void

assertType('Illuminate\Database\Eloquent\Relations\HasOneThrough<Illuminate\Types\Relations\Car, Illuminate\Types\Relations\Mechanic, Illuminate\Types\Relations\User>', $user->car());
assertType('Illuminate\Types\Relations\Car|null', $user->car()->getResults());
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Car>|Illuminate\Types\Relations\Car|null', $user->car()->find(1));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Car>|Illuminate\Types\Relations\Car|null', $user->car()->findOr(1));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Car>|Illuminate\Types\Relations\Car|int', $user->car()->findOr(1, callback: fn () => 42));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Car>', $user->car()->find([1]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Car>', $user->car()->findOr([1], callback: fn () => 42));
assertType('Illuminate\Types\Relations\Car|null', $user->car()->find(1));
assertType('Illuminate\Types\Relations\Car|null', $user->car()->findOr(1));
assertType('Illuminate\Types\Relations\Car|int', $user->car()->findOr(1, callback: fn () => 42));
assertType('Illuminate\Types\Relations\Car|null', $user->car()->first());
assertType('Illuminate\Types\Relations\Car|null', $user->car()->firstOr());
assertType('Illuminate\Types\Relations\Car|int', $user->car()->firstOr(callback: fn () => 42));
Expand Down
Loading

0 comments on commit 22e2b14

Please sign in to comment.