Skip to content

Commit

Permalink
wip: database persistance and generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mguinea committed Apr 16, 2019
1 parent eaba5da commit 273ac01
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 1 deletion.
10 changes: 10 additions & 0 deletions config/robots.php
@@ -0,0 +1,10 @@
<?php

return [
'models' => [
'row' => Robots\Models\RobotsRow::class,
],
'table_names' => [
'robot_rows' => 'robot_rows',
]
];
37 changes: 37 additions & 0 deletions database/migrations/create_robots_tables.php.stub
@@ -0,0 +1,37 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRobotsTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$tableNames = config('robots.table_names');

Schema::create($tableNames['robot_rows'], function (Blueprint $table) {
$table->increments('id');
$table->string('type');
$table->string('action');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$tableNames = config('robots.table_names');

Schema::drop($tableNames['robot_rows']);
}
}
38 changes: 38 additions & 0 deletions src/Models/RobotsRow.php
@@ -0,0 +1,38 @@
<?php

namespace Robots\Models;

use Robots\Robots;
use Illuminate\Database\Eloquent\Model;

class RobotsRow extends Model
{
protected $fillable = ['action', 'type'];

protected $guarded = ['id'];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setTable(config('robots.table_names.robot_rows'));
}

/**
*
* Generates an array from DB data, compatible with Robots constructor
*
* @return array
*/
public static function generateArray(): array
{
$response = [];

$response['allows'] = static::where('type', Robots::ALLOW)->get('action')->toArray();
$response['disallows'] = static::where('type', Robots::DISALLOW)->get('action')->toArray();
$response['hosts'] = static::where('type', Robots::HOST)->get('action')->toArray();
$response['sitemaps'] = static::where('type', Robots::SITEMAP)->get('action')->toArray();
$response['userAgents'] = static::where('type', Robots::USER_AGENT)->get('action')->toArray();

return $response;
}
}
32 changes: 31 additions & 1 deletion src/RobotsServiceProvider.php
Expand Up @@ -2,6 +2,8 @@

namespace Robots;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;

class RobotsServiceProvider extends ServiceProvider
Expand All @@ -18,8 +20,15 @@ class RobotsServiceProvider extends ServiceProvider
*
* @return void
*/
public function boot()
public function boot(Filesystem $filesystem)
{
$this->publishes([
__DIR__ . '/../config/robots.php' => config_path('robots.php'),
], 'config');

$this->publishes([
__DIR__ . '/../database/migrations/create_robots_tables.php.stub' => $this->getMigrationFileName($filesystem),
], 'migrations');
}

/**
Expand All @@ -34,5 +43,26 @@ public function register()
});

$this->app->alias('robots', 'Robots\Robots');

$this->mergeConfigFrom(
__DIR__.'/../config/robots.php',
'robots'
);
}

/**
* Returns existing migration file if found, else uses the current timestamp.
*
* @param Filesystem $filesystem
* @return string
*/
protected function getMigrationFileName(Filesystem $filesystem): string
{
$timestamp = date('Y_m_d_His');
return Collection::make($this->app->databasePath() . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR)
->flatMap(function ($path) use ($filesystem) {
return $filesystem->glob($path.'*_create_robots_tables.php');
})->push($this->app->databasePath()."/migrations/{$timestamp}_create_robots_tables.php")
->first();
}
}
14 changes: 14 additions & 0 deletions tests/TestCase.php
Expand Up @@ -15,6 +15,8 @@ public function setUp(): void
parent::setUp();

$this->robots = new Robots;

$this->setUpDatabase($this->app);
}

/**
Expand All @@ -28,4 +30,16 @@ protected function getPackageProviders($app)
\Robots\RobotsServiceProvider::class,
];
}

/**
* Set up the database.
*
* @param \Illuminate\Foundation\Application $app
*/
protected function setUpDatabase($app)
{
include_once __DIR__ . '/../database/migrations/create_robots_tables.php.stub';

(new \CreateRobotsTables())->up();
}
}
94 changes: 94 additions & 0 deletions tests/Unit/RobotsDBTest.php
@@ -0,0 +1,94 @@
<?php

namespace Robots\Tests;

use Robots\Robots;
use Robots\Models\RobotsRow;

class RobotsDBTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}

public function testAddDBAllow()
{
$expected = 'Allow: foo';

RobotsRow::create([
'type' => Robots::ALLOW,
'action' => 'foo'
]);

$arrayDBRows = RobotsRow::generateArray();

$robots = new Robots($arrayDBRows);

$this->assertEquals($expected, $robots->generate());
}

public function testAddDBDisallow()
{
$expected = 'Disallow: foo';

RobotsRow::create([
'type' => Robots::DISALLOW,
'action' => 'foo'
]);

$arrayDBRows = RobotsRow::generateArray();

$robots = new Robots($arrayDBRows);

$this->assertEquals($expected, $robots->generate());
}

public function testAddDBUserAgent()
{
$expected = 'User-agent: foo';

RobotsRow::create([
'type' => Robots::USER_AGENT,
'action' => 'foo'
]);

$arrayDBRows = RobotsRow::generateArray();

$robots = new Robots($arrayDBRows);

$this->assertEquals($expected, $robots->generate());
}

public function testAddDBHost()
{
$expected = 'Host: foo';

RobotsRow::create([
'type' => Robots::HOST,
'action' => 'foo'
]);

$arrayDBRows = RobotsRow::generateArray();

$robots = new Robots($arrayDBRows);

$this->assertEquals($expected, $robots->generate());
}

public function testAddDBSitemap()
{
$expected = 'Sitemap: foo';

RobotsRow::create([
'type' => Robots::SITEMAP,
'action' => 'foo'
]);

$arrayDBRows = RobotsRow::generateArray();

$robots = new Robots($arrayDBRows);

$this->assertEquals($expected, $robots->generate());
}
}

0 comments on commit 273ac01

Please sign in to comment.