-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing a few bugs, cleaning up tests by adding factories, making it m…
…uch easier to expand on additional features and testing.
- Loading branch information
1 parent
18362db
commit 4a4de05
Showing
21 changed files
with
242 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Tests\Acceptance; | ||
|
||
use Tests\Acceptance\Models\Category; | ||
use Tests\Acceptance\Models\Comment; | ||
|
||
class ChainedAggregatesTest extends AcceptanceTestCase | ||
{ | ||
function test_aggregateDependentsAreUpdated() | ||
{ | ||
// Comment will create a user, and a post - the post created will bubble up to category and update it as well. | ||
Comment::factory()->create(); | ||
|
||
$this->assertSame(1, Category::first()->postCount); | ||
$this->assertSame(1, Category::first()->totalComments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,49 @@ | ||
<?php | ||
namespace Tests\Acceptance; | ||
|
||
use Tests\Acceptance\Models\Category; | ||
use Tests\Acceptance\Models\Comment; | ||
use Tests\Acceptance\Models\Post; | ||
use Tests\Acceptance\Models\User; | ||
|
||
class CountCacheTest extends AcceptanceTestCase | ||
{ | ||
private $data = []; | ||
|
||
public function init() | ||
{ | ||
$this->data = $this->setupUserAndPost(); | ||
} | ||
|
||
function test_userHasASinglePostCount() | ||
{ | ||
$user = User::first(); | ||
$user = User::factory()->create(); | ||
|
||
$this->assertEquals(1, $user->postCount); | ||
Post::factory()->for($user)->create(); | ||
|
||
$this->assertEquals(1, $user->fresh()->postCount); | ||
} | ||
|
||
function test_whenRelatedModelsAreSwitchedBothCountCachesAreUpdated() | ||
{ | ||
$post = new Post; | ||
$post->userId = $this->data['user']->id; | ||
$post->save(); | ||
|
||
$comment = new Comment; | ||
$comment->userId = $this->data['user']->id; | ||
$comment->postId = $this->data['post']->id; | ||
$comment->save(); | ||
$user1 = User::factory()->create(); | ||
$user2 = User::factory()->create(); | ||
$posts = Post::factory()->count(2)->for($user1)->create(); | ||
$comment = Comment::factory()->for($user1)->for($posts->first())->create(); | ||
|
||
$this->assertEquals(2, User::first()->postCount); | ||
$this->assertEquals(1, User::first()->commentCount); | ||
$this->assertEquals(1, Post::first()->commentCount); | ||
$this->assertEquals(2, $user1->fresh()->postCount); | ||
$this->assertEquals(1, $user1->fresh()->commentCount); | ||
$this->assertEquals(1, $posts->first()->fresh()->commentCount); | ||
|
||
$comment = $comment->fresh(); | ||
$comment->postId = $post->id; | ||
$comment->userId = $user2->id; | ||
$comment->save(); | ||
|
||
$this->assertEquals(0, $this->data['post']->fresh()->commentCount); | ||
$this->assertEquals(1, $post->fresh()->commentCount); | ||
$this->assertEquals(0, $user1->fresh()->commentCount); | ||
$this->assertEquals(1, $user2->fresh()->commentCount); | ||
} | ||
|
||
public function testItCanHandleNegativeCounts() | ||
public function testItCanHandleModelRestoration() | ||
{ | ||
$post = new Post; | ||
$post->userId = $this->data['user']->id; | ||
$post->save(); | ||
$post = Post::factory()->create(); | ||
|
||
$comment = new Comment; | ||
$comment->userId = $this->data['user']->id; | ||
$comment->postId = $this->data['post']->id; | ||
$comment->save(); | ||
$comment = Comment::factory()->for($post)->create(); | ||
$comment->delete(); | ||
$comment->restore(); | ||
|
||
$this->assertEquals(1, Post::first()->commentCount); | ||
} | ||
|
||
private function setupUserAndPost() | ||
{ | ||
$user = new User; | ||
$user->firstName = 'Kirk'; | ||
$user->lastName = 'Bushell'; | ||
$user->save(); | ||
|
||
$post = new Post; | ||
$post->userId = $user->id; | ||
$post->save(); | ||
|
||
return compact('user', 'post'); | ||
$this->assertEquals(1, $post->fresh()->commentCount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Tests\Acceptance\Models; | ||
|
||
use Eloquence\Behaviours\CamelCased; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Category extends Model | ||
{ | ||
use CamelCased; | ||
use HasFactory; | ||
|
||
protected static function newFactory(): Factory | ||
{ | ||
return CategoryFactory::new(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Tests\Acceptance\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class CategoryFactory extends Factory | ||
{ | ||
protected $model = Category::class; | ||
|
||
public function definition() | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Tests\Acceptance\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class CommentFactory extends Factory | ||
{ | ||
protected $model = Comment::class; | ||
|
||
public function definition() | ||
{ | ||
return [ | ||
'post_id' => Post::factory(), | ||
'user_id' => User::factory(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Tests\Acceptance\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class ItemFactory extends Factory | ||
{ | ||
protected $model = Item::class; | ||
|
||
public function definition() | ||
{ | ||
// TODO: Implement definition() method. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Tests\Acceptance\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class OrderFactory extends Factory | ||
{ | ||
protected $model = Order::class; | ||
|
||
public function definition() | ||
{ | ||
// TODO: Implement definition() method. | ||
} | ||
} |
Oops, something went wrong.