EventTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Script;
  12. use Composer\Composer;
  13. use Composer\Config;
  14. use Composer\Script\Event;
  15. use Composer\Test\TestCase;
  16. class EventTest extends TestCase
  17. {
  18. public function testEventSetsOriginatingEvent()
  19. {
  20. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  21. $composer = $this->createComposerInstance();
  22. $originatingEvent = new \Composer\EventDispatcher\Event('originatingEvent');
  23. $scriptEvent = new Event('test', $composer, $io, true);
  24. $this->assertNull(
  25. $scriptEvent->getOriginatingEvent(),
  26. 'originatingEvent is initialized as null'
  27. );
  28. $scriptEvent->setOriginatingEvent($originatingEvent);
  29. $this->assertSame(
  30. $originatingEvent,
  31. $scriptEvent->getOriginatingEvent(),
  32. 'getOriginatingEvent() SHOULD return test event'
  33. );
  34. }
  35. public function testEventCalculatesNestedOriginatingEvent()
  36. {
  37. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  38. $composer = $this->createComposerInstance();
  39. $originatingEvent = new \Composer\EventDispatcher\Event('upperOriginatingEvent');
  40. $intermediateEvent = new Event('intermediate', $composer, $io, true);
  41. $intermediateEvent->setOriginatingEvent($originatingEvent);
  42. $scriptEvent = new Event('test', $composer, $io, true);
  43. $scriptEvent->setOriginatingEvent($intermediateEvent);
  44. $this->assertNotSame(
  45. $intermediateEvent,
  46. $scriptEvent->getOriginatingEvent(),
  47. 'getOriginatingEvent() SHOULD NOT return intermediate events'
  48. );
  49. $this->assertSame(
  50. $originatingEvent,
  51. $scriptEvent->getOriginatingEvent(),
  52. 'getOriginatingEvent() SHOULD return upper-most event'
  53. );
  54. }
  55. private function createComposerInstance()
  56. {
  57. $composer = new Composer;
  58. $config = new Config;
  59. $composer->setConfig($config);
  60. $package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
  61. $composer->setPackage($package);
  62. return $composer;
  63. }
  64. }