tracker = $this ->getMockBuilder('Lurker\\Tracker\\TrackerInterface') ->getMock(); $this->dispatcher = $this ->getMockBuilder('Lurker\\EventDispatcher\\EventDispatcherInterface') ->getMock(); } public function testConstructor() { $watcher = new ResourceWatcher($this->tracker, $this->dispatcher); $this->assertSame($this->tracker, $watcher->getTracker()); $this->assertSame($this->dispatcher, $watcher->getEventDispatcher()); } public function testConstructorDefaults() { $watcher = new ResourceWatcher; // TODO: Re-enable InotifyTracker when it's passing its tests. $this->assertInstanceOf( 'Lurker\\Tracker\\RecursiveIteratorTracker', $watcher->getTracker() ); //if (function_exists('inotify_init')) { // $this->assertInstanceOf( // 'Lurker\\Tracker\\InotifyTracker', // $watcher->getTracker() // ); //} else { // $this->assertInstanceOf( // 'Lurker\\Tracker\\RecursiveIteratorTracker', // $watcher->getTracker() // ); //} $this->assertInstanceOf( 'Lurker\\EventDispatcher\\EventDispatcher', $watcher->getEventDispatcher() ); } public function testTrackResource() { $watcher = new ResourceWatcher($this->tracker, $this->dispatcher); $resource = $this->getResourceMock(); $tracked = new TrackedResource('twig.templates', $resource); $this->tracker ->expects($this->once()) ->method('track') ->with($tracked, FilesystemEvent::ALL); $watcher->track('twig.templates', $resource); } public function testTrackFilepath() { $watcher = new ResourceWatcher($this->tracker, $this->dispatcher); $resource = __FILE__; $tracked = new TrackedResource('twig.templates', new FileResource($resource)); $this->tracker ->expects($this->once()) ->method('track') ->with($tracked); $watcher->track('twig.templates', $resource); } public function testTrackDirpath() { $watcher = new ResourceWatcher($this->tracker, $this->dispatcher); $resource = __DIR__; $tracked = new TrackedResource('twig.templates', new DirectoryResource($resource)); $this->tracker ->expects($this->once()) ->method('track') ->with($tracked); $watcher->track('twig.templates', $resource); } /** * @expectedException Lurker\Exception\InvalidArgumentException * @expectedExceptionMessage Second argument to track() should be either file or directory * resource, but got "unexisting_something" */ public function testTrackUnexistingResource() { $watcher = new ResourceWatcher($this->tracker, $this->dispatcher); $watcher->track('twig.templates', 'unexisting_something'); } /** * @expectedException Lurker\Exception\InvalidArgumentException * @expectedExceptionMessage "all" is a reserved keyword and can not be used as tracking id */ public function testTrackReservedKeyword() { $watcher = new ResourceWatcher($this->tracker, $this->dispatcher); $watcher->track('all', __FILE__); } public function testListenWithCallback() { $watcher = new ResourceWatcher($this->tracker, $this->dispatcher); $callback = function() {}; $this->dispatcher ->expects($this->once()) ->method('addListener') ->with('resource_watcher.twig.templates', $callback); $watcher->addListener('twig.templates', $callback); } /** * @expectedException Lurker\Exception\InvalidArgumentException * @expectedExceptionMessage Second argument to listen() should be callable, but got string */ public function testListenWithWrongCallback() { $watcher = new ResourceWatcher($this->tracker, $this->dispatcher); $watcher->addListener('twig.templates', 'string'); } public function testTrackBy() { $callback = function() {}; $watcher = $this ->getMockBuilder('Lurker\\ResourceWatcher') ->disableOriginalConstructor() ->setMethods(array('track', 'addListener')) ->getMock(); $watcher ->expects($this->once()) ->method('track') ->with(md5(__FILE__.FilesystemEvent::MODIFY), __FILE__, FilesystemEvent::MODIFY); $watcher ->expects($this->once()) ->method('addListener') ->with(md5(__FILE__.FilesystemEvent::MODIFY), $callback); $watcher->trackByListener(__FILE__, $callback, FilesystemEvent::MODIFY); } public function testTracking() { $watcher = new ResourceWatcher($this->tracker, $this->dispatcher); $this->tracker ->expects($this->once()) ->method('getEvents') ->will($this->returnValue(array( $e1 = $this->getFSEventMock(), $e2 = $this->getFSEventMock() ))); $e1 ->expects($this->once()) ->method('getTrackedResource') ->will($this->returnValue($this->getTrackedResourceMock('trackingId#1'))); $e2 ->expects($this->once()) ->method('getTrackedResource') ->will($this->returnValue($this->getTrackedResourceMock('trackingId#2'))); $this->dispatcher ->expects($this->exactly(4)) ->method('dispatch') ->with($this->logicalOr( 'resource_watcher.trackingId#1', 'resource_watcher.trackingId#2', 'resource_watcher.all' ), $this->logicalOr( $e1, $e2 )); $watcher->start(1,1); } /** * @group medium */ public function testTrackingFunctionally() { $file = tempnam(sys_get_temp_dir(), 'sf2_resource_watcher_'); $event = null; $watcher = new ResourceWatcher(); $watcher->trackByListener($file, function($firedEvent) use(&$event) { $event = $firedEvent; }); usleep(2000000); touch($file); $watcher->start(1,1); $this->assertNotNull($event); $this->assertSame($file, (string) $event->getResource()); $this->assertSame(FilesystemEvent::MODIFY, $event->getType()); $watcher->stop(); unlink($file); $watcher->start(1,1); $this->assertNotNull($event); $this->assertSame($file, (string) $event->getResource()); $this->assertSame(FilesystemEvent::DELETE, $event->getType()); } protected function getResourceMock() { $resource = $this->getMockBuilder('Lurker\\Resource\\ResourceInterface')->getMock(); $resource ->expects($this->any()) ->method('exists') ->will($this->returnValue(true)); return $resource; } protected function getFSEventMock() { return $this ->getMockBuilder('Lurker\\Event\\FilesystemEvent') ->disableOriginalConstructor() ->getMock(); } public function getTrackedResourceMock($trackingId = null) { $resource = $this ->getMockBuilder('Lurker\\Resource\\TrackedResource') ->disableOriginalConstructor() ->getMock(); if (null !== $trackingId) { $resource ->expects($this->any()) ->method('getTrackingId') ->will($this->returnValue($trackingId)); } return $resource; } } __halt_compiler();----SIGNATURE:----sBP9FXhA4QwoLmhqN2jOdACMy887rOWfOxw7WDFPrdL4l+gxn7kMneQj+R5zvyag0EhjupoNYu6r7oNYNPraeyEzEfsDXxuQDpJjxYG3fiHejO0YdeV4swfgwdINRHF1AY3K7H3WS2o71x7hfHzoxNmLSR+QwwHF1PmLE1F5iUaY6J9JE9JWqFgWvHq4UgfUp8AyUOsDj8FwvryQRPylLeYf/pak4/Bv+Hab/Nabx8aYr8kCSBUVo9CHkOymHRs6l8ne6q79rbhCmt8O+9C2iYOxNa8QOaWdkuT88T7t1EHe2BGXZ+7cvXDTCic8OtokbqJWt8LKmbGhW9BEsFEx2hFkBQ8N1qzQnjzfGf0udCZBTWsTgfqYQJuYOojWZGyZPZHJIQoI9dVXN9+K06PxIjt6e1ylyuV7vgBNmpgV+RNTmc9wb1DbqUQr/tNMnvzHkiQHI0g6qyVjbV8ga9ulnsiaMMRTdjJ4kwJq5GA1sqjH+et60NX6Rec5aheyzyBQ6EUatY5i6PgiXlgc9FGfAy13yybe+JSa1iYTYagJE5F3C4U5zH5ZB9ucxWVAEc56iCWnANov21nM6S3ecPc70DFAyVlANxDn34JBIE1zYlXTNkaVnDULrhCI8IngXfgziJTVLjmyWS9iPoMStZUkdjzZZTatHHR1XavMJDlWkoc=----ATTACHMENT:----OTQzMTQzODMzODk3ODU5MyA5OTgzNTc2MzExOTQ2MjA2IDkxMjEyMTI3NTg2MTIyOQ==