/Tests/Helper/Locking/LockingEnhancementTest.php

https://gitlab.com/sarelvdwalt/BaseCommandBundle · PHP · 212 lines · 143 code · 36 blank · 33 comment · 3 complexity · 31921961441d42c385d1bb3a25ebea01 MD5 · raw file

  1. <?php
  2. use Afrihost\BaseCommandBundle\Tests\Fixtures\App\TestKernel;
  3. use Afrihost\BaseCommandBundle\Tests\Fixtures\EncapsulationViolator;
  4. use Afrihost\BaseCommandBundle\Tests\Fixtures\HelloWorldCommand;
  5. use Afrihost\BaseCommandBundle\Tests\Fixtures\LockCommand;
  6. use Symfony\Component\Filesystem\Filesystem;
  7. use Symfony\Component\Finder\Finder;
  8. class LockingEnhancementTest extends AbstractContainerTest
  9. {
  10. static protected $homeLocationBackup;
  11. static protected $testHomeLocation;
  12. /**
  13. * Some of the tests in this class attempt to create files relative to the user's home directory. This fixture attempts
  14. * to override the $HOME environment to a directory within the Test folder to attempt to keep the tests clean
  15. */
  16. public static function setUpBeforeClass()
  17. {
  18. // Backup the current value to be restored after the tests
  19. self::$homeLocationBackup = getenv('HOME');
  20. // Get the location of the test Application
  21. $kernel = new TestKernel('test', true);
  22. $kernel->boot();
  23. self::$testHomeLocation = $kernel->getRootDir().DIRECTORY_SEPARATOR.'externals'.DIRECTORY_SEPARATOR.'home';
  24. // Override the current value
  25. putenv('HOME='.self::getTestHomeLocation());
  26. }
  27. /**
  28. * Cleans up the overriding of the $HOME environment variable done by setUpBeforeClass()
  29. */
  30. public static function tearDownAfterClass()
  31. {
  32. if(!is_null(self::$homeLocationBackup)){
  33. putenv('HOME='.self::getTestHomeLocation());
  34. }
  35. }
  36. public function testSetLockFileFolderTilde()
  37. {
  38. $this->removeAllLockFiles(self::getTestHomeLocation());
  39. $command = $this->registerCommand(new LockCommand());
  40. EncapsulationViolator::invokeMethod($command, 'setLockFileFolder', array('~/locks'));
  41. $commandTester = $this->executeCommand($command);
  42. $this->assertEquals(
  43. self::getTestHomeLocation().DIRECTORY_SEPARATOR.'locks',
  44. EncapsulationViolator::invokeMethod($command, 'getLockFileFolder'),
  45. 'The lock file location that was set relative to the user\'s home directory does not seem to have been returned'
  46. );
  47. $this->assertTrue(
  48. $this->lockFileExists($this->getTestHomeLocation(), 'LockCommand.php'),
  49. 'A lock file does not seem to have been created relative to the user\'s home directory '
  50. );
  51. $this->assertContains(
  52. 'Sorry, can\'t get the lock. Bailing out!',
  53. $commandTester->getDisplay(),
  54. 'The lock does not seem to have been acquired correctly as the same command was run twice at the same time '.
  55. 'without error'
  56. );
  57. $this->removeAllLockFiles(self::getTestHomeLocation());
  58. }
  59. public function testSetLockFileFolderAbsolute()
  60. {
  61. $lockFolderName = $this->application->getKernel()->getRootDir() . '/externals/absolute';
  62. $this->removeAllLockFiles($lockFolderName);
  63. $command = $this->registerCommand(new LockCommand());
  64. EncapsulationViolator::invokeMethod($command, 'setLockFileFolder', array($lockFolderName));
  65. EncapsulationViolator::invokeMethod($command, 'setLocking', array(true));
  66. $commandTester = $this->executeCommand($command);
  67. $this->assertEquals($lockFolderName, EncapsulationViolator::invokeMethod($command, 'getLockFileFolder'));
  68. $this->assertTrue(
  69. $this->lockFileExists($lockFolderName, 'LockCommand.php'),
  70. 'A lock file does not seem to have been created relative to the user\'s home directory '
  71. );
  72. $this->assertContains(
  73. 'Sorry, can\'t get the lock. Bailing out!',
  74. $commandTester->getDisplay(),
  75. 'The lock does not seem to have been acquired correctly as the same command was run twice at the same time '.
  76. 'without error'
  77. );
  78. $this->removeAllLockFiles($lockFolderName);
  79. }
  80. public function testSetLockFileFolderRelative()
  81. {
  82. $expectedFolder = $this->application->getKernel()->getRootDir() . '/externals/relative';
  83. $this->removeAllLockFiles($expectedFolder);
  84. $command = $this->registerCommand(new LockCommand());
  85. EncapsulationViolator::invokeMethod($command, 'setLockFileFolder', array('externals/relative'));
  86. $commandTester = $this->executeCommand($command);
  87. $this->assertEquals($expectedFolder, EncapsulationViolator::invokeMethod($command, 'getLockFileFolder'));
  88. $this->assertTrue(
  89. $this->lockFileExists($expectedFolder, 'LockCommand.php'),
  90. 'A lock file does not seem to have been created relative to the user\'s home directory '
  91. );
  92. $this->assertContains(
  93. 'Sorry, can\'t get the lock. Bailing out!',
  94. $commandTester->getDisplay(),
  95. 'The lock does not seem to have been acquired correctly as the same command was run twice at the same time '.
  96. 'without error'
  97. );
  98. $this->removeAllLockFiles($expectedFolder);
  99. }
  100. public function testGetAndSetLocking(){
  101. $command = $this->registerCommand(new HelloWorldCommand());
  102. EncapsulationViolator::invokeMethod($command, 'setLocking', array(false));
  103. $this->executeCommand($command);
  104. $this->assertFalse(
  105. EncapsulationViolator::invokeMethod($command, 'isLocking'),
  106. 'The locking value that we just set was not returned'
  107. );
  108. }
  109. public function testSetLockingViaParameter()
  110. {
  111. $command = $this->registerCommand(new HelloWorldCommand());
  112. $this->executeCommand($command, array('--locking'=>'off'));
  113. $this->assertFalse(
  114. EncapsulationViolator::invokeMethod($command, 'isLocking'),
  115. 'Locking was not turned off by parameter'
  116. );
  117. $command = $this->registerCommand(new HelloWorldCommand());
  118. $this->executeCommand($command, array('--locking'=>'on'));
  119. $this->assertTrue(
  120. EncapsulationViolator::invokeMethod($command, 'isLocking'),
  121. 'Locking was not turned on by parameter'
  122. );
  123. }
  124. /**
  125. * @expectedException \Afrihost\BaseCommandBundle\Exceptions\BaseCommandException
  126. * @expectedExceptionMessage Lock handler is already initialised
  127. */
  128. public function testSetLockingAfterInitializeException()
  129. {
  130. $command = $this->registerCommand(new HelloWorldCommand());
  131. $this->executeCommand($command);
  132. EncapsulationViolator::invokeMethod($command, 'setLocking', array(false));
  133. }
  134. /**
  135. * Deletes all files that look like they may have been created by the Symfony LockHandler that are in the provided
  136. * directory and its sub directories
  137. *
  138. * @param string $directory Where to look for the lock files
  139. * @param string $baseName The name of the lock passed to the LockHandler's constructor (supports wildcards)
  140. */
  141. protected function removeAllLockFiles($directory, $baseName = '*')
  142. {
  143. $fs = new Filesystem();
  144. if($fs->exists($directory)){
  145. $finder = new Finder();
  146. $lockFiles = $finder->in($directory)->name('sf.'.$baseName.'.*.lock')->files()->ignoreDotFiles(true);
  147. $fs->remove($lockFiles);
  148. }
  149. }
  150. /**
  151. * Confirms if a file exists in the provided directory that looks like is was generated by the Symfony LockHandler.
  152. * The search is not recursive.
  153. *
  154. * @param string $directory Where to look for the lock files
  155. * @param string $baseName he name of the lock passed to the LockHandler's constructor (supports wildcards)
  156. *
  157. * @return bool
  158. */
  159. protected function lockFileExists($directory, $baseName)
  160. {
  161. $fs = new Filesystem();
  162. if(!$fs->exists($directory)){
  163. return false;
  164. }
  165. $finder = new Finder();
  166. $lockFiles = $finder->in($directory)->name('sf.'.$baseName.'.*.lock')->files()->ignoreDotFiles(true)->depth('==0');
  167. return $fs->exists($lockFiles);
  168. }
  169. /**
  170. * @return string
  171. */
  172. protected static function getTestHomeLocation()
  173. {
  174. return self::$testHomeLocation;
  175. }
  176. }