InstallTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Tests\Feature\Console;
  3. use App\Console\Commands\Install;
  4. use Illuminate\Support\Facades\Artisan;
  5. use Jackiedo\DotenvEditor\DotenvEditor;
  6. use PHPUnit\Framework\Attributes\CoversClass;
  7. use Tests\FeatureTestCase;
  8. /**
  9. * InstallTest test class
  10. */
  11. #[CoversClass(Install::class)]
  12. class InstallTest extends FeatureTestCase
  13. {
  14. /**
  15. * @test
  16. */
  17. const PASSPORT_PENDING_MIGRATIONS_CONFIRMATION = 'Would you like to run all pending database migrations?';
  18. const PASSPORT_CREATE_CLIENTS_CONFIRMATION = 'Would you like to create the "personal access" and "password grant" clients?';
  19. const TWOFAUTH_REVIEW_ENV_VAR_CONFIRMATION = 'Existing .env file found. Do you wish to review its vars?';
  20. public function test_install_completes()
  21. {
  22. $this->artisan('2fauth:install')
  23. ->expectsConfirmation(self::TWOFAUTH_REVIEW_ENV_VAR_CONFIRMATION, 'no')
  24. // 2 following confirmations have been introduced with Passport v12 and its auto-publishing
  25. // migrations feature. Even if the '2fauth:install' command runs 'passport:install'
  26. // silently, the 2 confirmations are triggered and needs to be handled in tests.
  27. ->expectsConfirmation(self::PASSPORT_PENDING_MIGRATIONS_CONFIRMATION, 'yes')
  28. ->expectsConfirmation(self::PASSPORT_CREATE_CLIENTS_CONFIRMATION, 'yes')
  29. ->assertSuccessful();
  30. }
  31. /**
  32. * @test
  33. */
  34. public function test_install_informs_about_no_interaction()
  35. {
  36. $this->artisan('2fauth:install', ['--no-interaction' => true])
  37. ->expectsOutput('(Running in no-interaction mode)')
  38. ->expectsConfirmation(self::TWOFAUTH_REVIEW_ENV_VAR_CONFIRMATION, 'no')
  39. ->expectsConfirmation(self::PASSPORT_PENDING_MIGRATIONS_CONFIRMATION, 'yes')
  40. ->expectsConfirmation(self::PASSPORT_CREATE_CLIENTS_CONFIRMATION, 'yes')
  41. ->assertSuccessful();
  42. }
  43. /**
  44. * @test
  45. */
  46. public function test_install_generates_an_app_key()
  47. {
  48. config(['app.key' => '']);
  49. $this->assertEquals('', config('app.key'));
  50. $this->artisan('2fauth:install')
  51. ->expectsConfirmation(self::TWOFAUTH_REVIEW_ENV_VAR_CONFIRMATION, 'no')
  52. ->expectsConfirmation(self::PASSPORT_PENDING_MIGRATIONS_CONFIRMATION, 'yes')
  53. ->expectsConfirmation(self::PASSPORT_CREATE_CLIENTS_CONFIRMATION, 'yes')
  54. ->assertSuccessful();
  55. $this->assertNotEquals('', config('app.key'));
  56. }
  57. /**
  58. * @test
  59. */
  60. public function test_install_gives_2fauth_address()
  61. {
  62. $this->artisan('2fauth:install')
  63. ->expectsConfirmation(self::TWOFAUTH_REVIEW_ENV_VAR_CONFIRMATION, 'no')
  64. ->expectsConfirmation(self::PASSPORT_PENDING_MIGRATIONS_CONFIRMATION, 'yes')
  65. ->expectsConfirmation(self::PASSPORT_CREATE_CLIENTS_CONFIRMATION, 'yes')
  66. ->expectsOutputToContain(config('app.url'))
  67. ->assertSuccessful();
  68. }
  69. /**
  70. * @test
  71. */
  72. public function test_install_informs_about_sponsoring()
  73. {
  74. $this->artisan('2fauth:install')
  75. ->expectsConfirmation(self::TWOFAUTH_REVIEW_ENV_VAR_CONFIRMATION, 'no')
  76. ->expectsConfirmation(self::PASSPORT_PENDING_MIGRATIONS_CONFIRMATION, 'yes')
  77. ->expectsConfirmation(self::PASSPORT_CREATE_CLIENTS_CONFIRMATION, 'yes')
  78. ->expectsOutputToContain('https://ko-fi.com/bubka')
  79. ->expectsOutputToContain('https://github.com/sponsors/Bubka')
  80. ->assertSuccessful();
  81. }
  82. /**
  83. * @test
  84. */
  85. public function test_install_fails_with_exception_message()
  86. {
  87. $mock = $this->mock(DotenvEditor::class);
  88. $mock->shouldReceive('load')
  89. ->andThrow(new \Exception('exception message'));
  90. $this->artisan('2fauth:install')
  91. ->expectsOutputToContain('exception message')
  92. ->assertFailed();
  93. }
  94. /**
  95. * @test
  96. */
  97. public function test_install_fails_with_link_to_online_help()
  98. {
  99. $mock = $this->mock(DotenvEditor::class);
  100. $mock->shouldReceive('load')
  101. ->andThrow(new \Exception());
  102. $this->artisan('2fauth:install')
  103. ->expectsOutputToContain(config('2fauth.installDocUrl'))
  104. ->assertFailed();
  105. }
  106. }