I am creating a new component for Joomla 3.2. In my previous component for an older version of Joomla I did not have any unit tests as Joomla was very tightly coupled and complicated.
It is possible to do Test Driven Development with Joomla 3.2?
So far the below PHPUnit bootstrap is working for me.
<?php
//TODO: Change this to match your path
$pathToJoomla = "/Applications/MAMP/htdocs/joomla32";
if (!is_dir(realpath($pathToJoomla))) {
throw new Exception("Could not find the folder: $pathToJoomla");
}
//Fake some required variables
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '';
//start
define('_JEXEC', 1);
// Fix magic quotes.
ini_set('magic_quotes_runtime', 0);
// Maximise error reporting.
ini_set('zend.ze1_compatibility_mode', '0');
error_reporting(E_ALL & ~E_STRICT);
ini_set('display_errors', 1);
if (!defined('JPATH_PLATFORM'))
{
define('JPATH_PLATFORM', realpath($pathToJoomla . '/libraries'));
}
if (!defined('JPATH_LIBRARIES'))
{
define('JPATH_LIBRARIES', realpath($pathToJoomla . '/libraries'));
}
if (!defined('JPATH_BASE'))
{
define('JPATH_BASE', realpath($pathToJoomla));
}
if (!defined('JPATH_ROOT'))
{
define('JPATH_ROOT', realpath(JPATH_BASE));
}
if (!defined('JPATH_CACHE'))
{
define('JPATH_CACHE', JPATH_BASE . '/cache');
}
if (!defined('JPATH_CONFIGURATION'))
{
define('JPATH_CONFIGURATION', JPATH_BASE);
}
if (!defined('JPATH_SITE'))
{
define('JPATH_SITE', JPATH_ROOT);
}
if (!defined('JPATH_ADMINISTRATOR'))
{
define('JPATH_ADMINISTRATOR', JPATH_ROOT . '/administrator');
}
if (!defined('JPATH_INSTALLATION'))
{
define('JPATH_INSTALLATION', JPATH_ROOT . '/installation');
}
if (!defined('JPATH_MANIFESTS'))
{
define('JPATH_MANIFESTS', JPATH_ADMINISTRATOR . '/manifests');
}
if (!defined('JPATH_PLUGINS'))
{
define('JPATH_PLUGINS', JPATH_BASE . '/plugins');
}
if (!defined('JPATH_THEMES'))
{
define('JPATH_THEMES', JPATH_BASE . '/templates');
}
if (!defined('JDEBUG'))
{
define('JDEBUG', false);
}
// Import the platform in legacy mode.
require_once JPATH_PLATFORM . '/import.legacy.php';
// Force library to be in JError legacy mode
JError::setErrorHandling(E_NOTICE, 'message');
JError::setErrorHandling(E_WARNING, 'message');
// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';
//TODO: you will need to adjust this to match your paths
$srcPath = realpath("../../src");
JTable::addIncludePath($srcPath.'/site/tables');
require_once $srcPath."/site/models/base.php";
A few more details can be found on my blog post.
Related
Would this be the best way to use the native php sessions instead of using their drivers? Since their drivers are just wrappers around session functions; I figured this would be the easiest way.
The reason behind this is I am having issues with locking with mysql database driver and redis driver. I want to use native php sessions and swap in a redis driver at the php level.
This seems to work well. Any thoughts?
<?php
class MY_Session extends CI_Session
{
public function __construct(array $params = array())
{
$CI =& get_instance();
// No sessions under CLI
if (is_cli())
{
log_message('debug', 'Session: Initialization under CLI aborted.');
return;
}
elseif ((bool) ini_get('session.auto_start'))
{
log_message('error', 'Session: session.auto_start is enabled in php.ini. Aborting.');
return;
}
// Configuration ...
$this->_configure($params);
$this->_config['_sid_regexp'] = $this->_sid_regexp;
// Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
if (isset($_COOKIE[$this->_config['cookie_name']])
&& (
! is_string($_COOKIE[$this->_config['cookie_name']])
OR ! preg_match('#\A'.$this->_sid_regexp.'\z#', $_COOKIE[$this->_config['cookie_name']])
)
)
{
unset($_COOKIE[$this->_config['cookie_name']]);
}
session_start();
// Is session ID auto-regeneration configured? (ignoring ajax requests)
if ((empty($_SERVER['HTTP_X_REQUESTED_WITH']) OR strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
&& ($regenerate_time = config_item('sess_time_to_update')) > 0
)
{
if ( ! isset($_SESSION['__ci_last_regenerate']))
{
$_SESSION['__ci_last_regenerate'] = time();
}
elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time))
{
$this->sess_regenerate((bool) config_item('sess_regenerate_destroy'));
}
}
// Another work-around ... PHP doesn't seem to send the session cookie
// unless it is being currently created or regenerated
elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
{
setcookie(
$this->_config['cookie_name'],
session_id(),
(empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']),
$this->_config['cookie_path'],
$this->_config['cookie_domain'],
$this->_config['cookie_secure'],
TRUE
);
}
$this->_ci_init_vars();
}
}
error in exporting to pdf. i tried composer update and adding stuffs to app.php anf composer.json
public function exportPDF($request, $orgid)
{
/*$pdf = App::make('snappy.pdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->inline();*/
$data = User::get()->toArray();
return Excel::create('itsolutionstuff_example', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->fromArray($data);
});
})->download("pdf");
}
Wich pdf renderer have you set up? Acording to the documentation, you have 3 choices, tcpdf, mPdf and DomPDF.
You have to install at least one of them.
composer require mpdf/mpdf
and then direct PHPExcel to use it
$renderer = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererPath = dirname(__FILE__).'/../../../libraries/PDF/mPDF6.1'
if (!PHPExcel_Settings::setPdfRenderer(
$renderer,
$rendererPath
)) {
die('Wrong setup of PDF libraries');
}
Please comment this line in DomPDF.php
if (file_exists($pdfRendererClassFile)) {
require_once $pdfRendererClassFile;
} else {
throw new PHPExcel_Writer_Exception('Unable to load PDF Rendering library');
}
to
/** Require DomPDF library */
//$pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/dompdf_config.inc.php';
//if (file_exists($pdfRendererClassFile)) {
// require_once $pdfRendererClassFile;
//} else {
// throw new PHPExcel_Writer_Exception('Unable to load PDF Rendering library');
//}
Hope it will works.
I have a plugin for joomla 2.5
I want install it in joomla 3 but after installing I see this error :
Fatal error: Call to undefined method JController::getInstance() in /home/xxx/public_html/administrator/components/xxx/xxx.php on line 21
Line of error:
// Require specific controller if requested
if($controller = JRequest::getCmd('controller'))
{
$path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';
if(file_exists($path))
{
require_once $path;
}
else
{
$controller = '';
}
}
JController::getInstance() has been removed. Use JControllerLegacy::getInstance() instead.
Sitemap stopped generating suddenly.
Started displaying 'Unable to create XML sitemap.'
Please help
A quick fix to it .
Here I am using fishpig extension .
Observed that fishpig is overridding magento core sitemap controller .
So added the following code : /app/code/community/Fishpig/Wordpress/controllers/Adminhtml/SitemapController.php
Add this code before :
$this->_redirectReferer();
$id = $this->getRequest()->getParam('sitemap_id');
$sitemap = Mage::getModel('sitemap/sitemap');
/* #var $sitemap Mage_Sitemap_Model_Sitemap */
$sitemap->load($id);
// if sitemap record exists
if ($sitemap->getId()) {
try {
$sitemap->generateXml();
$this->_getSession()->addSuccess(
Mage::helper('sitemap')->__('The sitemap "%s" has been generated.', $sitemap->getSitemapFilename()));
}
catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
catch (Exception $e) {
$this->_getSession()->addException($e,
Mage::helper('sitemap')->__('Unable to generate the sitemap.'));
}
} else {
$this->_getSession()->addError(
Mage::helper('sitemap')->__('Unable to find a sitemap to generate.'));
}
Hope it helps someone experiencing with same issue .
Courtesy : lukecollymore
I am trying to get the new PDO driver running in Code Igniter 2.1.1 in (to start with) the local (Mac OS 10.7) copy of my app.
I initially coded it using Active Record for all db operations, and I am now thinking I want to use PDO prepared statements in my model files, going forward.
I modified 'application/config/database.php' like so:
(note a couple minor embedded questions)
[snip]
$active_group = 'local_dev';
$active_record = TRUE;//<---BTW, will this need to stay TRUE to make CI sessions work? For better security, don't we want db-based CI sessions to use PDO too?
//http://codeigniter.com/user_guide/database/configuration.html:
//Note: that some CodeIgniter classes such as Sessions require Active Records be enabled to access certain functionality.
//this is the config setting that I am guessing (?) is my main problem:
$db['local_dev']['hostname'] = 'localhost:/tmp/mysql.sock';
// 1.) if $db['local_dev']['dbdriver']='mysql', then here ^^^ 'localhost:/tmp/mysql.sock' works, 2.) but if $db['local_dev']['dbdriver']='pdo', then it fails with error msg. shown below.
$db['local_dev']['username'] = 'root';
$db['local_dev']['password'] = '';
$db['local_dev']['database'] = 'mydbname';
$db['local_dev']['dbdriver'] = 'pdo';
$db['local_dev']['dbprefix'] = '';
$db['local_dev']['pconnect'] = TRUE;
$db['local_dev']['db_debug'] = TRUE;//TRUE
$db['local_dev']['cache_on'] = FALSE;
$db['local_dev']['cachedir'] = '';
$db['local_dev']['char_set'] = 'utf8';
$db['local_dev']['dbcollat'] = 'utf8_general_ci';
$db['local_dev']['swap_pre'] = '';
$db['local_dev']['autoinit'] = TRUE;
$db['local_dev']['stricton'] = FALSE;
[snip]
With the above config., as soon as I load a controller, I get this error message:
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in
/Library/WebServer/Documents/system/database/drivers/pdo/pdo_driver.php:114 Stack trace: #0
/Library/WebServer/Documents/system/database/drivers/pdo/pdo_driver.php(114): PDO->__construct('localhost:/tmp/...', 'root', '', Array) #1 /Library/WebServer/Documents/system/database/DB_driver.php(115): CI_DB_pdo_driver->db_pconnect() #2
/Library/WebServer/Documents/system/database/DB.php(148): CI_DB_driver->initialize() #3
/Library/WebServer/Documents/system/core/Loader.php(346): DB('', NULL) #4
/Library/WebServer/Documents/system/core/Loader.php(1171): CI_Loader->database() #5
/Library/WebServer/Documents/system/core/Loader.php(152): CI_Loader->_ci_autoloader() #6
/Library/WebServer/Documents/system/core/Con in
/Library/WebServer/Documents/system/database/drivers/pdo/pdo_driver.php on line 114
I tried swapping out the 'pdo_driver.php' file from the one on github, as per this:
http://codeigniter.com/forums/viewthread/206124/
...but that just generates other errors, not to mention is disturbing to a newbie who does not want to touch the system files if at all possible.
This thread also seems to imply the need to be hacking the 'pdo_driver.php' system file:
CodeIgniter PDO database driver not working
It seems odd to me, though, that (someone thought that) a hack to a system file is needed to make PDO work in CI v.2.1.1, huh?
Thanks for any suggestions I can try.
I don't know if this might be helpful for you since you already started using the CI functions, but I made my own library for PDO with sqlite and just auto load it. My needs were simple, so it serves its purpose.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter PDO Library
*
*
* #author Michael Cruz
* #version 1.0
*/
class Sqlite_pdo
{
var $DB;
public function connect($path) {
try {
$this->DB = new PDO('sqlite:' . $path);
}
catch(PDOException $e) {
print "Error: " . $e->getMessage();
die();
}
}
public function simple_query($SQL) {
$results = $this->DB->query($SQL)
or die('SQL Error: ' . print_r($this->DB->errorInfo()));
return $results;
}
public function prepared_query($SQL, $bind = array()) {
$q = $this->DB->prepare($SQL)
or die('Prepare Error: ' . print_r($this->DB->errorInfo()));
$q->execute($bind)
or die('Execute Error: ' . print_r($this->DB->errorInfo()));
$q->setFetchMode(PDO::FETCH_BOTH);
return $q;
}
public function my_prepare($SQL) {
$q = $this->DB->prepare($SQL)
or die('Error: ' . print_r($this->DB->errorInfo()));
return $q;
}
public function my_execute($q, $bind) {
$q->execute($bind)
or die('Error: ' . print_r($this->DB->errorInfo()));
$q->setFetchMode(PDO::FETCH_BOTH);
return $q;
}
public function last_insert_id() {
return $this->DB->lastInsertId();
}
}
/* End of file Sqlite_pdo.php */
thanks to the noob thread http://codeigniter.com/forums/viewthread/180277/ (InsiteFX’s answer)..
I figured out the below seems to work (need to test more to be 100%... but at least the error messages are gone:
$db['local_dev']['hostname'] = 'mysql:host=127.0.0.1';