Skip to content
This repository has been archived by the owner on Oct 20, 2020. It is now read-only.

Commit

Permalink
Casts array is now supported. Tests with database.
Browse files Browse the repository at this point in the history
  • Loading branch information
biscofil committed Oct 15, 2019
1 parent 164b431 commit 8e2e4b9
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 28 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"model"
],
"require": {
"laravel/framework": ">=5.4"
"illuminate/database": "~5.6.0|~5.7.0|~5.8.0|^6.0.0"
},
"require-dev": {
"phpunit/phpunit": "^8.3",
"orchestra/testbench": "^4.1"
"orchestra/testbench": "3.8.*",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
Expand All @@ -31,7 +31,7 @@
},
"autoload-dev": {
"psr-4": {
"Biscofil\\LaravelSubmodels\\Test\\": "tests/"
"Biscofil\\LaravelSubmodels\\Tests\\": "tests/"
}
}
}
6 changes: 3 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory>tests</directory>
<testsuite name="Unit Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
Expand Down
12 changes: 10 additions & 2 deletions src/HasAppendedFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ trait HasAppendedFields

/**
* Get the fillable attributes for the model.
*
* @return array
*/
public function getFillable()
Expand All @@ -18,7 +17,6 @@ public function getFillable()

/**
* Get the attributes for the model.
*
* @return array
*/
public function getAttributes()
Expand All @@ -27,5 +25,15 @@ public function getAttributes()
return array_unique(array_merge($appendedAttributes, parent::getAttributes()));
}

/**
* Get the casted attributes for the model.
* @return array
*/
public function getCasts()
{
$appendedCasts = property_exists($this, 'appendedCasts') ? $this->appendedCasts : [];
return array_unique(array_merge($appendedCasts, parent::getCasts()));
}


}
6 changes: 5 additions & 1 deletion src/HasSubModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public function newFromBuilder($attributes = [], $connection = null)
}

if (!is_null($class)) {
$model = with(new $class)->newFromBuilder($attributes);

if ($class != get_class($this)) {
$model = with(new $class)->newFromBuilder($attributes);
}

} else {
$model->setConnection($connection ?: $this->getConnectionName());
$model->fireModelEvent('retrieved', false);
Expand Down
51 changes: 50 additions & 1 deletion tests/DemoTest.php → tests/Features/DemoTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php

namespace Biscofil\LaravelSubmodels\Test;
namespace Biscofil\LaravelSubmodels\Tests\Features;

use Biscofil\LaravelSubmodels\Tests\Models\AdminUser;
use Biscofil\LaravelSubmodels\Tests\Models\BaseUser;
use Biscofil\LaravelSubmodels\Tests\Models\CustomerUser;
use Biscofil\LaravelSubmodels\Tests\Models\NestedCustomerUser;
use Biscofil\LaravelSubmodels\Tests\TestCase;

class DemoTest extends TestCase
{
Expand Down Expand Up @@ -55,4 +61,47 @@ public function testNestedCustomer()

$this->assertEqualsCanonicalizing($expected, $actual);
}

public function testCreateAdmin()
{

/** @var AdminUser $admin */
$admin = factory(AdminUser::class)->create();

$adminFetched = BaseUser::find($admin->id);

$this->assertEquals(get_class($adminFetched), AdminUser::class);

}

public function testCreateCustomer()
{

/** @var CustomerUser $admin */
$customer = factory(CustomerUser::class)->create();

/** @var NestedCustomerUser $customerFetched */
$customerFetched = BaseUser::find($customer->id);

$this->assertEquals(get_class($customerFetched), CustomerUser::class);

$this->assertFalse($customerFetched->isNestedCustomer());
}

public function testCreateNestedCustomer()
{

/** @var CustomerUser $admin */
$customer = factory(NestedCustomerUser::class)->create();

/** @var NestedCustomerUser $customerFetched */
$customerFetched = BaseUser::find($customer->id);

$this->assertEquals(get_class($customerFetched), NestedCustomerUser::class);

$this->assertTrue($customerFetched->isNestedCustomer());

}


}
7 changes: 6 additions & 1 deletion tests/AdminUser.php → tests/Models/AdminUser.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?php

