PHP Classes

File: test/SchedulerPluginsTest.php

Recommend this page to a friend!
  Classes of Artur Graniszewski   ZEUS for PHP   test/SchedulerPluginsTest.php   Download  
File: test/SchedulerPluginsTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: ZEUS for PHP
Manage the execution of multiple parallel tasks
Author: By
Last change: Merge pull request #54 from piotrmacha/fix/mac_os_process_title

Disable ProcessTitle plugin on Darwin (macOS)
Date: 6 years ago
Size: 5,295 bytes
 

Contents

Class file image Download
<?php

namespace ZeusTest;

use
PHPUnit_Framework_TestCase;
use
Zeus\Kernel\ProcessManager\Plugin\DropPrivileges;
use
Zeus\Kernel\ProcessManager\SchedulerEvent;
use
ZeusTest\Helpers\ZeusFactories;

class
SchedulerPluginsTest extends PHPUnit_Framework_TestCase
{
    use
ZeusFactories;

   
/**
     * @param mixed[] $plugin
     * @return \Zeus\Kernel\ProcessManager\Scheduler
     */
   
protected function getSchedulerWithPlugin(array $plugin)
    {
       
$sm = $this->getServiceManager(
            [
               
'zeus_process_manager' => [
                   
'schedulers' => [
                       
'test_scheduler_1' => [
                           
'plugins' => $plugin
                       
]
                    ]
                ]
            ]
        );

        return
$this->getScheduler(0, null, $sm);
    }

   
/**
     * @return \PHPUnit_Framework_MockObject_MockObject|DropPrivileges
     */
   
protected function getDropPrivilegesMock()
    {
       
$pluginBuilder = $this->getMockBuilder(DropPrivileges::class);
       
$pluginBuilder->setMethods([
           
'posixSetEuid',
           
'posixSetUid',
           
'posixSetEgid',
           
'posixSetGid'
       
]);

       
$pluginBuilder->disableOriginalConstructor();
       
$plugin = $pluginBuilder->getMock();

        return
$plugin;
    }

   
/**
     * @expectedException \Zend\ServiceManager\Exception\ServiceNotCreatedException
     * @expectedExceptionMessageRegExp ~Failed to switch to the group ID~
     */
   
public function testDropPrivilegesPluginFailureWhenNotSudoer()
    {
       
$this->getSchedulerWithPlugin([
                \
Zeus\Kernel\ProcessManager\Plugin\DropPrivileges::class => [
                   
'user' => 'root',
                   
'group' => 'root',
                ]
            ]
        );
    }

    public function
testDropPrivilegesPluginWhenSudoer()
    {
       
$event = new SchedulerEvent();
       
$event->setName(SchedulerEvent::EVENT_PROCESS_INIT);

       
$plugin = $this->getDropPrivilegesMock();
       
$plugin->expects($this->atLeastOnce())->method("posixSetUid")->will($this->returnValue(true));
       
$plugin->expects($this->any())->method("posixSetEuid")->will($this->returnValue(true));
       
$plugin->expects($this->atLeastOnce())->method("posixSetGid")->will($this->returnValue(true));
       
$plugin->expects($this->any())->method("posixSetEgid")->will($this->returnValue(true));
       
$scheduler = $this->getSchedulerWithPlugin([$plugin]);
       
$event->setScheduler($scheduler);
       
$plugin->__construct(['user' => 'root', 'group' => 'root']);
       
$scheduler->getEventManager()->attach(SchedulerEvent::EVENT_PROCESS_INIT, function(SchedulerEvent $event) {
           
$event->stopPropagation(true); // block process main loop
       
}, SchedulerEvent::PRIORITY_FINALIZE + 1);
       
$scheduler->getEventManager()->triggerEvent($event);
    }

    public function
testSchedulerDestructor()
    {
       
$event = new SchedulerEvent();
       
$event->setName(SchedulerEvent::EVENT_PROCESS_INIT);

       
$plugin = $this->getDropPrivilegesMock();
       
$plugin->expects($this->atLeastOnce())->method("posixSetUid")->will($this->returnValue(true));
       
$plugin->expects($this->any())->method("posixSetEuid")->will($this->returnValue(true));
       
$plugin->expects($this->atLeastOnce())->method("posixSetGid")->will($this->returnValue(true));
       
$plugin->expects($this->any())->method("posixSetEgid")->will($this->returnValue(true));
       
$scheduler = $this->getSchedulerWithPlugin([$plugin]);
       
$event->setScheduler($scheduler);
       
$plugin->__construct(['user' => 'root', 'group' => 'root']);
       
$scheduler->getEventManager()->attach(SchedulerEvent::EVENT_PROCESS_INIT, function(SchedulerEvent $event) {
           
$event->stopPropagation(true); // block process main loop
       
}, SchedulerEvent::PRIORITY_FINALIZE + 1);

       
$scheduler->getEventManager()->triggerEvent($event);
       
$this->assertEquals(2, count($scheduler->getPluginRegistry()), 'Two plugins should be registered');
       
$scheduler->__destruct();
       
$this->assertEquals(1, count($scheduler->getPluginRegistry()), 'No plugin should be registered after Scheduler destruction');
    }

   
/**
     * @expectedException \RuntimeException
     * @expectedExceptionMessageRegExp ~Failed to switch to the group ID~
     */
   
public function testDropPrivilegesPluginWhenEffectiveSudoerButNotRealSudoer()
    {
       
$event = new SchedulerEvent();
       
$event->setName(SchedulerEvent::EVENT_PROCESS_INIT);

       
$plugin = $this->getDropPrivilegesMock();

       
$plugin->expects($this->never())->method("posixSetUid")->will($this->returnValue(false));
       
$plugin->expects($this->exactly(2))->method("posixSetEuid")->will($this->returnValue(true));
       
$plugin->expects($this->atLeastOnce())->method("posixSetGid")->will($this->returnValue(false));
       
$plugin->expects($this->exactly(2))->method("posixSetEgid")->will($this->returnValue(true));
       
$scheduler = $this->getSchedulerWithPlugin([$plugin]);
       
$event->setScheduler($scheduler);
       
$plugin->__construct(['user' => 'root', 'group' => 'root']);
       
$scheduler->getEventManager()->triggerEvent($event);
    }
}