Cake 2.0 CLI mode error "Undefined HTTP_HOST " - shell

I am Running a shell script on cake console. Able to run HelloShell But getting error undefined HTTP_host on app/config/core.php for following code:
class HelloShell extends AppShell {
public $uses=array('Test');
public function main() {
$this->out('Hello Kundan.');
}
public function hey() {
$this->Test->save($data['Test']['name']='Kundan');
}
}
bellow the error message:
PHP notice: undefined index HTTP_HOST in
C:\wamp\www\myapplication\app\Config\core.php on line 231.
When I opened that file I found following on line number 231 Configure::write('Website_root',$_SERVER['HTTP_HOST']);

I have replicate your code, and it run well.
where do you put HelloShell.php file? and how do you run your code?.
The common way, put your HelloShell.php file in app/Console/Command/ directory and run it by Console/cake hello
also try to modify your code:
class HelloShell extends AppShell {
public $uses=array('Test');
public function main() {
$this->out('Hello Kundan.');
$this->hey();
}
public function hey() {
$data['Test']['name']='Kundan';
$this->Test->save($data);
}
}

Related

Why I can't inject a class in a test case with dusk

I am testing my laravel project with dusk.
I am trying to inject a dependency in a test like that :
....
class PasswordLostTest extends DuskTestCase
{
private $passwordResetRepository;
public function __construct(PasswordResetRepository $passwordResetRepository)
{
$this->passwordResetRepository = $passwordResetRepository;
}
.....
When I run the test with php artisan dusk .\tests\Browser\PasswordLostTest.php, I have this error :
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Tests\Browser\PasswordLostTest::__construct(), 0 passed in D:\Workspace\classified-ads-mpa\vendor\phpunit\phpunit\src\Framework\TestBuilder.php on line 138 and exactly 1 expected in D:\Workspace\classified-ads-mpa\tests\Browser\PasswordLostTest.php:20
What is the problem ? Is it because I am in a test script ?
TestCases are not instantiated by the container. Secondly your app is not bootstrapped before setup has been called, the constructor is called first. Making your example technically impossible. Instead use resolve() or app()->make(), in the setup method to do what you want.
Your app is bootstrapped to the parent test cases setup, so calling parent::setUp(); is essential.
public function setUp()
{
parent::setUp();
$this->passwordResetRepository = resolve(PasswordResetRepository::class);
}
Here is the solution :
public function setUp(): void
{
parent::setUp();
$this->passwordResetRepository = resolve(PasswordResetRepository::class);
}
WARNING : the void is mandatory. That's why it did not work !
Many thanks to mrhn who guided me for this problem !

folder structure for scraping in Laravel, using Goutte

I am a bit confused about my folder structure for the scraping code. Using console/commands, not the controller. So, in the handle function I am writing the whole scraping code. But should I suppose to do that? Or... what is the best approach for this?
UPDATED
If I understand correctly the answer below. It should look like this right now.
calling services
class siteControl extends Command
{
protected $signature = 'bot:scrape {website_id}';
protected $description = 'target a portal site and scrape';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$website_id = $this->argument("website_id");
if ($website_id == 1) {
$portal = "App\Services\Site1";
}
$crawler = new $portal;
$crawler->run();
}
}
in handle method
class Site1 extends Utility
{
public function __construct()
{
parent::__construct();
}
public function run()
{
echo "method runs";
}
}
abstract:
use Goutte\Client;
abstract class Utility implements SiteInterfaces
{
protected $client;
public function __construct()
{
$this->client = new Client();
}
}
interfaces:
namespace App\Services;
interface SiteInterfaces
{
public function run();
}
and finally, I should write the whole scraping code inside the run() method? Please correct me If wrong about this... I am searching the best solution.
A best practice would be to call a separate service from your command handle() method. That way you could reuse that same service in a controller for instance.
The technical version:
Your application is given a specific thing to do (a command if you will). This command comes from outside of your application, which can be a anything from a web controller, to an API controller or a CLI application. In terms of hexagonal architecture this is called a port.
Once the application receives such a command it should not care which port it came from. By handling all similar commands in a single spot (a command handler) it does not have to worry about the origins of the command.
So to give you a short overview:
[Web request] [CLI command] <-- these are ports
\ /
\ /
\ /
[Command] <--- this is a method call to your service
|
|
|
[Command handler] <--- this is the service doing the actual work
Updated my answer
Based on the code you provided I implemented what I mentioned above like so:
app/Console/Command/BotScrapeCommand.php
This is the CLI command I mentioned above. All this class has to do is:
1. Gather input arguments; (website_id) in this case
2. Wrap those arguments in a command
3. Fire off the command using the command handler
namespace App\Console\Commands;
use App\Command\ScrapePortalSiteCommand;
use CommandHandler\ScrapePortalSiteCommandHandler;
class BotScrapeCommand extends Command
{
protected $signature = 'bot:scrape {website_id}';
protected $description = 'target a portal site and scrape';
public function handle(ScrapePortalSiteCommandHandler $handler)
{
$portalSiteId = $this->argument("website_id");
$command = new ScrapePortalSiteCommand($portalSiteId);
$handler->handle($command);
}
}
app/Command/ScapePortalSiteCommand.php
This is the Command I mentioned above. Its job is to wrap all input arguments in a class, which can be used by a command handler.
namespace App\Command;
class ScrapePortalSiteCommand
{
/**
* #var int
*/
private $portalSiteId;
public function __construct(int $portalSiteId)
{
$this->portalSiteId = $portalSiteId;
}
public function getPortalSiteId(): int
{
return $this->portalSiteId;
}
}
app/CommandHandler/ScrapePortalSiteCommandHandler.php
The command handler should implement logic based on its command. In this case that's figuring out which crawler to pick, then fire that one off.
namespace App\CommandHandler;
use App\Command\ScrapePortalSiteCommand;
use App\Crawler\PortalSite1Crawler;
use App\Crawler\PortalSiteCrawlerInterface;
use InvalidArgumentException;
class ScrapePortalSiteCommandHandler
{
public function handle(ScrapePortalSiteCommand $command): void
{
$crawler = $this->getCrawlerForPortalSite($command->getPortalSiteId());
$crawler->crawl();
}
private function getCrawlerForPortalSite(int $portalSiteId): PortalSiteCrawlerInterface {
switch ($portalSiteId) {
case 1:
return new PortalSite1Crawler();
default:
throw new InvalidArgumentException(
sprintf('No crawler configured for portal site with id "%s"', $portalSiteId)
);
}
}
}
app/Crawler/PortalSiteCrawlerInterface.php
This interface is there to make sure all crawlers can be called in similar fashion. Additionally it makes for nice type hinting.
namespace App\Crawler;
interface PortalSiteCrawlerInterface
{
public function crawl(): void;
}
app/Crawler/PortalSite1Crawler.php
This is where the implementation of the actual scraping goes.
namespace App\Crawler;
class PortalSite1Crawler implements PortalSiteCrawlerInterface
{
public function crawl(): void
{
// Crawl your site here
}
}
Another update
As you had some additional questions I've updated my answer once more.
:void
The use of : void in a method declaration means the method will not return anything. In a same way public function getPortalSiteId(): int means this method will always return an integer. The use of return typehints was added to PHP 7 and is not specific to Laravel. More information on return typehints can be found in the PHP documentation.
Commands and handlers
The use of commands and command handlers is a best practice which is part of the command bus pattern. This pattern describes an universal way of dealing with user input (a command). This post offers a nice explanation on commands and handlers. Additionally, this blog post describes in more details what a command bus is, how it's used and what the advantages are. Please note that in the code I've provided the bus implementation itself is skipped. In my opinion you do not need it per se, but in some cases it does add value.