namespace Biscofil\LaravelSubmodels\Test;
namespace Biscofil\LaravelSubmodels\Tests\Models;

use Biscofil\LaravelSubmodels\HasAppendedFields;

/**
* @property mixed admin_name
*/
class AdminUser extends BaseUser
{

use HasAppendedFields;

protected $table = 'users';

private $appendedFillable = [
'admin_name'
];
Expand Down
16 changes: 5 additions & 11 deletions tests/BaseUser.php → tests/Models/BaseUser.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

namespace Biscofil\LaravelSubmodels\Test;

namespace Biscofil\LaravelSubmodels\Tests\Models;

use Biscofil\LaravelSubmodels\HasSubModels;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -13,6 +12,10 @@ class BaseUser extends Model
{
use HasSubModels;

public $timestamps = false;

protected $table = 'users';

protected $fillable = [
'is_admin'
];
Expand Down Expand Up @@ -40,13 +43,4 @@ public function isAdmin()
return $this->is_admin;
}

public function operation(){
return "base";
}


public function getData()
{
return array_merge(['base'], []);
}
}
11 changes: 9 additions & 2 deletions tests/CustomerUser.php → tests/Models/CustomerUser.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace Biscofil\LaravelSubmodels\Test;
namespace Biscofil\LaravelSubmodels\Tests\Models;

use Biscofil\LaravelSubmodels\HasSubModels;
use Biscofil\LaravelSubmodels\HasAppendedFields;
use Biscofil\LaravelSubmodels\HasSubModels;

/**
* @property mixed customer_name
* @property bool is_nested_customer
*/
class CustomerUser extends BaseUser
Expand All @@ -14,11 +15,17 @@ class CustomerUser extends BaseUser
use HasAppendedFields;
use HasSubModels;

protected $table = 'users';

private $appendedFillable = [
'customer_name',
'is_nested_customer'
];

private $appendedCasts = [
'is_nested_customer' => 'bool'
];

public function isNestedCustomer()
{
return $this->is_nested_customer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php

namespace Biscofil\LaravelSubmodels\Test;

namespace Biscofil\LaravelSubmodels\Tests\Models;

use Biscofil\LaravelSubmodels\HasAppendedFields;

/**
* Class NestedCustomerUser
* @property mixed nested_customer_name
* @package Biscofil\LaravelSubmodels\Tests\Models
*/
class NestedCustomerUser extends CustomerUser
{

use HasAppendedFields;

protected $table = 'users';

private $appendedFillable = [
'nested_customer_name'
];
Expand Down
45 changes: 44 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
<?php


namespace Biscofil\LaravelSubmodels\Test;
namespace Biscofil\LaravelSubmodels\Tests;

use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

class TestCase extends OrchestraTestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
$this->runMigrations();
$this->withFactories(__DIR__ . '/factories');
}

/**
* @param Application $app
*/
protected function getEnvironmentSetUp($app)
{
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

/**
*
*/
public function runMigrations()
{
Schema::create('users', function ($table) {
$table->increments('id');

$table->boolean('is_admin')->default(false);
$table->string('admin_name')->nullable();

$table->string('customer_name')->nullable();

$table->boolean('is_nested_customer')->default(false);;
$table->string('nested_customer_name')->nullable();
});

}

}
10 changes: 10 additions & 0 deletions tests/factories/AdminUserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Biscofil\LaravelSubmodels\Tests\Models\AdminUser;

$factory->define(AdminUser::class, function () {
return [
'is_admin' => true,
'admin_name' => 'foo',
];
});
12 changes: 12 additions & 0 deletions tests/factories/CustomerUserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


use Biscofil\LaravelSubmodels\Tests\Models\CustomerUser;

$factory->define(CustomerUser::class, function () {
return [
'is_admin' => false,
'customer_name' => 'demo',
'is_nested_customer' => false,
];
});
13 changes: 13 additions & 0 deletions tests/factories/NestedCustomerUserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


use Biscofil\LaravelSubmodels\Tests\Models\NestedCustomerUser;

$factory->define(NestedCustomerUser::class, function () {
return [
'is_admin' => false,
'customer_name' => 'demo nested',
'is_nested_customer' => true,
'nested_customer_name' => 'foo',
];
});

0 comments on commit 8e2e4b9

Please sign in to comment.