Giter Club home page Giter Club logo

Comments (5)

luckydonald avatar luckydonald commented on June 3, 2024

I try to mock the mail loaded in a controller.

class Whatever_controller extends CI_Controller {
  function index() {
    $this->load->library('email');

    $this->email->clear()
      ->from('[email protected]')
      ->to('[email protected]')
      ->subject('Test')
      ->send();
  }
}
class Test extends TestCase {
  protected function setUp() {
    $this->resetInstance();
    $this->CI->load->database();
    // Mock email
    $this->CI->email = new Mock_Libraries_Email();
    $this->mock_mail = &$this->CI->email;
  }
  function test_foo() {
    $output = $this->request('POST', 'whatever/index);
  }
}

from ci-app-for-ci-phpunit-test.

luckydonald avatar luckydonald commented on June 3, 2024

However the controller is loading the stock email, which will send normally,
and $this->mock_mail is still unchanged (->_get_data() is empty array).

from ci-app-for-ci-phpunit-test.

luckydonald avatar luckydonald commented on June 3, 2024

With

protected function setUp() {
		$this->resetInstance();
		$this->CI->load->database();
		$email = new Mock_Libraries_Email();
		$this->request->setCallable(
			function ($CI) use ($email){
				$CI->email = $email;
			}
		);
		$this->mock_mail = $email;
		parent::setUp();
	}

I can install it to the actual running request controller instance but will get the error because of an isinstance check:
Resource 'email' already exists and is not a CI_Email instance.

from ci-app-for-ci-phpunit-test.

luckydonald avatar luckydonald commented on June 3, 2024

So with

class Mock_Libraries_Email extends CI_Email {

I can work around that, but now have inherited many stuff from the original.

from ci-app-for-ci-phpunit-test.

luckydonald avatar luckydonald commented on June 3, 2024

Some brainstorming:

class Mock_Libraries_Email extends CI_Email {
	const MOCK_OF = CI_Email::class;

/tests/_ci_phpunit_test/replacing/core/Loader.php

// function _ci_init_library

		if (isset($CI->$object_name))
		{
+			if ($CI->$object_name::MOCK_OF == $class_name)
+			{
+				log_message('debug', $object_name." has already been instantiated as mock of ".$class_name.'". Second attempt aborted.");
+				return;
+			}
			if ($CI->$object_name instanceof $class_name)
			{
				log_message('debug', $class_name." has already been instantiated as '".$object_name."'. Second attempt aborted.");
				return;
			}

			show_error("Resource '".$object_name."' already exists and is not a ".$class_name." instance.");
		}

from ci-app-for-ci-phpunit-test.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.