InstallTest.php 4.1 KB

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