Im writing some tests with Yii2 and Codeception and I'm getting a weird error when using my own class in the _before() function:
FATAL ERROR. TESTS NOT FINISHED.
Class 'app\lib\FTPImage' not found in /var/www/proyect/tests/codeception/unit/lib/FTPImageTest.php:13
It is strange because if I use it in a test method it works like charm :/
Here is the code of the test:
<?php
namespace tests\codeception\unit\lib;
use Yii;
use yii\codeception\TestCase;
use app\lib\FTPImage;
class FTPImageTest extends TestCase{
public $ftp;
public function _before(){
$this->ftp = new FTPImage();
}
public function _after(){
}
public function testGetImagesNoImages(){
$ftp = new FTPImage();
$images = $ftp->getImages("123", "Foobar");
$this->assertEmpty($images);
}
}
And this is the code of the FTPImage class:
<?php
namespace app\lib;
use Yii;
use app\lib\FTPClient;
use \yii\base\Exception;
use \yii\base\ErrorException;
use \yii\imagine\Image;
class FTPImage{
public function __construct() {
}
public function getImages($reference, $supplier_name){
...
}
I hope there's someone who can help me
Thanks in advance!
Finally I found the solution. I've changed to _before and _after functions to setUp and tearDown. This way, I can use my FTPImage class.
Here is what I did:
class FTPImageTest extends TestCase{
public $ftp;
public function setUp(){
parent::setUp();
$this->ftp = new FTPImage();
}
public function tearDown(){
parent::tearDown();
}
...
}
Related
I try to fetch data from database in codeigniter construct function
but getting undefined method error
controller-
class Welcome extends CI_Controller
function __construct()
{
parent::__construct();
$this->load->model('Login');
$this->Login->getadminnav();
}
}
model-
class Login extends CI_Model
{
public function adminnav()
{
$query="SELECT * from adminnav where status='1'";
$query->row_array();
return true;
}
}
You got to load the model you are using first:
$this->load->model('login_model');
$this->Login->getadminnav();
And remember to rename the model to Login_model
Try these pieces of code.
Controller:
class Welcome extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('Login');
$this->Login->adminnav();
}
}
Model:
class Login extends CI_Model{
public function adminnav(){
$this->load->database();
$sql = "SELECT * from adminnav where status='1'";
$result = $this->db->query($sql);
return $result;
}
}
I am seeing in your code that-
In controller you didn't start the second bracket after controller name.
You defined the function name as "adminnav" in model but called it as "getadminnav" in Controller
You didn't load the database in model (If you already loaded it in autoload.php then here is not needed)
Anyway please let me know whether your problem is solved or not.
you are calling getadminnav() but in your model the method name is adminnav.and change model name Login to Login_model.
try this:
class Welcome extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('Login_model');
$this->Login->adminnav();
}
}
// model
class Login_model extends CI_Model{
public function adminnav()
{
$query="SELECT * from adminnav where status='1'";
$query->row_array();
return true;
}
}
i made controller name crud.php this the code :
class Crud extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('m_data');
$this->load->helper('url');
}
public function admin(){
$data['produk'] = $this->m_data->tampil_data()->result();
$this->load->view('v_admin',$data);
}
}
i want take the admin() to place in admin.php code here :
class Admin extends CI_Controller{
function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
function index(){
$this->crud->admin();
}
}
what's code for take another function in another controller.. that's possible or not?
You could try to require Crud controller within Admin controller :
public function index()
{
require_once('crud.php');
$crud = new crud();
$crud->admin();
}
I do not know if I understood your question very well, but you could have the controller Admin extend the Crud controller, so he would have access to the admin ()
class Admin extends Crud{
function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
function index(){
$this->admin();
}
}
Loading another controller inside a controller is a bad idea. It can lead to hard to find bugs. One solution is to make Crud a custom library that the controller loads.
File: /application/libraries/Crud.php
class Crud
{
protected $CI;
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->model('m_data');
$this->CI->load->helper('url');
}
public function admin()
{
$data['produk'] = $this->CI->m_data->tampil_data()->result();
$this->CI->load->view('v_admin', $data);
}
}
Then the controller loads and uses the custom class.
class Admin extends CI_Controller{
public function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
public function index()
{
$this->load->library('crud');
$this->crud->admin();
}
}
You could load crud in the constructor too. That would make sense if other methods in the controller use the library.
May it can be useful for you, if you will customize your code in the view:
VIEW
your-link-to-another-controller
This is first class with "gordz()" function
public class Model extends Object implements IModel
{
public static function gordz() : void
{
newobject = gallas.pop();
}
}
Now i try to override the function but i still want that old code is executed... How can i extend this function correctly?
public class LOL extends Model
{
override public static function gordz() : void
{
... //New code + execute old code
}
}
Neither super
You cannot use the super statement in a static method.
nor override
You cannot use the override attribute on any of the following:
[...]
Static methods
can be used in a static method.
Whatever you are trying to do should be accomplished in a different way.
Hey everybody) I need help with the understanding of the framework Cairngorm for flex.
I code a simple application slider. I have a main class in which I have a tag
<fx:Declarations>
<control:AppController id="appController" />
</fx:Declarations>
Class itself AppController extends FrontController with constructor function:
public function AppController()
{
addCommand(SliderEvent.BUILD, SliderBuildCommand);
addCommand(SliderEvent.TRANSITION, SliderTransitionCommand);
}
and finally the code of SliderBuildCommand class:
public function SliderBuildCommand(){}
public function execute(event:CairngormEvent):void
{
config.loadSlides(this);
}
Unfortunately, in debugging, I see that the addition of the command didn't work out. This can be seen if set a breakpoint in "сonfig.loadSlides(this)" line. However, the command (addCommand) is processed.
Any idea why this is happening? Maybe I am a noob and I don't see the obvious :)
Instead of adding/mapping commands in AppController's constructor. Declare it in initialize() function. Example is given below for your reference.
public class AppController extends FrontController
{
public function AppController()
{
super();
}
public function initialize():void
{
this.addCommand(SliderEvent.BUILD, SliderBuildCommand);
this.addCommand(SliderEvent.TRANSITION, SliderTransitionCommand);
}
}
I have a function called wsGO() inside a class ceff(). I want to be able to run that function from within the class ceff. I used this code:
class ceff{
$ceff_instance = new ceff($this);
$ceff_instance = wsGO();
public function wsGO(){
....
}
}
However this didn't work. Any ideas?
In PHP you can only initialize a property with a constant - NOT functions or constructor calls. You can though do that in a constructor, such as:
class ceff{
public $ceff_instance;
public function __construct() {
$this->ceff_instance = $this->wsGO();
}
public function wsGO(){
....
}
}