TypeScript dynamic loading of AMD module ends in "Could not find symbol '...'

No, this topic won't answer my question and NO, the solution is not simply importing Command in the nav.ts file. nav.ts is one of many viewModel-files and they will be loaded dynamically on demand. The only problem is to set the parameter's type in the constructor of the class. (Type has to be "Command")
In the following class, which will be loaded by require.js, the method viewModel() requires a new class dynamically. In this case NavViewModel .
command.ts
export class Command {
...
public viewModel(name: string, callback: Function) {
require(["noext!boot/getViewModel/" + name], function (viewModel) {
callback(viewModel);
});
}
}
This is the class which will be fetched by viewModel():
nav.ts
export class NavViewModel extends kendo.Router {
constructor(command: Command) {
super();
this.route('/:name', function (name) {
command.view(name, $('div.content'));
});
this.start();
}
}
EDIT:
Here is the entry-point (requested in comment 2)
main.ts (EntryPoint)
import lib = require("command");
var cmd = new lib.Command();
cmd.viewModel('nav', function (o) {
cmd.view('nav', $('div.header'), function () {
kendo.bind($('.header .nav'), new o.NavViewModel(cmd));
});
});
/EDIT
The Problem:
Visual Studio will throw the error TS2095: Could not find symbol 'Command', because the "Command" class ist not defined in this Module.
The program works fine if the "Command"-Type will be removed from the NavViewModel constructor. Is there any solution to reference the Command class in the NavViewModel?
This won't work:
/// <reference path="../../Scripts/command.ts" />
When using RequireJS, the import statement should be the full path from the root of the application.
I also use a slightly different export syntax
command.ts
class command {
...
}
export = command;
main.ts
// I'm assuming the Scripts folder is at the root of the application
import Command = require('Scripts/command');
var cmd = new Command();
Note
I'm using Typescript 0.9.1.1. I can't upgrade my machine to 0.9.5 as a large internal application is affected by some breaking changes between versions

