diff --git a/README.md b/README.md index 9262cda..ac32f24 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ * [Requirements](#requirements) * [Installation](#installation) * [Customizing Mercurius](#customizing-mercurius) +* [Testing](#testing) * [Roadmap](#roadmap) * [Support](#support) * [Contributing](#contributing) @@ -192,6 +193,25 @@ Please see [Customizing-Mercurius](CUSTOMIZING-MERCURIUS.md) for more informatio
+## Testing +There are 2 different testsuites + +### Application Testsuite +Run the following command to test the general application testsuite. +``` +vendor/bin/phpunit +``` + +### Dusk Testsuite +Run the following command to test the Dusk testsuite. +``` +vendor/bin/phpunit --testsuite=dusk +``` + + +
+ + ## Roadmap Check the [roadmap](https://github.com/launcher-host/mercurius/issues/8) for more information. diff --git a/composer.json b/composer.json index f199245..8188172 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "phpunit/phpunit": "^7.0", "mockery/mockery": "^1.0", "orchestra/testbench": "^3.7", - "codeclimate/php-test-reporter": "dev-master" + "codeclimate/php-test-reporter": "dev-master", + "orchestra/testbench-dusk": "~3.7" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index d61f91a..27e1d4a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,10 +10,15 @@ stopOnError="false" stopOnFailure="false" verbose="true" + defaultTestSuite="app" > - - tests + + tests/Unit + tests/Feature + + + tests/Browser @@ -29,5 +34,7 @@ + + \ No newline at end of file diff --git a/src/MercuriusServiceProvider.php b/src/MercuriusServiceProvider.php index d2f5439..cb83ee4 100755 --- a/src/MercuriusServiceProvider.php +++ b/src/MercuriusServiceProvider.php @@ -2,6 +2,7 @@ namespace Launcher\Mercurius; +use Carbon\Carbon; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Routing\Router; use Illuminate\Support\Facades\Schema; @@ -55,16 +56,16 @@ public function register() */ protected function registerPublishable() { - $_path = __DIR__.'/../publishable/'; + $path = __DIR__.'/../publishable/'; $publishable = [ - 'mercurius-config' => ["{$_path}config/mercurius.php" => config_path('mercurius.php')], - 'mercurius-public' => ["{$_path}public" => public_path('vendor/mercurius')], + 'mercurius-config' => ["{$path}config/mercurius.php" => config_path('mercurius.php')], + 'mercurius-public' => ["{$path}public" => public_path('vendor/mercurius')], 'mercurius-sass' => [__DIR__.'/../resources/sass/' => resource_path('sass/vendor/mercurius')], 'mercurius-js' => [__DIR__.'/../resources/js/' => resource_path('js/vendor/mercurius')], - 'mercurius-seeds' => ["{$_path}database/seeds/" => database_path('seeds')], - 'mercurius-lang' => ["{$_path}lang/" => resource_path('lang')], - 'mercurius-views' => ["{$_path}views/" => resource_path('views/vendor/mercurius')], + 'mercurius-seeds' => ["{$path}database/seeds/" => database_path('seeds')], + 'mercurius-lang' => ["{$path}lang/" => resource_path('lang')], + 'mercurius-views' => ["{$path}views/" => resource_path('views/vendor/mercurius')], ]; foreach ($publishable as $group => $paths) { @@ -79,17 +80,17 @@ protected function registerPublishable() */ private function registerPublishableMigrations() { - if (!Schema::hasTable('mercurius_messages')) { - $_date = date('Y_m_d_His', time()); - $_path = __DIR__.'/../publishable/database/migrations/'; + //if (!Schema::hasTable('mercurius_messages')) { + $date = Carbon::now()->format('Y_m_d_His'); + $path = __DIR__ . '/../publishable/database/migrations/'; $_migrations = [ - "${_path}add_mercurius_user_fields.php" => database_path("migrations/${_date}_add_mercurius_user_fields.php"), - "${_path}create_mercurius_messages_table.php" => database_path("migrations/${_date}_create_mercurius_messages_table.php"), + "${path}add_mercurius_user_fields.php" => database_path("migrations/${date}_add_mercurius_user_fields.php"), + "${path}create_mercurius_messages_table.php" => database_path("migrations/${date}_create_mercurius_messages_table.php"), ]; $this->publishes($_migrations, 'mercurius-migrations'); - } + //} } /** diff --git a/src/Repositories/ConversationRepository.php b/src/Repositories/ConversationRepository.php index 080a62d..ba351e9 100755 --- a/src/Repositories/ConversationRepository.php +++ b/src/Repositories/ConversationRepository.php @@ -92,10 +92,10 @@ public function all($user = null) ' FROM', ' (', ' SELECT receiver_id as usr, MAX(id) AS id FROM '.$tbl_messages, - ' WHERE deleted_by_sender IS FALSE AND sender_id ='.$user.' GROUP BY usr', + ' WHERE deleted_by_sender IS 0 AND sender_id ='.$user.' GROUP BY usr', ' UNION ALL', ' SELECT sender_id as usr, MAX(id) as id FROM '.$tbl_messages, - ' WHERE deleted_by_receiver IS FALSE AND receiver_id ='.$user.' GROUP BY usr', + ' WHERE deleted_by_receiver IS 0 AND receiver_id ='.$user.' GROUP BY usr', ' ) u', ' GROUP BY u.usr', ' ) mm', @@ -127,10 +127,10 @@ public function recipients($user = null) 'FROM', ' (', ' SELECT receiver_id as id FROM '.$tbl_messages, - ' WHERE sender_id ='.$user.' AND deleted_by_receiver IS FALSE', + ' WHERE sender_id ='.$user.' AND deleted_by_receiver IS 0', ' UNION ALL', ' SELECT sender_id as id FROM '.$tbl_messages, - ' WHERE receiver_id ='.$user.' AND deleted_by_sender IS FALSE', + ' WHERE receiver_id ='.$user.' AND deleted_by_sender IS 0', ') u', ])); diff --git a/tests/Browser/ExampleTest.php b/tests/Browser/ExampleTest.php new file mode 100644 index 0000000..30517c0 --- /dev/null +++ b/tests/Browser/ExampleTest.php @@ -0,0 +1,23 @@ +userFactory()->create(); + + /* + $this->browse(function ($browser) use ($user) { + $response = $browser->loginAs($user) + ->visit(route('mercurius.home')); + sleep(10); + }); + */ + } +} \ No newline at end of file diff --git a/tests/DuskTestCase.php b/tests/DuskTestCase.php new file mode 100644 index 0000000..4987a7e --- /dev/null +++ b/tests/DuskTestCase.php @@ -0,0 +1,56 @@ +migrate) { + $this->migrate(); + } + + $this->withFactories(__DIR__.'/Factories'); + + $this->disableExceptionHandling(); + } + + protected function getPackageProviders($app) + { + return [ + MercuriusServiceProvider::class, + ]; + } + + protected function userFactory() + { + return factory(User::class); + } + + protected function migrate() + { + $this->loadLaravelMigrations(); + + Artisan::call('vendor:publish', [ + '--tag' => 'mercurius-migrations', + ]); + + Carbon::setTestNow(); + + $this->artisan('migrate'); + } +} diff --git a/tests/Factories/MessageFactory.php b/tests/Factories/MessageFactory.php new file mode 100644 index 0000000..9843a9f --- /dev/null +++ b/tests/Factories/MessageFactory.php @@ -0,0 +1,17 @@ +define(Message::class, function (Faker $faker) { + return [ + 'message' => $faker->sentence, + 'sender_id' => factory(User::class), + 'receiver_id' => factory(User::class), + 'seen_at' => Carbon::now(), + 'deleted_by_sender' => false, + 'deleted_by_receiver' => false, + ]; +}); diff --git a/tests/Factories/UserFactory.php b/tests/Factories/UserFactory.php new file mode 100644 index 0000000..73fd84f --- /dev/null +++ b/tests/Factories/UserFactory.php @@ -0,0 +1,13 @@ +define(User::class, function (Faker $faker) { + return [ + 'name' => $faker->name, + 'email' => $faker->unique()->safeEmail, + 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret + 'remember_token' => str_random(10), + ]; +}); diff --git a/tests/Feature/ConversationTest.php b/tests/Feature/ConversationTest.php new file mode 100644 index 0000000..5deff95 --- /dev/null +++ b/tests/Feature/ConversationTest.php @@ -0,0 +1,57 @@ +userFactory()->create(); + + $messageA = $this->messageFactory()->create([ + 'sender_id' => $user->id, + ]); + + $messageB = $this->messageFactory()->create([ + 'receiver_id' => $user->id, + ]); + + $users = User::all(); + + $response = $this->actingAs($user) + ->get('/conversations') + ->assertOk() + ->assertJson([ + [ + 'id' => '2', + 'user' => $users->find(2)->name, + 'avatar' => $users->find(2)->avatar, + 'is_online' => '1', + 'sender' => '1', + 'message' => $messageA->message, + 'seen_at' => Carbon::now()->toDateTimeString(), + 'created_at' => Carbon::now()->toDateTimeString(), + ], + [ + 'id' => '3', + 'user' => $users->find(3)->name, + 'avatar' => $users->find(2)->avatar, + 'is_online' => '1', + 'sender' => '3', + 'message' => $messageB->message, + 'seen_at' => Carbon::now()->toDateTimeString(), + 'created_at' => Carbon::now()->toDateTimeString(), + ] + ]); + } + + public function test_getting_recipients() {} + public function test_getting_converstion_with_given_user() {} + public function test_deleting_conversation() {} +} diff --git a/tests/Feature/MessageTest.php b/tests/Feature/MessageTest.php new file mode 100644 index 0000000..ecc20c2 --- /dev/null +++ b/tests/Feature/MessageTest.php @@ -0,0 +1,11 @@ +userFactory()->create(); + + $response = $this->actingAs($user) + ->get('/profile/refresh') + ->assertOk() + ->assertJson(array_except($user->toArray(), [ + 'email', 'password', 'remember_token', 'updated_at', 'created_at' + ])); + } + + public function test_getting_notifications() {} + public function test_updating_profile() {} +} diff --git a/tests/Feature/ReceiverTest.php b/tests/Feature/ReceiverTest.php new file mode 100644 index 0000000..6ad5692 --- /dev/null +++ b/tests/Feature/ReceiverTest.php @@ -0,0 +1,10 @@ +oldExceptionHandler = $this->app->make(ExceptionHandler::class); + + $this->app->instance(ExceptionHandler::class, new class extends Handler { + public function __construct() {} + public function report(Exception $e) {} + public function render($request, Exception $e) { + throw $e; + } + }); + } + + protected function withExceptionHandling() + { + $this->app->instance(ExceptionHandler::class, $this->oldExceptionHandler); + + return $this; + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index c62f2ce..0071ff6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,18 +2,61 @@ namespace Launcher\Mercurius\Tests; +use Carbon\Carbon; +use Illuminate\Foundation\Auth\User; +use Launcher\Mercurius\Models\Message; +use Illuminate\Support\Facades\Artisan; +use Launcher\Mercurius\MercuriusServiceProvider; use Orchestra\Testbench\TestCase as OrchestraTestCase; class TestCase extends OrchestraTestCase { - /** @test */ - public function example_test_aaa() + use HandlesExceptions; + + protected $migrate = false; + + public function setUp() + { + Carbon::setTestNow(Carbon::parse('2099-01-01 00:00:00')); + + parent::setUp(); + + if ($this->migrate) { + $this->migrate(); + } + + $this->withFactories(__DIR__.'/Factories'); + + $this->disableExceptionHandling(); + } + + protected function getPackageProviders($app) + { + return [ + MercuriusServiceProvider::class, + ]; + } + + protected function userFactory() + { + return factory(User::class); + } + + protected function messageFactory() { - $this->assertTrue(true); + return factory(Message::class); } - public function example_test_bbb() + protected function migrate() { - $this->assertTrue(true); + $this->loadLaravelMigrations(); + + Artisan::call('vendor:publish', [ + '--tag' => 'mercurius-migrations', + ]); + + Carbon::setTestNow(); + + $this->artisan('migrate'); } } diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php index f1d011f..18202c4 100644 --- a/tests/Unit/ExampleTest.php +++ b/tests/Unit/ExampleTest.php @@ -6,9 +6,5 @@ class ExampleTest extends TestCase { - /** @test */ - public function example_test_method() - { - $this->assertTrue(true); - } + // TODO: Make Unit Tests }