Skip to content

Commit

Permalink
fix(pest): update
Browse files Browse the repository at this point in the history
  • Loading branch information
陆云峰 committed Sep 1, 2022
1 parent 8f16572 commit f855bc6
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 38 deletions.
5 changes: 5 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ Image::hasMorphIn('imageable', [Post::class, Comment::class])->get();
User::hasIn('posts.comments')->get();
```

## 测试
```bash
composer test
```

## 联系交流
wx:biiiiiigmonster(备注:hasin)

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,10 @@ Image::hasMorphIn('imageable', [Post::class, Comment::class])->get();
User::hasIn('posts.comments')->get();
```

# Testing
```bash
composer test
```

# License
[MIT](./LICENSE)
11 changes: 11 additions & 0 deletions tests/Feature/DoesntHaveMorphInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use BiiiiiigMonster\Hasin\Tests\Models\Comment;
use BiiiiiigMonster\Hasin\Tests\Models\Post;

test('doesntHaveMorphIn same as doesntHaveMorph', function () {
$doesntHaveMorph = Comment::doesntHaveMorph('commentable', [Post::class])->orderBy('id')->pluck('id');
$doesntHaveMorphIn = Comment::doesntHaveMorphIn('commentable', [Post::class])->orderBy('id')->pluck('id');

expect($doesntHaveMorph)->toEqual($doesntHaveMorphIn);
});
18 changes: 18 additions & 0 deletions tests/Feature/HasMorphInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use BiiiiiigMonster\Hasin\Tests\Models\Comment;
use BiiiiiigMonster\Hasin\Tests\Models\Post;

test('hasMorphIn same as hasMorph', function () {
$hasMorph = Comment::hasMorph('commentable', [Post::class])->orderBy('id')->pluck('id');
$hasMorphIn = Comment::hasMorphIn('commentable', [Post::class])->orderBy('id')->pluck('id');

expect($hasMorph)->toEqual($hasMorphIn);
});

test('hasMorphIn(gte 2) same as hasMorph(gte 2)', function () {
$hasMorph = Comment::hasMorph('commentable', [Post::class], '>=', 2)->orderBy('id')->pluck('id');
$hasMorphIn = Comment::hasMorphIn('commentable', [Post::class], '>=', 2)->orderBy('id')->pluck('id');

expect($hasMorph)->toEqual($hasMorphIn);
});
11 changes: 11 additions & 0 deletions tests/Feature/OrDoesntHaveMorphInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use BiiiiiigMonster\Hasin\Tests\Models\Comment;
use BiiiiiigMonster\Hasin\Tests\Models\Post;

test('orDoesntHaveMorphIn same as orDoesntHaveMorph', function () {
$orDoesntHaveMorph = Comment::where('status', '>', 2)->orDoesntHaveMorph('commentable', [Post::class])->orderBy('id')->pluck('id');
$orDoesntHaveMorphIn = Comment::where('status', '>', 2)->orDoesntHaveMorphIn('commentable', [Post::class])->orderBy('id')->pluck('id');

expect($orDoesntHaveMorph)->toEqual($orDoesntHaveMorphIn);
});
18 changes: 18 additions & 0 deletions tests/Feature/OrHasMorphInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use BiiiiiigMonster\Hasin\Tests\Models\Comment;
use BiiiiiigMonster\Hasin\Tests\Models\Post;

test('orHasMorphIn same as orHasMorph', function () {
$orHasMorph = Comment::where('status', '>', 2)->orHasMorph('commentable', [Post::class])->orderBy('id')->pluck('id');
$orHasMorphIn = Comment::where('status', '>', 2)->orHasMorphIn('commentable', [Post::class])->orderBy('id')->pluck('id');

expect($orHasMorph)->toEqual($orHasMorphIn);
});

test('orHasMorphIn(gte 2) same as orHasMorph(gte 2)', function () {
$orHasMorph = Comment::where('status', '>', 2)->orHasMorph('commentable', [Post::class], '>=', 2)->orderBy('id')->pluck('id');
$orHasMorphIn = Comment::where('status', '>', 2)->orHasMorphIn('commentable', [Post::class], '>=', 2)->orderBy('id')->pluck('id');

expect($orHasMorph)->toEqual($orHasMorphIn);
});
12 changes: 4 additions & 8 deletions tests/Feature/OrWhereDoesntHaveInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
});