Error on Magmi after move to another server

I just moved to another server and cannot reindex with Magmi, I receive the error below:
This script cannot be run from Browser. This is the shell script.
Thanks!
This error occurs when you run Magmi from the browser, because Magmi runs the indexer using shell_exec command, and the $_SERVER['REQUEST_METHOD'] doesn't get unset.
You can try one of two things.
Method 1. Unset the $_SERVER['REQUEST_METHOD'] variable Magento uses to check if the shell file is being run from the browser.
To do this, open magmi/plugins/base/general/reindex/magmi_reindexing_plugin.php
Find:
public function updateIndexes()
{
At the top of the updateIndexes() function, add the following:
if(isset($_SERVER['REQUEST_METHOD']))
{
unset($_SERVER['REQUEST_METHOD']);
}
So it will look like this:
public function updateIndexes()
{
if(isset($_SERVER['REQUEST_METHOD']))
{
unset($_SERVER['REQUEST_METHOD']);
}
Method 2: Modify the _validate() function in [magento_root]/shell/abstract.php
Open [magento_root]/shell/abstract.php
Find:
protected function _validate()
{
if (isset($_SERVER['REQUEST_METHOD'])) {
die('This script cannot be run from Browser. This is the shell script.');
}
}
Replace with:
protected function _validate()
{
if (isset($_SERVER['REQUEST_METHOD'])) {
//die('This script cannot be run from Browser. This is the shell script.');
}
}

Selenium RC User Defined Functions

Trying to do something simple -
I have a set of statements to clear browser cookies:
public void clearCookies () {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("10000");
selenium.deleteAllVisibleCookies();
}
Now, if I use this function in a test script (using TestNG), calls to this work perfectly. However, if I moved this function to a separate class and change the declaration to include "static", the "selenium" keyword is not recognized.
In a configuration class (say configClass),
public static void clearCookies () {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("30000");
selenium.deleteAllVisibleCookies();
}
Now, in my test script, if I call configClass.clearCookies();, I get a runtime error
I tried declaring DefaultSelenium selenium = new DefaultSelenium(null);, in the clearCookies() function, but that too results in a runtime error.
I do have the import com.thoughtworks.selenium.*; import in my configClass.
Any pointers would be appreciated. Thanks.
You can do two things.
Refer to the same selenium object in both the classes i.e. in configClass and the class you are calling configClass.clearCookies().
or else
send selenium object to the clearCookies. So the code would be like this
public static void clearCookies (DefaultSelenium selenium) {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("30000");
selenium.deleteAllVisibleCookies();
}

Resources