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

[1.x] Fixes mapping and mock expectations. #2

Merged
merged 3 commits into from
Jan 10, 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
14 changes: 12 additions & 2 deletions src/RefineQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,22 @@ protected function validateRefiner()
protected function queryValuesFromRequest(Request $request, ?array $keys): array
{
return Collection::make($keys ?? $this->getKeysFromRefiner($this->request))
->mapWithKeys(static function ($key): array {
// Transforms all items to $method => $key
->mapWithKeys(static function (string $key): array {
return [Str::camel($key) => $key];
})
// Remove all keys that are not present in the request query.
->filter(static function (string $key) use ($request): bool {
return null !== $request->query($key);
})
// Keep all items which method is present in the refiner object.
->intersectByKeys(array_flip($this->getPublicMethodsFromRefiner()))
// Remove all items which method are part of the abstract refiner object.
->diffKeys(array_flip($this->getRefinerClassMethods()))
->map([$request, 'query'])
// Transforms all items into $method => $value
->map(static function (string $key) use ($request): string {
return $request->query($key);
})
->toArray();
}

Expand Down
64 changes: 32 additions & 32 deletions tests/RefinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public function test_calls_run_before_with_request_and_builder(Closure $getQuery
{
$builder = $getQuery();

$this->partialMock(MockRefiner::class, function (MockInterface $mock) use ($builder): void {
$mock->shouldReceive('runBefore')->with($builder, $this->app->make('request'))->once();
});
$mock = $this->partialMock(MockRefiner::class);

$builder->refineBy(MockRefiner::class);

$mock->shouldHaveReceived('runBefore')->with($builder, $this->app->make('request'))->once();
}

/**
Expand All @@ -57,13 +57,12 @@ public function test_calls_run_after_with_request_and_builder(Closure $getQuery)
{
$builder = $getQuery();

$this->partialMock(MockRefiner::class, function (MockInterface $mock) use ($builder): void {
$mock->shouldReceive('runAfter')->with($builder, $this->app->make('request'))->once();
});
$mock = $this->partialMock(MockRefiner::class);

$builder->refineBy(MockRefiner::class);
}

$mock->shouldHaveReceived('runAfter')->with($builder, $this->app->make('request'))->once();
}

/**
* @param \Closure():\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $getQuery
Expand All @@ -76,13 +75,13 @@ public function test_calls_matched_methods_from_request(Closure $getQuery): void

$builder = $getQuery();

$this->partialMock(MockRefiner::class, function (MockInterface $mock) use ($builder): void {
$mock->shouldReceive('foo')->with($builder, 1, $this->app->make('request'))->once();
$mock->shouldReceive('bar')->with($builder, 2, $this->app->make('request'))->once();
$mock->shouldNotReceive('quz');
});
$mock = $this->partialMock(MockRefiner::class);

$builder->refineBy(MockRefiner::class);

$mock->shouldHaveReceived('foo')->with($builder, 1, $this->app->make('request'))->once();
$mock->shouldHaveReceived('bar')->with($builder, 2, $this->app->make('request'))->once();
$mock->shouldNotHaveReceived('quz');
}

/**
Expand All @@ -96,13 +95,13 @@ public function test_calls_matched_method_using_camel_Case(Closure $getQuery): v

$builder = $getQuery();

$this->partialMock(MockCamelCaseRefiner::class, function (MockInterface $mock) use ($builder): void {
$mock->shouldReceive('fooBar')->with($builder, 1, $this->app->make('request'))->once();
$mock->shouldReceive('barQuz')->with($builder, 2, $this->app->make('request'))->once();
$mock->shouldReceive('qUZFOX')->with($builder, 3, $this->app->make('request'))->once();
});
$mock = $this->partialMock(MockCamelCaseRefiner::class);

$builder->refineBy(MockCamelCaseRefiner::class);

$mock->shouldhaveReceived('fooBar')->with($builder, 1, $this->app->make('request'))->once();
$mock->shouldhaveReceived('barQuz')->with($builder, 2, $this->app->make('request'))->once();
$mock->shouldhaveReceived('qUZFOX')->with($builder, 3, $this->app->make('request'))->once();
}

/**
Expand All @@ -114,15 +113,16 @@ public function test_doesnt_calls_non_callable_methods(Closure $getQuery): void
{
$this->mockRequest(['__construct' => 1, 'protected' => 2, 'static' => 3, '__destruct' => 4]);

$this->partialMock(MockVariedMethodsRefiner::class, function (MockInterface $mock): void {
$mock = $this->partialMock(MockVariedMethodsRefiner::class, function (MockInterface $mock): void {
$mock->shouldAllowMockingProtectedMethods();
$mock->shouldNotReceive('__construct');
$mock->shouldNotReceive('protected');
$mock->shouldNotReceive('static');
$mock->shouldNotReceive('__destruct');
});

$getQuery()->refineBy(MockVariedMethodsRefiner::class);

$mock->shouldNotHaveReceived('__construct');
$mock->shouldNotHaveReceived('protected');
$mock->shouldNotHaveReceived('static');
$mock->shouldNotHaveReceived('__destruct');
}

/**
Expand All @@ -134,13 +134,13 @@ public function test_doesnt_calls_refiner_included_methods(Closure $getQuery): v
{
$this->mockRequest(['get-keys' => 1, 'run-before' => 2, 'run-after' => 4]);

$this->partialMock(MockVariedMethodsRefiner::class, function (MockInterface $mock): void {
$mock->shouldNotReceive('getKeys');
$mock->shouldNotReceive('runBefore');
$mock->shouldNotReceive('runAfter');
});
$mock = $this->partialMock(MockVariedMethodsRefiner::class);

$getQuery()->refineBy(MockRefiner::class);

$mock->shouldNotHaveReceived('getKeys');
$mock->shouldNotHaveReceived('runBefore');
$mock->shouldNotHaveReceived('runAfter');
}

/**
Expand All @@ -154,13 +154,13 @@ public function test_calls_matched_methods_from_request_using_custom_keys(Closur

$builder = $getQuery();

$this->partialMock(MockRefiner::class, function (MockInterface $mock) use ($builder): void {
$mock->shouldNotReceive('foo');
$mock->shouldReceive('bar')->with($builder, 2, $this->app->make('request'))->once();
$mock->shouldNotReceive('quz');
});
$mock = $this->partialMock(MockRefiner::class);

$builder->refineBy(MockRefiner::class, ['bar']);

$mock->shouldHaveReceived('bar')->with($builder, 2, $this->app->make('request'))->once();
$mock->shouldNotHaveReceived('foo');
$mock->shouldNotHaveReceived('quz');
}

/**
Expand Down