Skip to content

Commit 4ae7157

Browse files
committed
update
1 parent 63caeab commit 4ae7157

File tree

12 files changed

+259
-1
lines changed

12 files changed

+259
-1
lines changed

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"barryvdh/reflection-docblock": "^2.1",
1616
"composer/class-map-generator": "^1.1"
1717
},
18+
"require-dev": {
19+
"orchestra/testbench": "^8.13",
20+
"nunomaduro/collision": "^7.8"
21+
},
1822
"autoload": {
1923
"psr-4": {
2024
"BoredProgrammers\\LaravelEloquentMappable\\": "src/"
@@ -26,5 +30,26 @@
2630
"BoredProgrammers\\LaravelEloquentMappable\\Providers\\LaravelEloquentMappableServiceProvider"
2731
]
2832
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"BoredProgrammers\\LaravelEloquentMappable\\Tests\\": "tests",
37+
"Workbench\\App\\": "workbench/app/",
38+
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
39+
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
40+
}
41+
},
42+
"scripts": {
43+
"post-autoload-dump": [
44+
"@clear",
45+
"@prepare"
46+
],
47+
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
48+
"prepare": "@php vendor/bin/testbench package:discover --ansi",
49+
"build": "@php vendor/bin/testbench workbench:build --ansi",
50+
"serve": [
51+
"@build",
52+
"@php vendor/bin/testbench serve"
53+
]
2954
}
3055
}

src/Models/Traits/HasMappableColumns.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected static function bootHasMappableColumns(): void
3535
public function newInstance($attributes = [], $exists = false): Model
3636
{
3737
/** @var Model $instance */
38-
$instance = parent::newInstance($attributes, $exists);
38+
$instance = parent::newInstance(exists: $exists);
3939

4040
foreach ($instance->mapAttributeToColumn ?? [] as $attribute => $column) {
4141
$instance->hidden[] = $column;
@@ -50,6 +50,8 @@ public function newInstance($attributes = [], $exists = false): Model
5050
}
5151
}
5252

53+
$instance->fill((array)$attributes);
54+
5355
return $instance;
5456
}
5557

testbench.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
providers: []
2+
3+
migrations:
4+
- workbench/database/migrations
5+
6+
seeders:
7+
- Workbench\Database\Seeders\DatabaseSeeder
8+
9+
workbench:
10+
start: '/'
11+
install: true
12+
welcome: true
13+
discovers:
14+
web: false
15+
api: false
16+
commands: false
17+
build: []
18+
assets: []
19+
sync: []

tests/TestCase.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BoredProgrammers\LaravelEloquentMappable\Tests;
4+
5+
use Closure;
6+
use Orchestra\Testbench\TestCase as OrchestraTestCase;
7+
8+
class TestCase extends OrchestraTestCase
9+
{
10+
11+
public function setUp(): void
12+
{
13+
parent::setUp();
14+
15+
$this->loadMigrationsFrom(__DIR__ . '/../workbench/database/migrations');
16+
$this->artisan('migrate', ['--database' => 'laravel_eloquent_mappable'])->run();
17+
}
18+
19+
protected function setUpDatabaseRequirements(Closure $callback): void
20+
{
21+
}
22+
23+
24+
protected function getEnvironmentSetUp($app): void
25+
{
26+
$app['config']->set('database.default', 'laravel_eloquent_mappable');
27+
$app['config']->set('database.connections.laravel_eloquent_mappable', [
28+
'driver' => 'sqlite',
29+
'database' => ':memory:',
30+
'prefix' => '',
31+
]);
32+
}
33+
34+
}

tests/Unit/EloquentTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BoredProgrammers\LaravelEloquentMappable\Tests\Unit;
4+
5+
use BoredProgrammers\LaravelEloquentMappable\Tests\TestCase;
6+
use Workbench\App\Models\Post;
7+
8+
class EloquentTest extends TestCase
9+
{
10+
11+
public function test_create()
12+
{
13+
$post = Post::create([
14+
'mapped_title' => 'testing title',
15+
'content' => 'testing content',
16+
]);
17+
18+
$this->assertEquals('testing title', $post->mapped_title);
19+
$this->assertEquals('testing title', $post->title);
20+
}
21+
22+
public function test_delete()
23+
{
24+
Post::create([
25+
'mapped_title' => 'testing title2',
26+
'content' => 'testing content2',
27+
]);
28+
Post::create([
29+
'mapped_title' => 'testing title3',
30+
'content' => 'testing content3',
31+
]);
32+
33+
$this->assertEquals(2, Post::count());
34+
35+
Post::whereMappedTitle('testing title2')->delete();
36+
37+
$this->assertEquals(1, Post::count());
38+
}
39+
40+
}

