Welcome Guest, Not a member yet? Register   Sign In
Spark Plug - Obtains an instance of CodeIgniter gracefully
#1

(This post was last modified: 03-20-2016, 09:34 PM by rougin.)

Hello! I've created a library that simplifies the process of getting CodeIgniter's instance. It can also be used to create a mock instance for testing your CodeIgniter applications to PHPUnit. https://github.com/rougin/spark-plug

Installation

You can install it via Composer:

Code:
$ composer require rougin/spark-plug

NOTE: composer_autoload must be enabled in the application/config/config.php.

Basic Usage

Getting the CodeIgniter's instance

PHP Code:
$ci Rougin\SparkPlug\Instance::create();

// You can now use its instance
$ci->load->helper('inflector'); 

As a mock instance for unit testing

PHP Code:
class SparkPlugTest extends \PHPUnit_Framework_TestCase
{
 
   /**
     * Checks if the CodeIgniter instance is successfully retrieved.
     * 
     * @return void
     */
 
   public function testCodeIgniterInstance()
 
   {
 
       // Path of your test application
 
       $appPath __DIR__ '/TestApp';

 
       // Instance::create($appPath, $_SERVER, $GLOBALS)
 
       $ci = \Rougin\SparkPlug\Instance::create($appPath);

 
       $this->assertInstanceOf('CI_Controller'$ci);
 
   }

Reply
#2

How is this more simple than just using:

Code:
$CI =& get_instance();

Is this just for people who do unit testing?
Reply
#3

This seems like it could be super useful for those of us that use different unit testing frameworks like Codeception. I'll let you know how it goes.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#4

Hello @skunkbad,

For me, the $CI =& get_instance(); can be used inside the CodeIgniter since its needs to go first in the index.php. But with the SparkPlug implementation, you can use it outside or even test libraries that uses the CodeIgniter core files with PHPUnit. You can also mock $GLOBALS and $_SERVER inside the Instance::create() class.
Reply
#5

(03-14-2016, 09:39 AM)rougin Wrote: Hello @skunkbad,

For me, the $CI =& get_instance(); can be used inside the CodeIgniter since its needs to go first in the index.php. But with the SparkPlug implementation, you can use it outside or even test libraries that uses the CodeIgniter core files with PHPUnit. You can also mock $GLOBALS and $_SERVER inside the Instance::create() class.

I like the idea of being able to use CI libraries outside of CI, especially the database classes. Actually, it's the database classes that are my favorite part of CI. Nobody else has a query builder that is so simple to use.
Reply
#6

That's also my another goal for this library, to be able to use and test its core libraries outside CodeIgniter. Big Grin I am currently doing a experimental extension of the Query Builder class with this library: https://github.com/rougin/wildfire. Using SparkPlug, I can test this library and can integrate the core files (Query Builder, inflector helper) with ease.
Reply
#7

Hello! Spark Plug is now v0.4.2! It fixes an issue when loading custom libraries. Smile
Reply
#8

(03-20-2016, 09:26 PM)rougin Wrote: Hello! Spark Plug is now v0.4.2! It fixes an issue when loading custom libraries. Smile

Thanks for writing this. It's very helpful for Phpunit testing or writing command line apps.
Reply
#9

Hi Rougin,

How to make this work for HMVC? I already try with different method but didn't work. Please help me.
Reply
#10

Hello @shcode,

Can I ask what kind of library did you use for HMVC? I would like to check it in order to get the problem.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB