/** * Create database tables required for testing. * * @return array array of test data created */ public static function setUp() { //Load environment variables $dotenv = new \Dotenv\Dotenv(__DIR__ . '/../../'); if (!getenv('APP_ENV')) { $dotenv->load(); } //Create connection and execute schema static::$conn = new Connection(); Schema::createSchema(); //Add test data to user table if not exist or no errors returned $user = User::firstOrCreate(['username' => 'test-root', 'password' => hash('SHA256', 'test-root')]); $emojiData = ['name' => 'Happy Face', 'char' => ':)', 'category' => 'Happy']; //Build keywords array and create users emojis $keywords = ['happy', 'face', 'emotion']; $emoji = $user->emojis()->firstOrCreate($emojiData); //Add keywords to keywords table $keyId = []; foreach ($keywords as $keyword) { $key = $emoji->keywords()->firstOrCreate(['name' => $keyword]); $keyId[] = $key->id; } return ['userId' => $user->id, 'emojiId' => $emoji->id, 'keywordsId' => $keyId]; }
<?php require 'vendor/autoload.php'; use Slim\App; use Slim\Container; use BB8\Emoji\Database\Connection; use BB8\Emoji\Database\Schema; use BB8\Emoji\Middleware; use BB8\Emoji\Models\User; //Create connection to database $connection = new Connection(); //Creaet database tables if table does not exist Schema::createSchema(); //Initialize a new dependency container $container = new Container(); //Add container to handle all exceptions/errors, fail safe and return json $container['errorHandler'] = function ($container) { return function ($request, $response, $exception) use($container) { //Format of exception to return $data = ['message' => $exception->getMessage()]; return $container->get('response')->withStatus(500)->withHeader('Content-Type', 'application/json')->write(json_encode($data)); }; }; //Register authentication container Dependency $container['auth'] = function ($container) { return new BB8\Emoji\Auth($container); }; //Initialize the slim app $app = new App($container); //Add middleware at app level $app->add('BB8\\Emoji\\Middleware:init');