LockableTraitTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Command;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Tester\CommandTester;
  13. use Symfony\Component\Lock\Factory;
  14. use Symfony\Component\Lock\Store\FlockStore;
  15. use Symfony\Component\Lock\Store\SemaphoreStore;
  16. class LockableTraitTest extends TestCase
  17. {
  18. protected static $fixturesPath;
  19. public static function setUpBeforeClass()
  20. {
  21. self::$fixturesPath = __DIR__.'/../Fixtures/';
  22. require_once self::$fixturesPath.'/FooLockCommand.php';
  23. require_once self::$fixturesPath.'/FooLock2Command.php';
  24. }
  25. public function testLockIsReleased()
  26. {
  27. $command = new \FooLockCommand();
  28. $tester = new CommandTester($command);
  29. $this->assertSame(2, $tester->execute([]));
  30. $this->assertSame(2, $tester->execute([]));
  31. }
  32. public function testLockReturnsFalseIfAlreadyLockedByAnotherCommand()
  33. {
  34. $command = new \FooLockCommand();
  35. if (SemaphoreStore::isSupported()) {
  36. $store = new SemaphoreStore();
  37. } else {
  38. $store = new FlockStore();
  39. }
  40. $lock = (new Factory($store))->createLock($command->getName());
  41. $lock->acquire();
  42. $tester = new CommandTester($command);
  43. $this->assertSame(1, $tester->execute([]));
  44. $lock->release();
  45. $this->assertSame(2, $tester->execute([]));
  46. }
  47. public function testMultipleLockCallsThrowLogicException()
  48. {
  49. $command = new \FooLock2Command();
  50. $tester = new CommandTester($command);
  51. $this->assertSame(1, $tester->execute([]));
  52. }
  53. }