Skip to content

Commit f0d1513

Browse files
committed
Bugfixes for unpair command
1 parent 3896ad9 commit f0d1513

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

src/Commands/PairCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8989
$pair->add($persona);
9090
}
9191

92+
$this->repository->storePreviousUser();
93+
9294
if ($this->repository->setUser($pair->factorUser())) {
9395
foreach ($pair as $persona) {
9496
/** @var Persona $persona */

src/Commands/UnpairCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Stolt\GitUserBend\Commands;
44

5-
use Stolt\GitUserBend\Exceptions\CommandFailed;
65
use Stolt\GitUserBend\Exceptions\Exception;
76
use Stolt\GitUserBend\Git\Repository;
7+
use Stolt\GitUserBend\Git\User;
88
use Symfony\Component\Console\Command\Command;
99
use Symfony\Component\Console\Input\InputArgument;
1010
use Symfony\Component\Console\Input\InputInterface;
@@ -65,6 +65,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6565

6666
$formerPersona = $this->repository->getFormerPersonaFromConfiguration();
6767

68+
$this->repository->removeFormerPersonaFromConfiguration();
69+
70+
$this->repository->setUser(new User($formerPersona->getName(), $formerPersona->getEmail()));
71+
6872
$outputContent = "<info>Reset user config to <comment>"
6973
. "'{$formerPersona->getName()} <{$formerPersona->getEmail()}>'</comment>.</info>";
7074
$output->writeln($outputContent);

src/Git/Repository.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,26 @@ public function getFormerPersonaFromConfiguration(): Persona
180180
throw new UnresolvablePersona('Unable to resolve former persona from Git configuration.');
181181
}
182182

183+
public function removeFormerPersonaFromConfiguration(): bool
184+
{
185+
chdir($this->directory);
186+
187+
$commands = [
188+
'git config --unset user.former.email',
189+
'git config --unset user.former.name',
190+
];
191+
192+
foreach ($commands as $command) {
193+
exec($command, $output, $returnValue);
194+
195+
if ($returnValue !== 0) {
196+
return false;
197+
}
198+
}
199+
200+
return true;
201+
}
202+
183203
/**
184204
* @return boolean
185205
*/
@@ -226,8 +246,8 @@ public function storePreviousUser(): bool
226246
$persona = $this->getPersonaFromConfiguration();
227247

228248
$storageCommands = [
229-
"git config --local user.former.email {$persona->getEmail()}",
230-
"git config --local user.former.name '{$persona->getName()}'",
249+
"git config --local --replace-all user.former.email {$persona->getEmail()}",
250+
"git config --local --replace-all user.former.name '{$persona->getName()}'",
231251
];
232252

233253
foreach ($storageCommands as $command) {
@@ -249,8 +269,6 @@ public function setUser(User $user): bool
249269
{
250270
chdir($this->directory);
251271

252-
$this->storePreviousUser();
253-
254272
$commands = [
255273
"git config --local user.email \"{$user->getEmail()}\"",
256274
"git config --local user.name \"{$user->getName()}\"",

tests/Commands/PairCommandTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
class PairCommandTest extends TestCase
1919
{
2020
/**
21-
* @var \Symfony\Component\Console\Application
21+
* @var Application
2222
*/
2323
private $application;
2424

2525
/**
26-
* @return \Symfony\Component\Console\Application
26+
* @return Application
2727
*/
2828
protected function getApplication(): Application
2929
{
@@ -211,6 +211,7 @@ public function returnsExpectedWarningWhenSetPairPersonaFails(): void
211211

212212
$repository->shouldReceive('setDirectory')->times(1);
213213
$repository->shouldReceive('setUser')->times(1)->andReturn(false);
214+
$repository->shouldReceive('storePreviousUser')->times(1)->andReturn(true);
214215

215216
$command = $application->find('pair');
216217
$commandTester = new CommandTester($command);

tests/Commands/UnpairCommandTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
use PHPUnit\Framework\Attributes\Test;
77
use Stolt\GitUserBend\Commands\PairCommand;
88
use Stolt\GitUserBend\Commands\UnpairCommand;
9+
use Stolt\GitUserBend\Exceptions\InvalidPersona;
10+
use Stolt\GitUserBend\Exceptions\UnresolvablePersona;
911
use Stolt\GitUserBend\Git\Repository;
1012
use Stolt\GitUserBend\Git\User;
1113
use Stolt\GitUserBend\Helpers\Str as OsHelper;
14+
use Stolt\GitUserBend\Persona;
1215
use Stolt\GitUserBend\Persona\Storage;
1316
use Stolt\GitUserBend\Tests\CommandTester;
1417
use Stolt\GitUserBend\Tests\TestCase;
@@ -119,6 +122,10 @@ public function returnsExpectedWarningWhenProvidedDirectoryIsNotAGitRepository()
119122
$this->assertTrue($commandTester->getStatusCode() == Command::FAILURE);
120123
}
121124

125+
/**
126+
* @throws UnresolvablePersona
127+
* @throws InvalidPersona
128+
*/
122129
#[Test]
123130
#[Group('integration')]
124131
public function resetsGitConfigToFormerUser(): void
@@ -161,5 +168,13 @@ public function resetsGitConfigToFormerUser(): void
161168

162169
$this->assertSame($expectedDisplay, $commandTester->getDisplay());
163170
$commandTester->assertCommandIsSuccessful();
171+
172+
$repository = new Repository($this->temporaryDirectory);
173+
$personaFromConfig = $repository->getPersonaFromConfiguration();
174+
$this->assertSame('John Doe', $personaFromConfig->getName());
175+
$this->assertSame('test@test.org', $personaFromConfig->getEmail());
176+
177+
$this->expectException(UnresolvablePersona::class);
178+
$repository->getFormerPersonaFromConfiguration();
164179
}
165180
}

0 commit comments

Comments
 (0)