tests/Unit/QueryBuilderTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BoredProgrammers\LaravelEloquentMappable\Tests\Unit;
4+
5+
use BoredProgrammers\LaravelEloquentMappable\Tests\TestCase;
6+
use Illuminate\Database\Query\JoinClause;
7+
use Workbench\App\Models\Post;
8+
9+
class QueryBuilderTest extends TestCase
10+
{
11+
12+
public function test_where()
13+
{
14+
$this->assertEquals(
15+
"select * from \"posts\" where \"title\" = 'asd'",
16+
Post::whereMappedTitle('asd')->toRawSql()
17+
);
18+
19+
$this->assertEquals(
20+
"select * from \"posts\" where \"title\" = 'asd'",
21+
Post::where('mapped_title', 'asd')
22+
->toRawSql()
23+
);
24+
25+
$this->assertEquals(
26+
"select * from \"posts\" where \"title\" != 'asd'",
27+
Post::where('mapped_title', '!=', 'asd')
28+
->toRawSql()
29+
);
30+
}
31+
32+
public function test_or_where()
33+
{
34+
$this->assertEquals(
35+
"select * from \"posts\" where \"title\" = 'asd' or \"title\" like '%testing%'",
36+
Post::where('mapped_title', 'asd')
37+
->orWhere('mapped_title', 'like', '%testing%')
38+
->toRawSql()
39+
);
40+
}
41+
42+
public function test_where_not_null()
43+
{
44+
$this->assertEquals(
45+
"select * from \"posts\" where \"title\" is not null",
46+
Post::whereNotNull('mapped_title')
47+
->toRawSql()
48+
);
49+
}
50+
51+
public function test_join()
52+
{
53+
$this->assertEquals(
54+
"select * from \"posts\""
55+
. " inner join \"user_accounts\" on \"user_accounts\".\"id\" = \"posts\".\"title\""
56+
. " where \"title\" is not null",
57+
Post::whereNotNull('mapped_title')
58+
->join('user_accounts', 'user_accounts.id', '=', 'posts.mapped_title')
59+
->toRawSql()
60+
);
61+
62+
$this->assertEquals(
63+
"select * from \"posts\" inner join (\"user_accounts\" as \"ua1\""
64+
. " inner join \"user_accounts\" as \"ua2\" on \"ua2\".\"id\" = \"posts\".\"title\")"
65+
. " where \"title\" is not null",
66+
Post::whereNotNull('mapped_title')
67+
->join('user_accounts as ua1', function (JoinClause $join) {
68+
$join->join('user_accounts as ua2', 'ua2.id', '=', 'posts.mapped_title');
69+
})
70+
->toRawSql()
71+
);
72+
}
73+
74+
}

workbench/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
.env.dusk

workbench/app/Models/Post.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Workbench\App\Models;
4+
5+
use BoredProgrammers\LaravelEloquentMappable\Models\Traits\HasMappableColumns;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Post extends Model
9+
{
10+
11+
use HasMappableColumns;
12+
13+
protected $fillable = [
14+
'mapped_title',
15+
'content',
16+
];
17+
18+
public $mapAttributeToColumn = [
19+
'mapped_title' => 'title'
20+
];
21+
}

workbench/database/factories/.gitkeep

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
9+
public function up(): void
10+
{
11+
Schema::create('posts', function (Blueprint $table) {
12+
$table->id();
13+
$table->timestamps();
14+
$table->string('title');
15+
$table->text('content');
16+
});
17+
}
18+
19+
public function down(): void
20+
{
21+
Schema::dropIfExists('posts');
22+
}
23+
24+
};

0 commit comments

Comments
 (0)