Comments (5)
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.
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.
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.
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.
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)
- My mistake
- update the composer config HOT 4
- my-codeigniter.sh -> composer update failed HOT 1
- Testing controller method which has no output but returns HOT 2
- How to i use the inside tests/models file ? i got an error. HOT 1
- Faild to open tream: No such file or directory in ~\ci-app-for-ci-phpunit-test\application\tests\Bootstrap.php on line 298
- Issue with REST test HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ci-app-for-ci-phpunit-test.