Include generic testcase in codeception - php

I have multiple similar websites that I want to test. Here's the current approach:
Clone an existing codeception project
Adjust the variables
Add some additional specific test cases if required
This works just fine, but has one issue. If the general test case changes that means that I have to go into every project and make the changes. So what I would rather see is a way to include a generic top level test case in every project and call it with a parameter.
I tried so with a simple include() statement, but unfortunately this doesn work. And ideas how I can accomplish this?
Here some code of my initial try:
/home/tests/site1/tests/acceptance/isOnlineCept.php
include("../1generic_tests/isonline.php");
$I = new AcceptanceTester($scenario);
$I->am('user');
$I->wantTo('see the content of the site');
$I->lookForwardTo('see the homepage');
$I->amOnPage('/');
$I->see("Tech, Geek and Rock'n'Roll");
The file I include looks like this
$I = new AcceptanceTester($scenario);
$I->am('user');
$I->wantTo('see the content of the site');
$I->lookForwardTo('see the homepage');
$I->amOnPage('/');
$I->see('something else');
Unfortunately it fails with this error
[PHPUnit_Framework_Exception] Undefined index: class
Maybe include is not the right way to do it, but what would be a better way?

You can use Page Object for this.
Create one Constants.php file using Page Object where you can define all the selectors.
Create one other file where you can keep all common operation going to be performed. E.g. Settings.php file where you can have all methods as per your requirement. you can define it with parameter too.
To access any selector and/or method from declared under page dir can be accessible by using the following line.
use Page\Constants as ConstantsPage;
To access any selector from any specific file
FileNamePage::$selectorName;
To access any method, first create the object and then call the method.
E.g.
$settings = new SettingsPage( $I )
$settings->methodName();
To call the method and/pr selector in the same file.
self::selectorName;
self::methodName();
Hope this will be helpful.

Related

Unable to access variable from base .php called by required php

assume i have a code as below:
index.php
<?php
require 'file_1.php';
// file_1.php contents can go here, but i'm trying to make this simple and clutter free, thus clean.
// The reason being to not centralize all code in a single file should file_2.php, file_3.php, file_n.php exist.
$app = new type_of_object();
$app->intialize_some_stuff();
?>
file_1.php
<?php
$app->access_some_method($param1, new function($app) {
// do some logic here...
});
?>
doing this approach, i get an error such as below:
Notice: Undefined variable: app in X:\project_folder\file_1.php on line 2
to summarize, i'm trying to decentralize all codes from being defined in index.php and instead, have them hosted by different files such as file_1.php to file_n.php. The problem is, i'd be needing to access the $app variable from index.php on the required files such as file_1.php to file_n.php since i'll only need 1 instance of this, no duplicates allowed.
i'm also open to being taught another approach on this.
cheers!
EDIT: its worth nothing that i'm fairly new in PHP, and that i've already done few research. except that what i'm getting is the opposite of my issue. also, the need to access $app of index.php from other php files is the very center of my question.
Splitting code can be a good idea to achieve a better maintainability. You should avoid unclear scripts name.
at a high degree, you want definitely go on composer approach to maintain packages.
Anyway, for a basic code splitting, the rules are the same than a regular script. Code is interpreted at the time it is read. Just rewrite your example in one script :
<?php
$app->access_some_method($param1, new function($app) {
// do some logic here...
});
$app = new type_of_object();
$app->intialize_some_stuff();
?>
See the problem ? $app is simply not initialized when calling the access_some_method.
So, this will work :
<?php
$app = new type_of_object();
$app->intialize_some_stuff();
require 'file_1.php';
?>
But the gain is poor. If you already have a class, there's a few cases where it can be interesting to split logic among many files. I think this one is a better example (all files in the same folder, no namespaces) :
<?php
require('App');
require('AppRequest');
$app = new App();
$request = new AppRequest();
$app->process($request);
With this way, you can split the main app logic and the request analysis logic for instance

Joomla 3! Module Parameters

I have a Joomla website where I have a custom module with mod_myModuleName.php and mod_myModuleName.xml files and a folder where there are several PHP scripts that add special functionality to my module. There is a config.php file in the folder that holds an associative array with variables and their values hard-coded. The module works just fine.
What I want though is to provide administrator area for the values of the variables in the array, so that I can put values in administrator panel and get their values in config.php. In my mod_myModuleName.php I use <?php echo $params->get('param')?> and it works like a charm.
But when I try to use the same technique in the config.php it breaks my code. I tried to get the values in mod_myModuleName.php and then include it in config.php and use the variables but it does not work either. I have not got so much experience in php and cannot understand what can be the reason.
It sometimes gives me an error of something non object and I guess it must be something connected with object oriented php, am I right? And if so is there a way to overcome this without object orientation or how can I solve my problem?
The problem will be with the way you're using your config.php.
When your modules entry point file mod_myModuleName.php is loaded by Joomla the $params object is already available in that context, you need to provide it to your scripts.
If you look at something like the mod_articles_latest module you will notice that the helper class is included with this line:
require_once __DIR__ . '/helper.php';
And then helper class is has it's getList() method called statically with the $params passed into it, so that $params is available to class context:
$list = ModArticlesLatestHelper::getList($params);
Inside the helper class ModArticlesLatestHelper you will notice that the getList() expects the $params to be passed in.
public static function getList(&$params)
{
...
}
I would strongly recommend reading the articles in the Modules section of Developers Portal on the Joomla Doc's.
Try the "Creating a simple module" article.

PHP Function Scope Failure

I am struggling to understand scope and what's preventing my new code from working (assuming it is a scope issue).
The following function is in a file PATH.'/includes/custom-functions.php' that references a class:
function infusion() {
require_once(PATH.'/classes/infusion.php'); //PATH is defined in WordPress from ~/wp-content/themes/theme/
return new infusion();
}
The class is reliant on PATH.'/api/isdk.php' and connection credentials from another file within /api/ directory. From within PATH .'/includes/custom-functions.php', I have many other functions that call $infusion = infusion(); and work perfectly.
PROBLEM
I have created a new file: PATH.'/includes/report.php' which I need to access $infusion = infusion();but can't get to work by either repeating the function infusion() definition from above; using require_once();; or using include();. All 3 of those options simply kill the rest of the code and I can only come to the conclusion - well, I have no conclusion.
Any help would be greatly appreciated.
I'm assuming the code isn't using namespaces, therefore you aren't permitted to redeclare the infusion function (either by redefining the function, or re-including the class).
Your includes/report.php file should simply have:
require_once PATH.'/includes/custom-functions.php';
// your other code here ...
$infusion = infusion();
It may be the case that other files / classes that you're including in your file are already requiring custom-functions.php along the line, so you may be able to skip that entirely. Also note that the PATH constant should have already been defined somewhere (either directly or via an included file) before you attempt to use it. If you set your error_reporting to include E_ALL, you'll get a notification in your error log if that constant doesn't exist.
If that fails, your error log(s) may provide some additional background on what your issue is.

When I move code into a function, I get a Fatal error. How can I call code from function

There are two pieces to this code:
One that adds documents to an index to be searched, which works fine, and a crawl() function that is a web-crawler that gets the contents of a page, which also works fine.
But, I need to add a document from inside the crawl() function.
When I move the code that adds a document inside the crawl() function, I get a Fatal Error:
Fatal Error: call to member function addDocument() on a non-object.
I am wondering how I can access the member function addDocument() from inside the crawl function?
Right now, I have a working version where the crawl() function returns what it has crawled in the form of a variable and then the addDocument code, outside the crawl() function, also has access to the returned variable and adds the document to the index that way.
But, that only (logically) works when I am crawling one page or a page with no links to follow. As the function only returns when it is done and since it is recursive to follow a page's links, the only content it will return is the content of the last-page.
Where I need the content of each page to be added each as a new document in the index.
Here is the working code, described above, commented as much as I could: http://pastebin.com/5ngcucDp
and here is the non-working code where I try to move the addDocument() inside the crawl() function: http://pastebin.com/mUEwQJTG
If you have a solution that involves how to access the addDocument() function from inside the crawl() function, then please share.
Or if you have a solution that involves modifying the working code so that it returns the contents of each page it crawls instead of the last-page, please share.
If you have any solutions, please share as I am absolutely exhausted and have tried everything I know.
When moving code to a function, you are completely removing its ability to access variables in the same scope. In this case, you probably (not going to go looking through your off-site code) have something like $someObject = new myClass();, then are trying to access $someObject->addDocument() on it from within the function.
You need to pass $someObject as a parameter to the function, or you could use global $someObject inside the function, though it's not as good an idea.
You have specified that:
// The below line is where the error takes place.
$elasticaType->addDocument($document);
Is your error line. Now, PHP is trying to access a class linked to $elasticaType If you have a linked class then use:
$elasticaType = new ClassName();
If not then you should create a class:
class Name {
public function addDocument ($document){
//Add document code
return $somevar;
}
}
$elasticaType = new Name();
$elasticaType->addDocument($document);

How to include a codeigniter project into another php file?

I have a project based on codeigniter. And I should use one class that extended from a codeigniter controller in another php file. But I didn't find the solution about how to teach another php file to see whole CI-project. Beyond that needed class can not inherit when i call it from other place.
I'm not 100% sure if this helps get you in the right direction, but kudos if it does!
Codeigniter routes the application depending on the environment state of the URI. What you need to do is set the environment and include the index view file like so:
$_SERVER["REQUEST_URI"] = "cms/2";
//Set GET action,method params etc
require_once "path/to/index.php";
When you load CI Index file it reads the SERVER Variable and others which you may have to find and execute the controller and method, I would also advise that you modify the library/view file as it may exit upon output causing your script to exit.
Also you may wis hto look into ob_start() to catch the buffer.

Categories