Joomla 2.5 - component fatal error [duplicate] - joomla2.5

This question already exists:
imade a simple component aboutus with table contactus when i click on publish/unpublish get this errore
Closed 7 years ago.
When I make component in Joomla 2.5 I had this error:
Fatal error: Call to a member function reset() on a non-object in
/var/www/html/joomla/libraries/joomla/application/component/modeladmin.php
on line 850

I found‌ :when click on publish or unpublish first run this controller quite in my project this name is :
notic:run first this file
com_contactus/controllers/categories.php
<?php
defined( '_JEXEC' ) or die();
jimport ('joomla.application.component.controlleradmin');
echo "run first this file ";
class contactusControllerCategories extends JControllerAdmin
{
public function getModel($name='Category',$prefix='contactusModel',$config=array('ignore_request'=>true))
{
$model = parent::getModel($name,$prefix,$config);
return $model;
}
}
after run that Top file rund this file Of course gettable method
com_contactus/models/category.php
<?php
defined( '_JEXEC' ) or die();
jimport ('joomla.application.component.modeladmin');
class contactusModelCategory extends JModelAdmin
{
public function getTable($type='Category',$prefix='contactusTable',$config=array())
{
echo "second ";
return JTable::getInstance ($type,$prefix,$config);
}
protected function loadFormData()
{
$data = JFactory::getApplication()->getUserState('com_contactus.edit.category.data',array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
function getForm($data=array(),$loadData=true)
{
$form = $this->loadForm('com_contactus.category','Category',array('control'=>'jform' , 'load_data'=>$loadData));
return $form;
}
}
after that run towice files rund this file for run
com_contactus/tables/category.php
<?php
defined( '_JEXEC' ) or die();
jimport ('joomla.database.table');
echo "three ";
die();
class contactusTableCategory extends JTable
{
public function __construct (&$db)
{
parent::__construct('#__contactus','id',$db);
}
}
one of my wrong:
. I put the wrong tabels instead tables name in project

Related

How to write PHPunit test code with external object (having DAO)?

I'm trying to write test code for monosilic code like below.
Q1. How to write test code which has access to DB?
Q2. How to refactor these code testable?
Q3. Is there any way to write test code with fewer change to production code?
I need your help!
Thanks!!
Example Production Code)
<?
class Sample_Model_Service_A
{
private $_result
private $_options
private $_someValue
public function __construct($params, $ids, $data) {
$this->_options = Sample_Model_Service_B::getOption($data);
}
private function setSomeValue() {
// some code shaping $_params to $someValue with $this->_options
$this->_someValue= $someValue;
}
// want to write test for this function
// changed this function's logic
private function setResult() {
// some code shaping $_someValue to $result
$this->_result = $result;
}
public function getter() {
retrn $this->_result;
}
}
?>
<?
class Sample_Model_Service_B
{
// get option from DB
public static function getOption($data) {
$dao = new Model_Dao_Option();
$option = $dao->getOption($data['id']);
return $option;
}
}
?>
My Test Code so far)
public function testsetResult()
{
// just make sure these variables are defined
$params = $ids = $data = [];
// try to make test for private function
$sample = new Sample_Model_Service_A($params, $ids, $data);
$reflection = new ReflectionClass($sample);
// get Method
$method = $reflection->getMethod('setresult');
$method->setAccessible(true);
// wondering how to get $result
$result = $method->invoke($sample);
// assert
$this->assertSame($result);
}
Mockery solved my issue.
Sample Code)
/**
* #dataProvider sampleProvider
*/
public function testsetResult($sampleData)
{
// mock Sample_Model_Service_B
$mockSample_Model_Service_B = Mockery::mock('alias:' . Sample_Model_Service_B::class);
$mockSample_Model_Service_B->shouldReceive('getOption')->andReturn($sampleData['option']);
$sample = new Sample_Model_Service_A($sampleData['params'], $sampleData['ids'], $sampleData['data']);
$sample->setResult();
$result = $sample->getter();
// assert
$this->assertSame($result, $sampleData['result']);
}

mock function called, but expects($this->once()) fails