test('nested orWhereDoesntHaveIn same as nested orWhereDoesntHave', function () {
$orWhereDoesntHave = User::where('age', '>', 18)->orWhereDoesntHave('posts', function ($query) {
$query->where('votes', '>', 20)->orWhereDoesntHave('comments', function ($nestQuery) {
$nestQuery->where('status', '=', 2);
});
$orWhereDoesntHave = User::where('age', '>', 18)->orWhereDoesntHave('posts.comments', function ($query) {
$query->where('status', '>', 2);
})->orderBy('id')->pluck('id');
$orWhereDoesntHaveIn = User::where('age', '>', 18)->orWhereDoesntHaveIn('posts', function ($query) {
$query->where('votes', '>', 20)->orWhereDoesntHaveIn('comments', function ($nestQuery) {
$nestQuery->where('status', '=', 2);
});
$orWhereDoesntHaveIn = User::where('age', '>', 18)->orWhereDoesntHaveIn('posts.comments', function ($query) {
$query->where('status', '>', 2);
})->orderBy('id')->pluck('id');

expect($orWhereDoesntHave)->toEqual($orWhereDoesntHaveIn);
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/OrWhereDoesntHaveMorphInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use BiiiiiigMonster\Hasin\Tests\Models\Comment;
use BiiiiiigMonster\Hasin\Tests\Models\Post;

test('orWhereDoesntHaveMorphIn same as orWhereDoesntHaveMorph', function () {
$orWhereDoesntHaveMorph = Comment::where('status', '>', 2)->orWhereDoesntHaveMorph('commentable', [Post::class], function ($query) {
$query->where('title', 'like', '%code%');
})->orderBy('id')->pluck('id');
$orWhereDoesntHaveMorphIn = Comment::where('status', '>', 2)->orWhereDoesntHaveMorphIn('commentable', [Post::class], function ($query) {
$query->where('title', 'like', '%code%');
})->orderBy('id')->pluck('id');

expect($orWhereDoesntHaveMorph)->toEqual($orWhereDoesntHaveMorphIn);
});
12 changes: 4 additions & 8 deletions tests/Feature/OrWhereHasInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
});

test('nested orWhereHasIn same as nested orWhereHas', function () {
$orWhereHas = User::where('age', '>', 18)->orWhereHas('posts', function ($query) {
$query->where('votes', '>', 20)->orWhereHas('comments', function ($nestQuery) {
$nestQuery->where('status', '=', 2);
});
$orWhereHas = User::where('age', '>', 18)->orWhereHas('posts.comments', function ($query) {
$query->where('status', '>', 2);
})->orderBy('id')->pluck('id');
$orWhereHasIn = User::where('age', '>', 18)->orWhereHasIn('posts', function ($query) {
$query->where('votes', '>', 20)->orWhereHasIn('comments', function ($nestQuery) {
$nestQuery->where('status', '=', 2);
});
$orWhereHasIn = User::where('age', '>', 18)->orWhereHasIn('posts.comments', function ($query) {
$query->where('status', '>', 2);
})->orderBy('id')->pluck('id');

expect($orWhereHas)->toEqual($orWhereHasIn);
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/OrWhereHasMorphInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use BiiiiiigMonster\Hasin\Tests\Models\Comment;
use BiiiiiigMonster\Hasin\Tests\Models\Post;

test('orWhereHasMorphIn same as orWhereHasMorph', function () {
$orWhereHasMorph = Comment::where('status', '>', 2)->orWhereHasMorph('commentable', [Post::class], function ($query) {
$query->where('title', 'like', '%code%');
})->orderBy('id')->pluck('id');
$orWhereHasMorphIn = Comment::where('status', '>', 2)->orWhereHasMorphIn('commentable', [Post::class], function ($query) {
$query->where('title', 'like', '%code%');
})->orderBy('id')->pluck('id');

expect($orWhereHasMorph)->toEqual($orWhereHasMorphIn);
});
13 changes: 5 additions & 8 deletions tests/Feature/WhereDoesntHaveInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@
});

test('nested whereDoesntHaveIn same as nested whereDoesntHave', function () {
$whereDoesntHave = User::whereDoesntHave('posts', function ($query) {
$query->where('votes', '>', 20)->whereDoesntHave('comments', function ($nestQuery) {
$nestQuery->where('status', '=', 2);
});
$whereDoesntHave = User::whereDoesntHave('posts.comments', function ($query) {
$query->where('status', '>', 2);
;
})->orderBy('id')->pluck('id');
$whereDoesntHaveIn = User::whereDoesntHaveIn('posts', function ($query) {
$query->where('votes', '>', 20)->whereDoesntHaveIn('comments', function ($nestQuery) {
$nestQuery->where('status', '=', 2);
});
$whereDoesntHaveIn = User::whereDoesntHaveIn('posts.comments', function ($query) {
$query->where('status', '>', 2);
})->orderBy('id')->pluck('id');

expect($whereDoesntHave)->toEqual($whereDoesntHaveIn);
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/WhereDoesntHaveMorphInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use BiiiiiigMonster\Hasin\Tests\Models\Comment;
use BiiiiiigMonster\Hasin\Tests\Models\Post;

test('whereDoesntHaveMorphIn same as whereDoesntHaveMorph', function () {
$whereDoesntHaveMorph = Comment::whereDoesntHaveMorph('commentable', [Post::class], function ($query) {
$query->where('title', 'like', '%code%');
})->orderBy('id')->pluck('id');
$whereDoesntHaveMorphIn = Comment::whereDoesntHaveMorphIn('commentable', [Post::class], function ($query) {
$query->where('title', 'like', '%code%');
})->orderBy('id')->pluck('id');

expect($whereDoesntHaveMorph)->toEqual($whereDoesntHaveMorphIn);
});
12 changes: 4 additions & 8 deletions tests/Feature/WhereHasInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
});

test('nested whereHasIn same as nested whereHas', function () {
$whereHas = User::whereHas('posts', function ($query) {
$query->where('votes', '>', 20)->whereHas('comments', function ($nestQuery) {
$nestQuery->where('status', '=', 2);
});
$whereHas = User::whereHas('posts.comments', function ($query) {
$query->where('status', '>', 2);
})->orderBy('id')->pluck('id');
$whereHasIn = User::whereHasIn('posts', function ($query) {
$query->where('votes', '>', 20)->whereHasIn('comments', function ($nestQuery) {
$nestQuery->where('status', '=', 2);
});
$whereHasIn = User::whereHasIn('posts.comments', function ($query) {
$query->where('status', '>', 2);
})->orderBy('id')->pluck('id');

expect($whereHas)->toEqual($whereHasIn);
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/WhereHasMorphInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use BiiiiiigMonster\Hasin\Tests\Models\Comment;
use BiiiiiigMonster\Hasin\Tests\Models\Post;

test('whereHasMorphIn same as whereHasMorph', function () {
$whereHasMorph = Comment::whereHasMorph('commentable', [Post::class], function ($query) {
$query->where('title', 'like', '%code%');
})->orderBy('id')->pluck('id');
$whereHasMorphIn = Comment::whereHasMorphIn('commentable', [Post::class], function ($query) {
$query->where('title', 'like', '%code%');
})->orderBy('id')->pluck('id');

expect($whereHasMorph)->toEqual($whereHasMorphIn);
});
6 changes: 6 additions & 0 deletions tests/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Comment extends Model
{
use HasFactory;

public function commentable(): MorphTo
{
return $this->morphTo();
}
}
6 changes: 6 additions & 0 deletions tests/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Image extends Model
{
use HasFactory;

public function imageable(): MorphTo
{
return $this->morphTo();
}
}
18 changes: 12 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ protected function getPackageProviders($app)

protected function defineDatabaseMigrations()
{
$this->migration->down();
$this->migration->up();
}

protected function destroyDatabaseMigrations()
{
// $this->migration->down();
$this->migration->down();
}

protected function defineDatabaseSeeders()
Expand All @@ -57,14 +56,21 @@ protected function defineDatabaseSeeders()
->sequence(fn () => ['supplier_id' => $suppliers->pluck('id')->random()])
->create();

Post::factory(15)
$posts = Post::factory(15)
->sequence(fn () => ['user_id' => $users->pluck('id')->random()])
->has(Comment::factory(3))
->has(Image::factory(2))
->hasAttached($tags->random(15))
->create();

Video::factory(15)->hasAttached($tags->random(15))->create();
$videos = Video::factory(15)->hasAttached($tags->random(15))->create();

$posts->random(5)->map(function ($post) {
Comment::factory(3)->for($post, 'commentable')->create();
Image::factory(2)->for($post, 'imageable')->create();
});

$videos->random(5)->map(function ($video) {
Comment::factory(3)->for($video, 'commentable')->create();
});
}

public function getEnvironmentSetUp($app)
Expand Down

0 comments on commit f855bc6

Please sign in to comment.