Skip to content

Commit 2f74971

Browse files
Add Tests, fix composer
1 parent 6e54096 commit 2f74971

File tree

5 files changed

+119
-9
lines changed

5 files changed

+119
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
"require": {
1111
"php": ">=7.1.0",
12-
"illuminate/database": "^5.5.0",
12+
"illuminate/database": "^5.5.0"
1313
},
1414
"require-dev": {
1515
"phpunit/phpunit": "^7.0.0",

tests/DummyModel.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace LiamWiltshire\LaravelJitLoader\Tests;
4+
5+
6+
use LiamWiltshire\LaravelJitLoader\Model;
7+
8+
class DummyModel extends Model {
9+
10+
protected $fillable = ['dummy_model_id'];
11+
12+
public function myRelationship()
13+
{
14+
return $this->hasOne(DummyModel::class);
15+
}
16+
17+
public function nonExistentRelationship()
18+
{
19+
return false;
20+
}
21+
22+
public function setAutoloadThreshold(int $autoloadThreshold)
23+
{
24+
$this->autoloadThreshold = $autoloadThreshold;
25+
}
26+
}

tests/Model.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/ModelTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace LiamWiltshire\LaravelJitLoader\Tests;
4+
5+
6+
class ModelTest extends TestCase
7+
{
8+
public function testGetRelationshipFromMethodWithNonExistentRelationshipThrowsException()
9+
{
10+
$model = new DummyModel();
11+
$this->expectException(\LogicException::class);
12+
$this->expectExceptionMessage('DummyModel::nonExistentRelationship must return a relationship instance.');
13+
$model->getRelationshipFromMethod('nonExistentRelationship');
14+
}
15+
16+
public function testGetRelationshipFromMethodAfterDisableAutoLoadCalledDoesntAutoLoad()
17+
{
18+
$models = DummyModel::all();
19+
$related = $models[0]->disableAutoload()->myRelationship;
20+
21+
$this->assertFalse($models[1]->relationLoaded('myRelationship'));
22+
}
23+
24+
public function testGetRelationshipFromMethodOverThresholdDoesntAutoLoad()
25+
{
26+
$models = DummyModel::all();
27+
$models[0]->setAutoloadThreshold(2);
28+
$related = $models[0]->myRelationship;
29+
30+
$this->assertFalse($models[1]->relationLoaded('myRelationship'));
31+
}
32+
33+
34+
public function testGetRelationshipFromMethodUnderThresholdDoesAutoLoad()
35+
{
36+
$models = DummyModel::all();
37+
$related = $models[0]->myRelationship;
38+
39+
$this->assertTrue($models[1]->relationLoaded('myRelationship'));
40+
}
41+
}
42+
43+

tests/TestCase.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: liam
5+
* Date: 24/02/19
6+
* Time: 18:17
7+
*/
8+
9+
namespace LiamWiltshire\LaravelJitLoader\Tests;
10+
11+
12+
use Illuminate\Database\Capsule\Manager;
13+
14+
class TestCase extends \PHPUnit\Framework\TestCase
15+
{
16+
public function setUp()
17+
{
18+
$this->configureDatabase();
19+
$this->migrateIdentitiesTable();
20+
}
21+
protected function configureDatabase()
22+
{
23+
$db = new Manager();
24+
$db->addConnection(array(
25+
'driver' => 'sqlite',
26+
'database' => ':memory:',
27+
'charset' => 'utf8',
28+
'collation' => 'utf8_unicode_ci',
29+
'prefix' => '',
30+
));
31+
$db->bootEloquent();
32+
$db->setAsGlobal();
33+
}
34+
35+
public function migrateIdentitiesTable()
36+
{
37+
Manager::schema()->create('dummy_models', function($table) {
38+
$table->increments('id');
39+
$table->integer('dummy_model_id');
40+
$table->timestamps();
41+
});
42+
DummyModel::create(array('dummy_model_id' => 5));
43+
DummyModel::create(array('dummy_model_id' => 4));
44+
DummyModel::create(array('dummy_model_id' => 3));
45+
DummyModel::create(array('dummy_model_id' => 2));
46+
DummyModel::create(array('dummy_model_id' => 1));
47+
48+
}
49+
}

0 commit comments

Comments
 (0)