I'm testing a Symfony command to send reminder Text messages. For this I have created a service for my text message interface and am mocking the container as well as the text messaging service:
The function under test
protected function textReminders()
{
$mailer = $this->getContainer()->get('mailer');
$em = $this->getContainer()->get( 'doctrine' )->getManager();
if ($this->getContainer()->get('kernel')->getEnvironment() == 'dev'){
$debug = true;
}else{
$debug = false;
}
$textMessage = $this->getContainer()->get('text_messaging.interface');
$textMessage->sendSMS( $target, $content, $debug);
}
Test
private function getMockContainer()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock();
return $container;
}
protected function setupMocks()
{
$mockText = $this->getMockBuilder('TextaHQ')
->disableOriginalConstructor()
->setMethods(array('sendSMS'))
->getMock();
$mockContainer = $this->getMockContainer();
$container = self::$kernel->getContainer();
$mockContainer->method('get')
->withConsecutive(['mailer'], ['doctrine'], ['kernel'], ['text_messaging.interface'])
->willReturnOnConsecutiveCalls(
$this->returnValue($container->get('mailer')),
$this->returnValue($container->get('doctrine')),
$this->returnValue(self::$kernel),
$this->returnValue($mockText))
;
$this->setMyMock([
'text' => $mockText,
'container' => $mockContainer
]);
}
public function testExecute()
{
$this->setupMocks();
self::bootKernel();
$application = new Application(self::$kernel);
$application->add(new ActionRemindCommand());
$command = $application->find( 'ahp:remind' );
$command->setContainer($this->getMyMock()['container']);
$commandTester = new CommandTester( $command );
$commandTester->execute( array(
'command' => $command->getName(),
'type' => 'text'
) );
$output = $commandTester->getDisplay();
$this->assertions();
}
protected function assertions()
{
$this->getMyMock()['text']
->expects( $this->once() )
->method( 'sendSMS' )
;
}
Updated test, all in one file
public function testExecute()
{
$insertSql = 'echo "'
. str_replace(
array('"' ,'`' ),
array('\\"' ,'\\`'),
$this->getPrepSql() )
. '" | mysql ahp_example_com';
exec($insertSql);
self::bootKernel();
$mockText = $this->getMockBuilder('TextaHQ')
->disableOriginalConstructor()
->setMethods(array('sendSMS'))
->getMock();
$mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock();
$container = self::$kernel->getContainer();
$mockContainer->method('get')
->withConsecutive(['mailer'], ['doctrine'], ['kernel'], ['text_messaging.interface'])
->willReturnOnConsecutiveCalls(
$this->returnValue($container->get('mailer')),
$this->returnValue($container->get('doctrine')),
$this->returnValue(self::$kernel),
$this->returnValue($mockText))
;
$application = new Application(self::$kernel);
$application->add(new ActionRemindCommand());
$mailer = self::$kernel->getContainer()->get('swiftmailer.mailer');
$logger = new \Swift_Plugins_MessageLogger();
$mailer->registerPlugin( $logger );
$this->setMailCollector($logger);
$output = '';
for($i=1;$i<=$this->getRunNoTimes();$i++) {
$command = $application->find( 'ahp:remind' );
$command->setContainer($mockContainer);
$commandTester = new CommandTester( $command );
$commandTester->execute( array(
'command' => $command->getName(),
'type' => 'text'
) );
$output .= $commandTester->getDisplay();
}
$mockText
->expects( $this->once() )
->method( 'sendSMS' )
;
}
**PHPStorm Test call **
/usr/bin/php /home/jochen/projects/ahp/trunk/vendor/phpunit/phpunit/phpunit --configuration /home/jochen/projects/ahp/trunk/app/phpunit.xml.dist AgriHealth\AhpBundle\Tests\Command\ActionRemindCommandVet7DaysBeforeTest /home/jochen/projects/ahp/trunk/src/AgriHealth/AhpBundle/Tests/Command/RemindText/ActionRemindCommandVet7DaysBeforeTest.php --teamcity
Testing started at 10:25 AM ...
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.
Expectation failed for method name is equal to <string:sendSMS> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
Time: 1.12 seconds, Memory: 18.00MB
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Process finished with exit code 1
When I debug the test, I can see that $textMessage is a mock.
However at the end of the test in assertions(), I get an error:
Expectation failed for method name is equal to <string:sendsms> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
The debugger shows the mocked function as lower case: "sendsms", but renaming the function did not help.
here is a small illustartive case to clarify the points about I've mentioned in comments. Also it has a failing test for the situation when expectations are set after actuall call for the method was performed (which looks to be the case of your all-In-One-Method update test.
class MyClass
{
public function someMethod(){
}
}
class ExpectationsTest extends PHPUnit_Framework_TestCase
{
private $myClassMock;
public function setUp(){
$this->myClassMock = $this->getMock('MyClass');
}
public function testLocalMock(){
$localMock = $this->getMock('MyClass');
$localMock->expects($this->once())
->method('someMethod');
$localMock->someMethod();
}
public function testClassScopeMockInstance(){
$this->myClassMock->expects($this->once())
->method('someMethod');
$this->myClassMock->someMethod();
}
public function testWillFailBecauseExpectationWasSetAfterCall(){
$this->myClassMock->someMethod();
$this->myClassMock->expects($this->once())
->method('someMethod');
}
public function testCanUseHelperToCreateLocalMock(){
$mock = $this->createMyClassMock();
$mock->expects($this->once())
->method('someMethod');
$mock->someMethod();
}
private function createMyClassMock(){
return $this->getMock('MyClass');
}
public function testCanSetExpectationsInHelper(){
$this->setExpecatationsOnTestCaseScopeMock();
$this->myClassMock->someMethod();
}
private function setExpecatationsOnTestCaseScopeMock(){
$this->myClassMock->expects($this->once())
->method('someMethod');
}
}
btw I think I did not explore you code thoroughly enough for the first time. I think I might missassumed about how your setupMocks and getMyMock were intended to work. Sorry about that.

