|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Stolt\GitUserBend\Tests\Commands; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\Group; |
| 6 | +use PHPUnit\Framework\Attributes\Test; |
| 7 | +use Stolt\GitUserBend\Commands\ResetCommand; |
| 8 | +use Stolt\GitUserBend\Commands\UseCommand; |
| 9 | +use Stolt\GitUserBend\Exceptions\InvalidPersona; |
| 10 | +use Stolt\GitUserBend\Exceptions\UnresolvablePersona; |
| 11 | +use Stolt\GitUserBend\Git\Repository; |
| 12 | +use Stolt\GitUserBend\Git\User; |
| 13 | +use Stolt\GitUserBend\Helpers\Str as OsHelper; |
| 14 | +use Stolt\GitUserBend\Persona\Storage; |
| 15 | +use Stolt\GitUserBend\Tests\CommandTester; |
| 16 | +use Stolt\GitUserBend\Tests\TestCase; |
| 17 | +use Symfony\Component\Console\Application; |
| 18 | +use Symfony\Component\Console\Command\Command; |
| 19 | + |
| 20 | +class ResetCommandTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var Application |
| 24 | + */ |
| 25 | + private Application $application; |
| 26 | + |
| 27 | + /** |
| 28 | + * @return Application |
| 29 | + */ |
| 30 | + protected function getApplication(): Application |
| 31 | + { |
| 32 | + $application = new Application(); |
| 33 | + $command = new UseCommand( |
| 34 | + new Storage(STORAGE_FILE), |
| 35 | + new Repository($this->temporaryDirectory) |
| 36 | + ); |
| 37 | + $resetCommand = new ResetCommand( |
| 38 | + new Repository($this->temporaryDirectory) |
| 39 | + ); |
| 40 | + |
| 41 | + $application->add($command); |
| 42 | + $application->add($resetCommand); |
| 43 | + |
| 44 | + return $application; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Set up test environment. |
| 49 | + */ |
| 50 | + protected function setUp(): void |
| 51 | + { |
| 52 | + $this->setUpTemporaryDirectory(); |
| 53 | + |
| 54 | + if (!defined('WORKING_DIRECTORY')) { |
| 55 | + define('WORKING_DIRECTORY', $this->temporaryDirectory); |
| 56 | + } |
| 57 | + |
| 58 | + if (!defined('HOME_DIRECTORY')) { |
| 59 | + define('HOME_DIRECTORY', $this->temporaryDirectory); |
| 60 | + } |
| 61 | + |
| 62 | + if (!defined('STORAGE_FILE')) { |
| 63 | + define( |
| 64 | + 'STORAGE_FILE', |
| 65 | + HOME_DIRECTORY . DIRECTORY_SEPARATOR . Storage::FILE_NAME |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + $this->application = $this->getApplication(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Tear down test environment. |
| 74 | + * |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + protected function tearDown(): void |
| 78 | + { |
| 79 | + if (is_dir($this->temporaryDirectory)) { |
| 80 | + $this->removeDirectory($this->temporaryDirectory); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + #[Test] |
| 85 | + #[Group('integration')] |
| 86 | + public function returnsExpectedWarningWhenProvidedDirectoryDoesNotExist(): void |
| 87 | + { |
| 88 | + $command = $this->application->find('reset'); |
| 89 | + $commandTester = new CommandTester($command); |
| 90 | + $commandTester->execute([ |
| 91 | + 'command' => $command->getName(), |
| 92 | + 'directory' => '/out/of/orbit', |
| 93 | + ]); |
| 94 | + |
| 95 | + $expectedDisplay = <<<CONTENT |
| 96 | +Error: The directory /out/of/orbit doesn't exist. |
| 97 | +
|
| 98 | +CONTENT; |
| 99 | + |
| 100 | + $this->assertSame($expectedDisplay, $commandTester->getDisplay()); |
| 101 | + $this->assertTrue($commandTester->getStatusCode() == 1); |
| 102 | + } |
| 103 | + |
| 104 | + #[Test] |
| 105 | + #[Group('integration')] |
| 106 | + public function returnsExpectedWarningWhenProvidedDirectoryIsNotAGitRepository(): void |
| 107 | + { |
| 108 | + $command = $this->application->find('reset'); |
| 109 | + $commandTester = new CommandTester($command); |
| 110 | + $commandTester->execute([ |
| 111 | + 'command' => $command->getName(), |
| 112 | + 'directory' => $this->temporaryDirectory, |
| 113 | + ]); |
| 114 | + |
| 115 | + $expectedDisplay = <<<CONTENT |
| 116 | +Error: No Git repository in {$this->temporaryDirectory}. |
| 117 | +
|
| 118 | +CONTENT; |
| 119 | + |
| 120 | + $this->assertSame($expectedDisplay, $commandTester->getDisplay()); |
| 121 | + $this->assertTrue($commandTester->getStatusCode() == Command::FAILURE); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @throws UnresolvablePersona |
| 126 | + * @throws InvalidPersona |
| 127 | + */ |
| 128 | + #[Test] |
| 129 | + #[Group('integration')] |
| 130 | + public function resetsGitConfigToFormerUser(): void |
| 131 | + { |
| 132 | + /*if ((new OsHelper())->isWindows()) { |
| 133 | + $this->markTestSkipped('Skipping test on Windows systems'); |
| 134 | + }*/ |
| 135 | + |
| 136 | + $this-> createTemporaryGitRepository( new User( 'John Doe', '[email protected]')); |
| 137 | + |
| 138 | + chdir($this->temporaryDirectory); |
| 139 | + |
| 140 | + $existingStorageContent = <<<CONTENT |
| 141 | +[{"alias":"jd","name":"John Doe","email":"[email protected]","usage_frequency":11}, |
| 142 | + {"alias":"so","name":"Some One","email":"[email protected]","usage_frequency":23}] |
| 143 | +CONTENT; |
| 144 | + $this->createTemporaryStorageFile($existingStorageContent); |
| 145 | + |
| 146 | + $command = $this->application->find('use'); |
| 147 | + $commandTester = new CommandTester($command); |
| 148 | + $commandTester->execute([ |
| 149 | + 'command' => $command->getName(), |
| 150 | + 'directory' => $this->temporaryDirectory, |
| 151 | + 'alias' => 'jd', |
| 152 | + ]); |
| 153 | + |
| 154 | + $commandTester->assertCommandIsSuccessful(); |
| 155 | + |
| 156 | + $command = $this->application->find('reset'); |
| 157 | + $commandTester = new CommandTester($command); |
| 158 | + $commandTester->execute([ |
| 159 | + 'command' => $command->getName(), |
| 160 | + 'directory' => $this->temporaryDirectory, |
| 161 | + ]); |
| 162 | + |
| 163 | + $expectedDisplay = <<<CONTENT |
| 164 | +Reset user config to 'John Doe <[email protected]>'. |
| 165 | +
|
| 166 | +CONTENT; |
| 167 | + |
| 168 | + $this->assertSame($expectedDisplay, $commandTester->getDisplay()); |
| 169 | + $commandTester->assertCommandIsSuccessful(); |
| 170 | + |
| 171 | + $repository = new Repository($this->temporaryDirectory); |
| 172 | + $personaFromConfig = $repository->getPersonaFromConfiguration(); |
| 173 | + $this->assertSame('John Doe', $personaFromConfig->getName()); |
| 174 | + $this-> assertSame( '[email protected]', $personaFromConfig-> getEmail()); |
| 175 | + |
| 176 | + $this->expectException(UnresolvablePersona::class); |
| 177 | + $repository->getFormerPersonaFromConfiguration(); |
| 178 | + } |
| 179 | +} |
0 commit comments