Skip to content

Commit

Permalink
feat(cli): adds elgg-cli seed and unseed commands
Browse files Browse the repository at this point in the history
Database seeding can now be performed via elgg-cli tool
  • Loading branch information
hypeJunction committed Aug 15, 2017
1 parent 7b2d459 commit ded471f
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 68 deletions.
28 changes: 0 additions & 28 deletions .scripts/seeder/seed.php

This file was deleted.

22 changes: 0 additions & 22 deletions .scripts/seeder/unseed.php

This file was deleted.

12 changes: 6 additions & 6 deletions .travis.yml
Expand Up @@ -80,13 +80,13 @@ matrix:
install:
- composer travis:install-with-mysql
- php -f ./.scripts/travis/enable_plugins.php
- composer database:seeder:seed
- php ./elgg-cli seed
script:
- php -f ./.scripts/is_memcached_enabled.php
- ./vendor/bin/phpunit --configuration ./.scripts/travis/phpunit-memcached.xml
- php ./elgg-cli simpletest -p all -c ./.scripts/travis/elgg-config/simpletest_memcached.php
after_script:
- composer database:seeder:unseed
- php ./elgg-cli unseed

# HHVM build
- php: hhvm-3.15
Expand All @@ -110,15 +110,15 @@ matrix:
install:
- composer travis:install-with-mysql
- php -f ./.scripts/travis/enable_plugins.php
- composer database:seeder:seed
- php ./elgg-cli seed
- php -S localhost:8888 index.php &
- sleep 3 # give Web server some time to bind to sockets, etc
script:
- curl -o - http://localhost:8888/ | grep "<title>Elgg Travis Site</title>"
- ./vendor/bin/phpunit
- php ./elgg-cli simpletest -p all
after_script:
- composer database:seeder:unseed
- php ./elgg-cli unseed

# Test upgrade path from 2.3
- php: 5.6
Expand All @@ -133,7 +133,7 @@ matrix:
- git checkout FETCH_HEAD
- composer travis:install-with-mysql
- php -f ./.scripts/travis/enable_plugins.php
- composer database:seeder:seed
- php ./elgg-cli seed
- git checkout -
- composer install --prefer-dist
- php -f ./.scripts/travis/upgrade.php
Expand All @@ -144,7 +144,7 @@ matrix:
- sleep 3 # give Web server some time to bind to sockets, etc
- curl -o - http://localhost:8888/ | grep "<title>Elgg Travis Site</title>"
after_script:
- composer database:seeder:unseed
- php ./elgg-cli unseed

services:
- mysql
Expand Down
4 changes: 1 addition & 3 deletions composer.json
Expand Up @@ -65,9 +65,7 @@
"echo \"USE mysql; UPDATE user SET password=PASSWORD('password') WHERE user='root'; FLUSH PRIVILEGES;\" | mysql -u root",
"mkdir \"${HOME}/elgg_data/\"",
"php ./elgg-cli install --config ./install/cli/travis.php"
],
"database:seeder:seed": "php -f ./.scripts/seeder/seed.php",
"database:seeder:unseed": "php -f ./.scripts/seeder/unseed.php"
]
},
"suggest": {
"ext-mbstring": "*"
Expand Down
4 changes: 2 additions & 2 deletions docs/intro/development.rst
Expand Up @@ -79,7 +79,7 @@ You can run the following commands to seed and unseed the database.
..code::sh

# seed the database
composer database:seeder:seed
php ./elgg-cli seed

# unseed the database
composer database:seeder:unseed
php ./elgg-cli unseed
8 changes: 8 additions & 0 deletions docs/intro/elgg-cli.rst
Expand Up @@ -33,6 +33,12 @@ Available commands
# Run Simpletest test suite
elgg-cli simpletest [-c|--config CONFIG] [-p|--plugins PLUGINS]

# Seed the database with fake entities
elgg-cli seed

# Remove seeded faked entities
elgg-cli unseed


Adding custom commands
======================
Expand All @@ -49,3 +55,5 @@ Command class must extend ``\Elgg\CLI\Command``.
return $return;
});



42 changes: 42 additions & 0 deletions engine/classes/Elgg/Cli/SeedCommand.php
@@ -0,0 +1,42 @@
<?php

namespace Elgg\Cli;

/**
* elgg-cli seed
*/
class SeedCommand extends Command {

/**
* {@inheritdoc}
*/
protected function configure() {
$this->setName('seed')
->setDescription('Seeds the database with fake entities');
}

/**
* {@inheritdoc}
*/
protected function command() {

if (!class_exists('\Faker\Generator')) {
elgg_log('This is a developer tool currently intended for testing purposes only. Please refrain from using it.', 'ERROR');
return 1;
}

elgg_set_config('debug', 'NOTICE');

set_time_limit(0);

if (elgg_is_logged_in()) {
elgg_log("Seeds should not be run with a logged in user", 'ERROR');
return 2;
}

_elgg_services()->setValue('mailer', new \Zend\Mail\Transport\InMemory());

_elgg_services()->seeder->seed();
}

}
35 changes: 35 additions & 0 deletions engine/classes/Elgg/Cli/UnseedCommand.php
@@ -0,0 +1,35 @@
<?php

namespace Elgg\Cli;

/**
* elgg-cli unseed
*/
class UnseedCommand extends Command {

/**
* {@inheritdoc}
*/
protected function configure() {
$this->setName('unseed')
->setDescription('Removes seeded fake entities from the database');
}

/**
* {@inheritdoc}
*/
protected function command() {

if (!class_exists('\Faker\Generator')) {
elgg_log('This is a developer tool currently intended for testing purposes only. Please refrain from using it.', 'ERROR');
return 1;
}

set_time_limit(0);

_elgg_services()->setValue('mailer', new \Zend\Mail\Transport\InMemory());

_elgg_services()->seeder->unseed();
}

}
8 changes: 1 addition & 7 deletions engine/classes/Elgg/Database/Seeds/Seed.php
Expand Up @@ -704,12 +704,6 @@ public function createLikes(ElggEntity $entity, $limit = null) {
* @return void
*/
public function log($msg, $level = 'NOTICE') {

if (php_sapi_name() === 'cli') {
$handle = $level === 'ERROR' ? STDERR : STDOUT;
fwrite($handle, $msg . PHP_EOL);
} else {
elgg_log($msg, $level);
}
elgg_log($msg, $level);
}
}
2 changes: 2 additions & 0 deletions engine/lib/elgglib.php
Expand Up @@ -1817,6 +1817,8 @@ function _elgg_init() {
function _elgg_init_cli_commands(\Elgg\Hook $hook) {
$defaults = [
\Elgg\Cli\SimpletestCommand::class,
\Elgg\Cli\SeedCommand::class,
\Elgg\Cli\UnseedCommand::class,
];

return array_merge($defaults, (array) $hook->getValue());
Expand Down

0 comments on commit ded471f

Please sign in to comment.