Symfony3, Doctrine2, custom fixtures loader

I am trying to make a custom fixture that will take data from a csv file, parse it in a specific way, create the objects, insert the data and flush it to the database.
My problem is that my file lives in AppBundle/Command and I do not know how to access the manager in order to persist and flush my file.
So my question is: How do i access doctrine's manager?
<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use AppBundle\Entity\Country;
class LoadFixturesCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('app:load-fixtures'));
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// file path
$csvfile = '/home/amarui/Documents/studyin/country.csv';
// if opened
if (($handle = fopen($csvfile, 'r')) !== false) {
// read line and breakes at semicolon(;)
while (($line = fgetcsv($handle, 1000, ';', '"')) !== false) {
$counter = 0;
$i = 0; // counter for the array elements
$city{$counter} = new Country();
// read elements from breakpoint(;)
foreach ($line as $value) {
// if the element has a comma(,)
if (strstr($line[$i], ',')) {
// breaks at comma(,)
$line[$i] = explode(',', $line[$i]);
// reads elements from the breakpoint(,)
foreach ($line[$i] as $multiple) {
// echo '<br /> count'.$i.' '.$multiple;
// TODO:
}
} else {
// echo '<br /> count'.$i.' '.$value;
if ($i = 0) {
$city{$counter}->setName($value);
}
}
$i += 1; // next element in the array
$counter += 1; // updates the variable name
$this->getDoctrine()->getManager()->persist($city{$counter});
}
}
}
$this->getDoctrine()->getManager()->flush();
}
}
Here is the error that it outputs
[Symfony\Component\Debug\Exception\FatalThrowableError]
Fatal error: Call to undefined method AppBundle\Comm
and\LoadFixturesCommand::getDoctrine()
Stack trace
[2016-02-29 11:55:44] php.CRITICAL: Fatal error: Call to undefined method AppBundle\Command\LoadFixturesCommand::getDoctrine() {"type":1,"file":"/home/amarui/Dev/student360/src/AppBundle/Command/LoadFixturesCommand.php","line":60,"level":32767,"stack":[{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php","line":256,"function":"execute","class":"AppBundle\\Command\\LoadFixturesCommand","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php","line":803,"function":"run","class":"Symfony\\Component\\Console\\Command\\Command","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php","line":186,"function":"doRunCommand","class":"Symfony\\Component\\Console\\Application","type":"->","args":["[object] (AppBundle\\Command\\LoadFixturesCommand: {})","[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php","line":86,"function":"doRun","class":"Symfony\\Component\\Console\\Application","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php","line":117,"function":"doRun","class":"Symfony\\Bundle\\FrameworkBundle\\Console\\Application","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/bin/console","line":29,"function":"run","class":"Symfony\\Component\\Console\\Application","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')"]}]}
You haven't the helper method getDoctrine() in a command (instead this exists in a controller if inhered from Symfony\Bundle\FrameworkBundle\Controller\Controller).
try with
$this->getContainer()->get('doctrine')
instead of
$this->getDoctrine()
Hope this help

SimpleTester on CodeIgniter fails with "Class 'GroupTest' not found"

I'm trying to do a clean install SimpleTester on a new CodeIgniter application, following the instructions here: http://codeigniter.com/wiki/SimpleTester_-_Unit_testing_library
Everything's fine until step 6, when I add "simpletester" to the list of libraries that are autoloaded. As soon as I do that, visiting any page simply results in:
Fatal error: Class 'GroupTest' not found in
/path/to/app/application/libraries/simpletester.php on line 84
Grepping through the code for GroupTest I only see it referenced in comments, and in a readme file which states the following:
The GroupTest has been renamed TestSuite (see below).
It was removed completely in 1.1 in favour of this
name.
I tried modifying line 84 to replace GroupTest with TestSuite, but then I get the following error:
Fatal error: Call to undefined method TestSuite::addTestFile() in
/home/path/to/app/application/libraries/simpletester.php
on line 96
Is this a bug on their end? Has anyone seen this before?
I have run into the same issue. The GroupTest class can be found in test_case.php of version 1.0.1 of SimpleTest:
http://sourceforge.net/projects/simpletest/files/simpletest/simpletest_1.0.1/
Unfortunately, simply inserting v1.0.1 into the libraries folder doesn’t solve all the world’s problems. I no longer get the “Fatal error: Class ‘GroupTest’ not found ...” error, but I do get a segmentation fault and my site no longer works.
I have briefly tried to track down the issue but to no avail.
Note: I also responded on the CodeIgniter Wiki page containing the same question.
I had the same problem with a current project and found that the problem is that GroupTest was replaced with TestSuite which works a little differently.
This is the library code I use:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
$libraryDir = APPPATH . 'libraries/simpletest';
if(!is_dir($libraryDir))
exit("Simpletest must be located in \"$libraryDir\"");
require_once $libraryDir . '/unit_tester.php';
require_once $libraryDir . '/mock_objects.php';
require_once $libraryDir . '/collector.php';
class SimpleTester
{
/**
* What reporter should be used for display.
* Could be either HtmlReporter, SmallReporter, MinimalReporter or ShowPasses.
*/
public $Reporter = 'MinimalReporter';
private $testDir;
private $testTitle;
private $fileExtension;
public function __construct($params = false)
{
$ci =& get_instance();
$ci->config->load('simpletester');
if($params == false) {
$params['runFromIPs'] = $ci->config->item('runFromIPs');
$params['testDir'] = $ci->config->item('testDir');
$params['fileExtension'] = $ci->config->item('fileExtension');
$params['autorun'] = $ci->config->item('autorun');
$params['reporter'] = $ci->config->item('reporter');
$params['testTitle'] = $ci->config->item('testTitle');
}
if(isset($params['runFromIPs']) && strpos($params['runFromIPs'], $ci->input->server('SERVER_ADDR') === FALSE))
{
// Tests won't be run automatically from this IP.
$params['autorun'] = FALSE;
}
// Check if call was an AJAX call. No point in running test
// if not seen and may break the call.
$header = 'CONTENT_TYPE';
if(!empty($_SERVER[$header])) {
// #todo Content types could be placed in config.
$ajaxContentTypes = array('application/x-www-form-urlencoded', 'multipart/form-data');
foreach ($ajaxContentTypes as $ajaxContentType) {
if(false !== stripos($_SERVER[$header], $ajaxContentType))
{
$params['autorun'] = FALSE;
break;
}
}
}
$this->testDir = $params['testDir'];
$this->testTitle = $params['testTitle'];
$this->fileExtension = $params['fileExtension'];
if(isset($params['reporter']))
$this->Reporter = $params['reporter'];
if($params['autorun'] == TRUE)
echo $this->Run();
}
/**
* Run the tests, returning the reporter output.
*/
public function Run()
{
// Save superglobals that might be tested.
if(isset($_SESSION)) $oldsession = $_SESSION;
$oldrequest = $_REQUEST;
$oldpost = $_POST;
$oldget = $_GET;
$oldfiles = $_FILES;
$oldcookie = $_COOKIE;
$test_suite = new TestSuite($this->testTitle);
// Add files in tests_dir
if(is_dir($this->testDir))
{
if($dh = opendir($this->testDir))
{
while(($file = readdir($dh)) !== FALSE)
{
// Test if file ends with php, then include it.
if(substr($file, -(strlen($this->fileExtension)+1)) == '.' . $this->fileExtension)
{
$test_suite->addFile($this->testDir . "/$file");
}
}
closedir($dh);
}
}
// Start the tests
ob_start();
$test_suite->run(new $this->Reporter);
$output_buffer = ob_get_clean();
// Restore superglobals
if(isset($oldsession)) $_SESSION = $oldsession;
$_REQUEST = $oldrequest;
$_POST = $oldpost;
$_GET = $oldget;
$_FILES = $oldfiles;
$_COOKIE = $oldcookie;
return $output_buffer;
}
}
// Html output reporter classes //////////////////////////////////////
/**
* Display passes
*/
class ShowPasses extends HtmlReporter
{
function ShowPasses()
{
$this->HtmlReporter();
}
function paintPass($message)
{
parent::paintPass($message);
print "<span class=\"pass\">Pass</span>: ";
$breadcrumb = $this->getTestList();
array_shift($breadcrumb);
print implode("->", $breadcrumb);
print "->$message<br />\n";
}
function _getCss()
{
return parent::_getCss() . ' .pass {color:green;}';
}
}
/**
* Displays a tiny div in upper right corner when ok
*/
class SmallReporter extends HtmlReporter
{
var $test_name;
function ShowPasses()
{
$this->HtmlReporter();
}
function paintHeader($test_name)
{
$this->test_name = $test_name;
}
function paintFooter($test_name)
{
if($this->getFailCount() + $this->getExceptionCount() == 0)
{
$text = $this->getPassCount() . " tests ok";
print "<div style=\"background-color:#F5FFA8; text-align:center; right:10px; top:30px; border:2px solid green; z-index:10; position:absolute;\">$text</div>";
}
else
{
parent::paintFooter($test_name);
print "</div>";
}
}
function paintFail($message)
{
static $header = FALSE;
if(!$header)
{
$this->newPaintHeader();
$header = TRUE;
}
parent::paintFail($message);
}
function newPaintHeader()
{
$this->sendNoCacheHeaders();
print "<style type=\"text/css\">\n";
print $this->_getCss() . "\n";
print "</style>\n";
print "<h1 style=\"background-color:red; color:white;\">$this->test_name</h1>\n";
print "<div style=\"background-color:#FBFBF0;\">";
flush();
}
}
/**
* Minimal only displays on error
*/
class MinimalReporter extends SmallReporter
{
function paintFooter($test_name)
{
if($this->getFailCount() + $this->getExceptionCount() != 0)
{
parent::paintFooter($test_name);
print "</div>";
}
}
}
Works fine for me I haven't tested all the different reporters yet though. But the default one works fine.
And this is how I use it:
$this->load->library('simpletester');
echo $this->simpletester->Run();
And my config file is:
$config['testDir'] = APPPATH . 'tests';
$config['runFromIPs'] = '127.0.0.1';
$config['reporter'] = 'HtmlReporter';
$config['autorun'] = false;
$config['fileExtension'] = 'php';
$config['testTitle'] = 'My Unit Tests';

Indentation when using File Template to create new PHP Class in Netbeans

I'm probably missing something, but I have the following issue:
I'm using Netbeans IDE 7.0.
I've created a template for new PHP Classes, but when I try to create a new class, the template I've created is inserted without any indentation.
Example:
Template:
<?php
/**
* Description of ${name}
*
* #author ${user}
*/
include_once("class_functions.php");
class ${name} {
//Class properties
function __construct() {
//Constructor
}
public function __get($name) {
if(method_exists($this, 'get' . ucfirst($name))) {
return $this->{'get' . ucfirst($name)};
} else {
if (property_exists($this, $name)) {
$this->{$name} = $value;
} else {
throw new Exception("Undefined property '$name'.");
}
}
}
}
But when I use this template, the new class is created like:
<?php
/**
* Description of ${name}
*
* #author ${user}
*/
include_once("class_functions.php");
class ${name} {
//Class properties
function __construct() {
//Constructor
}
public function __get($name) {
if(method_exists($this, 'get' . ucfirst($name))) {
return $this->{'get' . ucfirst($name)};
} else {
if (property_exists($this, $name)) {
$this->{$name} = $value;
} else {
throw new Exception("Undefined property '$name'.");
}
}
}
}
Does anyone know what I'm doing wrong?
Any help is greatly appreciated! Thanks in advance.
Seems if you save your template using tabs it will strip them. Save your templating with spaces used for indenting they will be filtered into tabs or left as spaces per your netbeans preferences.
You may also have syntax issues that will sometimes through the render off.
Also as I said before, alt + shit + f is your friend in netbeans (auto format)
Workaround:
Create your OWN template file and add it to Netbeans File Templates.
All the indentations magically starts working...
File templates dialog -> Add