Skip to content
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
17 changes: 9 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ includes:
- vendor/larastan/larastan/extension.neon

parameters:
level: 8
checkGenericClassInNonGenericObjectType: false
universalObjectCratesClasses:
- 'Illuminate\Support\Fluent'
paths:
- src
excludePaths:
- src/Schema
level: 8
ignoreErrors:
- identifier: missingType.generics
universalObjectCratesClasses:
- 'Illuminate\Support\Fluent'
paths:
- src
excludePaths:
- src/Schema
17 changes: 9 additions & 8 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ includes:
- vendor/larastan/larastan/extension.neon

parameters:
level: 8
checkGenericClassInNonGenericObjectType: false
universalObjectCratesClasses:
- 'Illuminate\Support\Fluent'
paths:
- src
excludePaths:
- src/Schema
level: 8
ignoreErrors:
- identifier: missingType.generics
universalObjectCratesClasses:
- 'Illuminate\Support\Fluent'
paths:
- src
excludePaths:
- src/Schema
2 changes: 2 additions & 0 deletions src/AranguentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class AranguentServiceProvider extends ServiceProvider
*/
public function boot()
{
/** @phpstan-ignore offsetAccess.nonOffsetAccessible */
if (isset($this->app['db'])) {
Model::setConnectionResolver($this->app['db']);
}

/** @phpstan-ignore offsetAccess.nonOffsetAccessible */
if (isset($this->app['events'])) {
Model::setEventDispatcher($this->app['events']);
}
Expand Down
13 changes: 10 additions & 3 deletions src/Query/Concerns/BuildsUpdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace LaravelFreelancerNL\Aranguent\Query\Concerns;

use Closure;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
use InvalidArgumentException;
Expand Down Expand Up @@ -64,13 +65,19 @@ public function update(array $values)
* Insert or update a record matching the attributes, and fill it with values.
*
* @param array<mixed> $attributes
* @param array<mixed> $values
* @param array<mixed>|callable $values
* @return bool
* @throws BindException
*/
public function updateOrInsert(array $attributes, array $values = [])
public function updateOrInsert(array $attributes, array|callable $values = [])
{
if (!$this->where($attributes)->exists()) {
$exists = $this->where($attributes)->exists();

if ($values instanceof Closure) {
$values = $values($exists);
}

if (! $exists) {
$this->bindings['where'] = [];
return $this->insert(array_merge($attributes, $values));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Console/MigrateInstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@

test('migrate:install --database=none', function () {
$this->artisan('migrate:install', ['--database' => 'none'])->assertExitCode(0);
})->throws(ErrorException::class);
})->throws(InvalidArgumentException::class);
52 changes: 52 additions & 0 deletions tests/Query/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@
->and($result->en->words)->toBe($words);
});

test('updateOrInsert without values', function () {
$exists = DB::table('characters')->where('id', 'JaimeLannister')->exists();
expect($exists)->toBeTrue();

$result = DB::table('characters')
->updateOrInsert(["_key" => "JaimeLannister"], []);

$exists = DB::table('characters')->where('id', 'JaimeLannister')->exists();

expect($result)->toBeTrue();
expect($exists)->toBeTrue();
});




test('updateOrInsert updates', function () {
DB::table('characters')
->updateOrInsert(["_key" => "NedStark"], ["alive" => false, "age" => 42]);
Expand Down Expand Up @@ -96,6 +112,42 @@
expect($result)->toBeTrue();
});

test('updateOrInsert inserts with callback', function () {
$result = DB::table('characters')->where('id', 'LyannaStark')->exists();

expect($result)->toBeFalse();

$data = [
"name" => "Lyanna",
"surname" => "Stark",
"alive" => false,
"age" => 25,
"residence_id" => "winterfell",
"tags" => [],
];

DB::table('characters')->updateOrInsert(
["_key" => "LyannaStark"],
function ($exists) use ($data) {
if ($exists) {
return [
"name" => "Lyanna",
"surname" => "Stark",
];
}

return [
"name" => "Lyanna",
"surname" => "Stark",
"alive" => $data['alive'],
];
},
);

$result = DB::table('characters')->where('id', 'LyannaStark')->exists();

expect($result)->toBeTrue();
});

test('increment', function () {
$youngNed = DB::table('characters')->where('id', 'NedStark')->first();
Expand Down