Skip to content

Commit

Permalink
OrderByRandom Test and Docs + PHPStan clean up. Pest upgrade to 3
Browse files Browse the repository at this point in the history
  • Loading branch information
pdphilip committed Sep 23, 2024
1 parent 0caafee commit b61da1f
Show file tree
Hide file tree
Showing 9 changed files with 662 additions and 620 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"orchestra/testbench": "^9.0.0||^8.22.0",
"mockery/mockery": "^1.4.4",
"doctrine/coding-standard": "12.0.x-dev",
"pestphp/pest": "^2.34",
"pestphp/pest-plugin-laravel": "^2.4",
"pestphp/pest": "^3",
"pestphp/pest-plugin-laravel": "^3",
"laravel/pint": "^1.14",
"nunomaduro/collision": "^8.1.1||^7.10.0",
"larastan/larastan": "^2.9",
"pestphp/pest-plugin-arch": "^2.7",
"pestphp/pest-plugin-arch": "^3",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3"
Expand Down
1,226 changes: 618 additions & 608 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,13 @@ public function findOrNew($id, $columns = ['*']): Model
* Performs a raw search using the provided body parameters.
*
* @param array $bodyParams The body parameters to use for the search.
* @return ElasticCollection The search results as an ElasticCollection object.
* @return TCollection
*/
public function rawSearch(array $bodyParams): ElasticCollection
{
$data = $this->query->rawSearch($bodyParams);
$results = $this->model->hydrate($data->data)->all();
$elasticCollection = $this->hydrate($data->data);
$meta = $data->getMetaData();

$elasticCollection = $this->getModel()->newCollection($results);
$elasticCollection->setQueryMeta($meta);

return $elasticCollection;
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Docs/ModelDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @method static $this orderByNested(string $column, string $direction = 'asc', string $mode = null)
* @method static $this filterGeoBox(string $column, array $topLeftCoords, array $bottomRightCoords)
* @method static $this filterGeoPoint(string $column, string $distance, array $point)
* @method static $this orderByRandom(string $column, int $seed = 1)
*
* Full Text Search Methods ---------------------------------
* @method static $this searchFor($value, $fields = ['*'], $options = [], $boolean = 'and')
Expand Down
2 changes: 2 additions & 0 deletions src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
[$type, $id] = $this->getMorphs($name, $type, $id);

if (($class = $this->$type) === null) {
//@phpstan-ignore-next-line
return new MorphTo($this->newQuery(), $this, $id, $ownerKey, $type, $name);
}

Expand All @@ -131,6 +132,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
*/
public function newEloquentBuilder($query): EloquentBuilder|Builder
{
//@phpstan-ignore-next-line
if (is_subclass_of($this, ParentModel::class)) {
return new Builder($query);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,8 @@ public function saveWithoutRefresh(array $options = []): bool
$query->setRefresh(false);

if ($this->exists) {
//@phpstan-ignore-next-line
$saved = ! $this->isDirty() || $this->performUpdate($query);
} else {
//@phpstan-ignore-next-line
$saved = $this->performInsert($query);
}

Expand Down
22 changes: 20 additions & 2 deletions tests/Eloquent/ElasticsearchSpecificTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
expect($regexProducts)->toHaveCount(3);
});

test('execute raw DSL query on products', function () {
test('execute raw search query on products', function () {
Product::factory()->state(['color' => 'silver'])->create();
Product::factory(2)->state(['color' => 'silver'])->create();
Product::factory(1)->state(['color' => 'blue'])->create();
Expand All @@ -120,11 +120,29 @@

expect($products)
->toBeInstanceOf(ElasticCollection::class)
->toHaveCount(2)
->toHaveCount(3)
->and($products->first())->toBeInstanceOf(Product::class)
->and($products->first()['color'])->toBe('silver');
});

test('execute raw DSL query on products', function () {
Product::factory()->state(['color' => 'silver'])->create();
Product::factory(2)->state(['color' => 'silver'])->create();
Product::factory(1)->state(['color' => 'blue'])->create();

$bodyParams = [
'query' => [
'match' => [
'color' => 'silver',
],
],
];
$raw = Product::rawDsl($bodyParams);

expect($raw['hits']['total']['value'])->toBe(3)
->and($raw['hits']['hits'][0]['_source']['color'])->toBe('silver');
});

test('perform raw aggregation query', function () {
Product::factory()->state(['price' => 50])->create();
Product::factory()->state(['price' => 300])->create();
Expand Down
15 changes: 15 additions & 0 deletions tests/Eloquent/OrderAndPaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,18 @@ function isSorted(Collection $collection, $key, $descending = false): bool
$products = Product::orderByGeo('manufacturer.location', [[2.3488, 48.85341], [-0.12574, 51.50853]], 'desc', 'km', 'avg', 'plane')->get();
expect(! empty($products))->toBeTrue();
});

test('sort products by random', function () {
$products = Product::factory(50)->make();
Product::insert($products->toArray());

$sortA = Product::where('orders', '>', 0)->orderByRandom('orders', 5)->limit(5)->get();
$sortAFirstId = $sortA->first()->_id;
$sortB = Product::where('orders', '>', 0)->orderByRandom('orders', 7)->limit(5)->get();
$sortBFirstId = $sortB->first()->_id;
expect($sortAFirstId == $sortBFirstId)->toBeFalse('Seed 5 and 7 should have different results');
$sortC = Product::where('orders', '>', 0)->orderByRandom('orders', 5)->limit(5)->get();
$sortCFirstId = $sortC->first()->_id;
expect($sortAFirstId == $sortCFirstId)->toBeTrue('Same Seeds should have same results');

});
2 changes: 1 addition & 1 deletion workbench/database/factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function definition(): array
'country' => fake()->country(),
],
],
'created_at' => Carbon::now(),
'created_at' => $this->faker->dateTimeBetween('-31 days'),
'updated_at' => Carbon::now(),
'deleted_at' => null,
];
Expand Down

0 comments on commit b61da1f

Please sign in to comment.