-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
170 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use Illuminate\Database\Capsule\Manager as DB; | ||
use Conner\Likeable\LikeableTrait; | ||
|
||
class CommonUseTest extends TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
Eloquent::unguard(); | ||
|
||
$this->artisan('migrate', [ | ||
'--database' => 'testbench', | ||
'--realpath' => realpath(__DIR__.'/../migrations'), | ||
]); | ||
} | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('database.default', 'testbench'); | ||
$app['config']->set('database.connections.testbench', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
|
||
\Schema::create('books', function ($table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
\Schema::drop('books'); | ||
} | ||
|
||
public function test_basic_like() | ||
{ | ||
$stub = Stub::create(['name'=>123]); | ||
|
||
$stub->like(); | ||
|
||
$this->assertEquals(1, $stub->likeCount); | ||
} | ||
|
||
public function test_multiple_likes() | ||
{ | ||
$stub = Stub::create(['name'=>123]); | ||
|
||
$stub->like(1); | ||
$stub->like(2); | ||
$stub->like(3); | ||
$stub->like(4); | ||
|
||
$this->assertEquals(4, $stub->likeCount); | ||
} | ||
|
||
public function test_unike() | ||
{ | ||
$stub = Stub::create(['name'=>123]); | ||
|
||
$stub->unlike(1); | ||
|
||
$this->assertEquals(0, $stub->likeCount); | ||
} | ||
|
||
public function test_where_liked_by() | ||
{ | ||
Stub::create(['name'=>'A'])->like(1); | ||
Stub::create(['name'=>'B'])->like(1); | ||
Stub::create(['name'=>'C'])->like(1); | ||
|
||
$stubs = Stub::whereLikedBy(1)->get(); | ||
$shouldBeEmpty = Stub::whereLikedBy(2)->get(); | ||
|
||
$this->assertEquals(3, $stubs->count()); | ||
$this->assertEmpty($shouldBeEmpty); | ||
} | ||
} | ||
|
||
class Stub extends Eloquent | ||
{ | ||
use LikeableTrait; | ||
|
||
protected $connection = 'testbench'; | ||
|
||
public $table = 'books'; | ||
} |
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,44 @@ | ||
<?php | ||
|
||
use Conner\Likeable\LikeableServiceProvider; | ||
|
||
abstract class TestCase extends Orchestra\Testbench\TestCase | ||
{ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [LikeableServiceProvider::class]; | ||
} | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* Assert that two arrays are equal. This helper method will sort the two arrays before comparing them if | ||
* necessary. This only works for one-dimensional arrays, if you need multi-dimension support, you will | ||
* have to iterate through the dimensions yourself. | ||
* @param array $expected the expected array | ||
* @param array $actual the actual array | ||
* @param bool $regard_order whether or not array elements may appear in any order, default is false | ||
* @param bool $check_keys whether or not to check the keys in an associative array | ||
*/ | ||
protected function assertArraysEqual(array $expected, array $actual, $regard_order = false, $check_keys = false) { | ||
// check length first | ||
$this->assertEquals(count($expected), count($actual), 'Failed to assert that two arrays have the same length.'); | ||
|
||
// sort arrays if order is irrelevant | ||
if (!$regard_order) { | ||
if ($check_keys) { | ||
$this->assertTrue(ksort($expected), 'Failed to sort array.'); | ||
$this->assertTrue(ksort($actual), 'Failed to sort array.'); | ||
} else { | ||
$this->assertTrue(sort($expected), 'Failed to sort array.'); | ||
$this->assertTrue(sort($actual), 'Failed to sort array.'); | ||
} | ||
} | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
} |