Skip to content

Commit b61da1f

Browse files
committed
OrderByRandom Test and Docs + PHPStan clean up. Pest upgrade to 3
1 parent 0caafee commit b61da1f

File tree

9 files changed

+662
-620
lines changed

9 files changed

+662
-620
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
"orchestra/testbench": "^9.0.0||^8.22.0",
3131
"mockery/mockery": "^1.4.4",
3232
"doctrine/coding-standard": "12.0.x-dev",
33-
"pestphp/pest": "^2.34",
34-
"pestphp/pest-plugin-laravel": "^2.4",
33+
"pestphp/pest": "^3",
34+
"pestphp/pest-plugin-laravel": "^3",
3535
"laravel/pint": "^1.14",
3636
"nunomaduro/collision": "^8.1.1||^7.10.0",
3737
"larastan/larastan": "^2.9",
38-
"pestphp/pest-plugin-arch": "^2.7",
38+
"pestphp/pest-plugin-arch": "^3",
3939
"phpstan/extension-installer": "^1.3",
4040
"phpstan/phpstan-deprecation-rules": "^1.1",
4141
"phpstan/phpstan-phpunit": "^1.3"

composer.lock

Lines changed: 618 additions & 608 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Eloquent/Builder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,13 @@ public function findOrNew($id, $columns = ['*']): Model
218218
* Performs a raw search using the provided body parameters.
219219
*
220220
* @param array $bodyParams The body parameters to use for the search.
221-
* @return ElasticCollection The search results as an ElasticCollection object.
221+
* @return TCollection
222222
*/
223223
public function rawSearch(array $bodyParams): ElasticCollection
224224
{
225225
$data = $this->query->rawSearch($bodyParams);
226-
$results = $this->model->hydrate($data->data)->all();
226+
$elasticCollection = $this->hydrate($data->data);
227227
$meta = $data->getMetaData();
228-
229-
$elasticCollection = $this->getModel()->newCollection($results);
230228
$elasticCollection->setQueryMeta($meta);
231229

232230
return $elasticCollection;

src/Eloquent/Docs/ModelDocs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @method static $this orderByNested(string $column, string $direction = 'asc', string $mode = null)
4747
* @method static $this filterGeoBox(string $column, array $topLeftCoords, array $bottomRightCoords)
4848
* @method static $this filterGeoPoint(string $column, string $distance, array $point)
49+
* @method static $this orderByRandom(string $column, int $seed = 1)
4950
*
5051
* Full Text Search Methods ---------------------------------
5152
* @method static $this searchFor($value, $fields = ['*'], $options = [], $boolean = 'and')

src/Eloquent/HybridRelations.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
114114
[$type, $id] = $this->getMorphs($name, $type, $id);
115115

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

@@ -131,6 +132,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
131132
*/
132133
public function newEloquentBuilder($query): EloquentBuilder|Builder
133134
{
135+
//@phpstan-ignore-next-line
134136
if (is_subclass_of($this, ParentModel::class)) {
135137
return new Builder($query);
136138
}

src/Eloquent/Model.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,8 @@ public function saveWithoutRefresh(array $options = []): bool
285285
$query->setRefresh(false);
286286

287287
if ($this->exists) {
288-
//@phpstan-ignore-next-line
289288
$saved = ! $this->isDirty() || $this->performUpdate($query);
290289
} else {
291-
//@phpstan-ignore-next-line
292290
$saved = $this->performInsert($query);
293291
}
294292

tests/Eloquent/ElasticsearchSpecificTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
expect($regexProducts)->toHaveCount(3);
105105
});
106106

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

121121
expect($products)
122122
->toBeInstanceOf(ElasticCollection::class)
123-
->toHaveCount(2)
123+
->toHaveCount(3)
124124
->and($products->first())->toBeInstanceOf(Product::class)
125125
->and($products->first()['color'])->toBe('silver');
126126
});
127127

128+
test('execute raw DSL query on products', function () {
129+
Product::factory()->state(['color' => 'silver'])->create();
130+
Product::factory(2)->state(['color' => 'silver'])->create();
131+
Product::factory(1)->state(['color' => 'blue'])->create();
132+
133+
$bodyParams = [
134+
'query' => [
135+
'match' => [
136+
'color' => 'silver',
137+
],
138+
],
139+
];
140+
$raw = Product::rawDsl($bodyParams);
141+
142+
expect($raw['hits']['total']['value'])->toBe(3)
143+
->and($raw['hits']['hits'][0]['_source']['color'])->toBe('silver');
144+
});
145+
128146
test('perform raw aggregation query', function () {
129147
Product::factory()->state(['price' => 50])->create();
130148
Product::factory()->state(['price' => 300])->create();

tests/Eloquent/OrderAndPaginationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,18 @@ function isSorted(Collection $collection, $key, $descending = false): bool
7676
$products = Product::orderByGeo('manufacturer.location', [[2.3488, 48.85341], [-0.12574, 51.50853]], 'desc', 'km', 'avg', 'plane')->get();
7777
expect(! empty($products))->toBeTrue();
7878
});
79+
80+
test('sort products by random', function () {
81+
$products = Product::factory(50)->make();
82+
Product::insert($products->toArray());
83+
84+
$sortA = Product::where('orders', '>', 0)->orderByRandom('orders', 5)->limit(5)->get();
85+
$sortAFirstId = $sortA->first()->_id;
86+
$sortB = Product::where('orders', '>', 0)->orderByRandom('orders', 7)->limit(5)->get();
87+
$sortBFirstId = $sortB->first()->_id;
88+
expect($sortAFirstId == $sortBFirstId)->toBeFalse('Seed 5 and 7 should have different results');
89+
$sortC = Product::where('orders', '>', 0)->orderByRandom('orders', 5)->limit(5)->get();
90+
$sortCFirstId = $sortC->first()->_id;
91+
expect($sortAFirstId == $sortCFirstId)->toBeTrue('Same Seeds should have same results');
92+
93+
});

workbench/database/factories/ProductFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function definition(): array
3636
'country' => fake()->country(),
3737
],
3838
],
39-
'created_at' => Carbon::now(),
39+
'created_at' => $this->faker->dateTimeBetween('-31 days'),
4040
'updated_at' => Carbon::now(),
4141
'deleted_at' => null,
4242
];

0 commit comments

Comments
 (0)