AppserverIo\Appserver\ServletEngine\Servlets\DhtmlServlet::init PHP Method

init() public method

Initializes the servlet with the passed configuration.
public init ( AppserverIo\Psr\Servlet\ServletConfigInterface $servletConfig ) : void
$servletConfig AppserverIo\Psr\Servlet\ServletConfigInterface The configuration to initialize the servlet with
return void
    public function init(ServletConfigInterface $servletConfig)
    {
        // pre-initialize the X-POWERED-BY header
        $this->poweredBy = get_class($this);
        // pre-initialize the possible DHTML template paths
        $this->webappPath = $servletConfig->getWebappPath();
        $this->appBase = $servletConfig->getServletContext()->getAppBase();
        $this->baseDirectory = $servletConfig->getServletContext()->getBaseDirectory();
    }

Usage Example

 /**
  * Tests the servlets init() method.
  *
  * @return void
  */
 public function testInit()
 {
     // create a servlet config mock instance
     $mockServletConfig = $this->getMockBuilder($servletConfigInterface = 'AppserverIo\\Psr\\Servlet\\ServletConfigInterface')->setMethods(get_class_methods($servletConfigInterface))->getMock();
     // mock the necessary method
     $mockServletConfig->expects($this->once())->method('getWebappPath')->will($this->returnValue($webappPath = '/opt/appserver/webapps/test'));
     // create and initialize a servlet instance
     $servlet = new DhtmlServlet();
     $servlet->init($mockServletConfig);
     // check that the servlet has been initilized successfully
     $this->assertSame($webappPath, $servlet->getWebappPath());
     $this->assertSame(get_class($servlet), $servlet->getPoweredBy());
 }