injectMock example

$request = $this->setRequestBody($request);

        // Initialize the RouteCollection         if ($routes = $this->routes) {
            $routes = Services::routes()->loadRoutes();
        }

        $routes->setHTTPVerb($method);

        // Make sure any other classes that might call the request         // instance get the right one.         Services::injectMock('request', $request);

        // Make sure filters are reset between tests         Services::injectMock('filters', Services::filters(null, false));

        $response = $this->app
            ->setContext('web')
            ->setRequest($request)
            ->run($routes, true);

        $output = \ob_get_contents();
        if (empty($response->getBody()) && ! empty($output)) {
            
$request = $this->setRequestBody($request$params);

        // Initialize the RouteCollection         if ($routes = $this->routes) {
            $routes = Services::routes()->loadRoutes();
        }

        $routes->setHTTPVerb($method);

        // Make sure any other classes that might call the request         // instance get the right one.         Services::injectMock('request', $request);

        // Make sure filters are reset between tests         Services::injectMock('filters', Services::filters(null, false));

        // Make sure validation is reset between tests         Services::injectMock('validation', Services::validation(null, false));

        $response = $this->app
            ->setContext('web')
            ->setRequest($request)
            ->run($routes, true);

        

        return Factories::models($name['getShared' => $getShared]$connection);
    }

    /** * Helper method for injecting mock instances while testing. * * @param object $instance */
    public static function injectMock(string $name$instance)
    {
        Factories::injectMock('models', $name$instance);
    }

    /** * Resets the static arrays */
    public static function reset()
    {
        Factories::reset('models');
    }
}
if (empty($this->appConfig)) {
            $this->appConfig = config(App::class);
        }

        if ($this->uri instanceof URI) {
            $this->uri = Services::uri($this->appConfig->baseURL ?? 'http://example.com/', false);
        }

        if (empty($this->request)) {
            // Do some acrobatics so we can use the Request service with our own URI             $tempUri = Services::uri();
            Services::injectMock('uri', $this->uri);

            $this->withRequest(Services::request($this->appConfig, false)->setBody($this->body));

            // Restore the URI service             Services::injectMock('uri', $tempUri);
        }

        if (empty($this->response)) {
            $this->response = Services::response($this->appConfig, false);
        }

        
$this->appConfig = config(App::class);
        }

        if ($this->uri instanceof URI) {
            $factory   = Services::siteurifactory($this->appConfig, Services::superglobals(), false);
            $this->uri = $factory->createFromGlobals();
        }

        if (empty($this->request)) {
            // Do some acrobatics so we can use the Request service with our own URI             $tempUri = Services::uri();
            Services::injectMock('uri', $this->uri);

            $this->withRequest(Services::request($this->appConfig, false));

            // Restore the URI service             Services::injectMock('uri', $tempUri);
        }

        if (empty($this->response)) {
            $this->response = Services::response($this->appConfig, false);
        }

        

        $mockClass   = $className::$mockClass;
        $mockService = $className::$mockServiceName ?? '';

        if (empty($mockClass) || ! class_exists($mockClass)) {
            throw TestException::forInvalidMockClass($mockClass);
        }

        $mock = new $mockClass();

        if (empty($mockService)) {
            Services::injectMock($mockService$mock);
        }

        return $mock;
    }
}

    protected function resetServices(bool $initAutoloader = true)
    {
        Services::reset($initAutoloader);
    }

    /** * Injects the mock Cache driver to prevent filesystem collisions */
    protected function mockCache()
    {
        Services::injectMock('cache', new MockCache());
    }

    /** * Injects the mock email driver so no emails really send */
    protected function mockEmail()
    {
        Services::injectMock('email', new MockEmail(config(Email::class)));
    }

    /** * Injects the mock session driver into Services */


    /** * Helper method for injecting mock instances while testing. * * @param object $instance * * @return void */
    public static function injectMock(string $name$instance)
    {
        Factories::injectMock('config', $name$instance);
    }

    /** * Resets the static arrays * * @return void */
    public static function reset()
    {
        Factories::reset('config');
    }
}
Home | Imprint | This part of the site doesn't use cookies.