Skip to content

Commit a3d3844

Browse files
authored
Merge pull request #25 from cesargb/postgres
Add support to PostgreSQL
2 parents 3fd8bbb + 50a8547 commit a3d3844

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

phpunit.postgres.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="League Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
<php>
30+
<env name="DB_DRIVER" value="pgsql" force="true"/>
31+
</php>
32+
</phpunit>

src/MagicLink.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ protected static function boot()
3131

3232
public function getActionAttribute($value)
3333
{
34+
if ($this->getConnection()->getDriverName() === 'pgsql') {
35+
return unserialize(base64_decode($value));
36+
}
37+
3438
return unserialize($value);
3539
}
3640

3741
public function setActionAttribute($value)
3842
{
39-
$this->attributes['action'] = serialize($value);
43+
$this->attributes['action'] = $this->getConnection()->getDriverName() === 'pgsql'
44+
? base64_encode(serialize($value))
45+
: serialize($value);
4046
}
4147

4248
public function getUrlAttribute()

tests/ConfigTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MagicLink\Test;
44

55
use Illuminate\Support\Facades\App;
6+
use Illuminate\Support\Facades\DB;
67
use MagicLink\Actions\ResponseAction;
78
use MagicLink\Controllers\MagicLinkController;
89
use MagicLink\MagicLink;
@@ -61,4 +62,23 @@ public function test_custom_response_error()
6162

6263
$this->assertEquals(422, $response->getStatusCode());
6364
}
65+
66+
public function test_save_action_serialize()
67+
{
68+
MagicLink::create(new ResponseAction());
69+
70+
$action = DB::table('magic_links')->first(['action'])->action;
71+
72+
if (getenv('DB_DRIVER') === 'pgsql') {
73+
$this->assertInstanceOf(
74+
ResponseAction::class,
75+
unserialize(base64_decode($action))
76+
);
77+
} else {
78+
$this->assertInstanceOf(
79+
ResponseAction::class,
80+
unserialize($action)
81+
);
82+
}
83+
}
6484
}

tests/TestCase.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,42 @@ protected function getEnvironmentSetUp($app)
4040

4141
$app['config']->set('auth.providers.users.model', 'MagicLink\Test\User');
4242

43-
$app['config']->set('database.default', 'sqlite');
43+
$app['config']->set('view.paths', [__DIR__.'/stubs/resources/views']);
44+
45+
$app['config']->set('filesystems.disks.local.root', __DIR__.'/stubs/storage/app');
46+
4447
$app['config']->set('database.connections.sqlite', [
4548
'driver' => 'sqlite',
4649
'database' => ':memory:',
4750
'prefix' => '',
4851
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
4952
]);
5053

51-
$app['config']->set('view.paths', [__DIR__.'/stubs/resources/views']);
52-
$app['config']->set('filesystems.disks.local.root', __DIR__.'/stubs/storage/app');
54+
$app['config']->set('database.connections.pgsql', [
55+
'driver' => 'pgsql',
56+
'host' => '127.0.0.1',
57+
'port' => '54320',
58+
'username' => 'postgres',
59+
'password' => 'mysecretpassword',
60+
'database' => 'test',
61+
]);
62+
63+
$app['config']->set('database.connections.mysql', [
64+
'driver' => 'mysql',
65+
'host' => '127.0.0.1',
66+
'port' => '3306',
67+
'username' => 'root',
68+
'password' => '',
69+
'database' => 'test',
70+
]);
71+
72+
$app['config']->set('database.default', 'sqlite');
73+
74+
if (getenv('DB_DRIVER') === 'pgsql') {
75+
$app['config']->set('database.default', 'pgsql');
76+
} elseif (getenv('DB_DRIVER') === 'mysql') {
77+
$app['config']->set('database.default', 'mysql');
78+
}
5379
}
5480

5581
/**
@@ -59,7 +85,14 @@ protected function getEnvironmentSetUp($app)
5985
*/
6086
protected function setUpDatabase($app)
6187
{
88+
if ($app['config']->get('database.default') !== 'sqlite') {
89+
$app['db']->connection()->getSchemaBuilder()->dropIfExists('users');
90+
$app['db']->connection()->getSchemaBuilder()->dropIfExists('migrations');
91+
$app['db']->connection()->getSchemaBuilder()->dropIfExists('magic_links');
92+
}
93+
6294
$this->artisan('migrate');
95+
6396
$app['db']->connection()->getSchemaBuilder()->create('users', function (Blueprint $table) {
6497
$table->increments('id');
6598
$table->string('email');

0 commit comments

Comments
 (0)