LockableTraitTest.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(array()));
  30. $this->assertSame(2, $tester->execute(array()));
  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(array()));
  44. $lock->release();
  45. $this->assertSame(2, $tester->execute(array()));
  46. }
  47. public function testMultipleLockCallsThrowLogicException()
  48. {
  49. $command = new \FooLock2Command();
  50. $tester = new CommandTester($command);
  51. $this->assertSame(1, $tester->execute(array()));
  52. }
  53. }