Last active
March 7, 2016 20:35
-
-
Save michaelachrisco/fe62542809b628d93ec2 to your computer and use it in GitHub Desktop.
TestCase with example (basic) relation testing built in.
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
<?php | |
//Set up Testing env | |
use Illuminate\Foundation\Testing\WithoutMiddleware; | |
// use Illuminate\Foundation\Testing\DatabaseMigrations; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase | |
{ | |
/** | |
* The base URL to use while testing the application. | |
* | |
* @var string | |
*/ | |
protected $baseUrl = 'http://localhost'; | |
/** | |
* Creates the application. | |
* | |
* @return \Illuminate\Foundation\Application | |
*/ | |
public function createApplication() | |
{ | |
$app = require __DIR__.'/../bootstrap/app.php'; | |
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); | |
return $app; | |
} | |
public function assertArrayHasKeys(array $keys, array $array, $message = null) | |
{ | |
foreach ($keys as $key) { | |
$this->assertArrayHasKey($key, $array, $message); | |
} | |
} | |
public function assertBelongsTo($relation, $relationName){ | |
$this->assertEquals('Illuminate\Database\Eloquent\Relations\BelongsTo', get_class($relation)); | |
$this->assertEquals($relationName, get_class($relation->getRelated())); | |
} | |
public function assertHasMany($relation, $relationName){ | |
$this->assertEquals('Illuminate\Database\Eloquent\Relations\HasMany', get_class($relation)); | |
$this->assertEquals($relationName, get_class($relation->getRelated())); | |
} | |
} |
To start out:
Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Eligibility extends Model
{
public function Plan(){
return $this->belongsTo('App\Plan');
}
}
Test:
<?php
class EligibilityTest extends TestCase
{
public function testRelations(){
$elig = factory(App\Eligibility::class)->make();
$this->assertBelongsTo($elig->Plan(), 'App\Plan')
}
}
?>
This is a compactly shallow test and really should include the models reflexive properties as well as index through operations. This may change depending on how long Laravel sticks around.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I continue to use PHP/Laravel, something like https://github.com/thoughtbot/shoulda for laravel is absolutely necessary.
If not, this is a good starting point for someone else.
Used within PHPunit.