Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12b0f44

Browse files
Add tests
1 parent 11d7396 commit 12b0f44

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

src/Symfony/Component/Console/Command/LockableTrait.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ trait LockableTrait
2828

2929
private ?LockFactory $lockFactory = null;
3030

31-
public function setLockFactory(?LockFactory $lockFactory): void
32-
{
33-
if (null !== $this->lock) {
34-
throw new LogicException('You cannot set the lockFactory if a lock is already in place.');
35-
}
36-
37-
$this->lockFactory = $lockFactory;
38-
}
39-
4031
/**
4132
* Locks a command.
4233
*/

src/Symfony/Component/Console/Tests/Command/LockableTraitTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Tester\CommandTester;
1616
use Symfony\Component\Lock\LockFactory;
17+
use Symfony\Component\Lock\LockInterface;
1718
use Symfony\Component\Lock\Store\FlockStore;
1819
use Symfony\Component\Lock\Store\SemaphoreStore;
1920

@@ -26,6 +27,7 @@ public static function setUpBeforeClass(): void
2627
self::$fixturesPath = __DIR__.'/../Fixtures/';
2728
require_once self::$fixturesPath.'/FooLockCommand.php';
2829
require_once self::$fixturesPath.'/FooLock2Command.php';
30+
require_once self::$fixturesPath.'/FooLock3Command.php';
2931
}
3032

3133
public function testLockIsReleased()
@@ -64,4 +66,18 @@ public function testMultipleLockCallsThrowLogicException()
6466
$tester = new CommandTester($command);
6567
$this->assertSame(1, $tester->execute([]));
6668
}
69+
70+
public function testCustomLockFactoryIsUsed()
71+
{
72+
$lockFactory = $this->createMock(LockFactory::class);
73+
$command = new \FooLock3Command($lockFactory);
74+
75+
$tester = new CommandTester($command);
76+
77+
$lock = $this->createMock(LockInterface::class);
78+
$lock->method('acquire')->willReturn(false);
79+
80+
$lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
81+
$this->assertSame(1, $tester->execute([]));
82+
}
6783
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Command\LockableTrait;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
use Symfony\Component\Lock\LockFactory;
8+
9+
class FooLock3Command extends Command
10+
{
11+
use LockableTrait;
12+
13+
public function __construct(LockFactory $lockFactory)
14+
{
15+
parent::__construct();
16+
17+
$this->lockFactory = $lockFactory;
18+
}
19+
20+
protected function configure(): void
21+
{
22+
$this->setName('foo:lock3');
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output): int
26+
{
27+
if (!$this->lock()) {
28+
return 1;
29+
}
30+
31+
$this->release();
32+
33+
return 2;
34+
}
35+
}

0 commit comments

Comments
 (0)