Laravel - Elasticsearch ClientBuilder Not found - php

I am using Laravel 5.7 and Elasticsearch/elasticsearch ^6.1
I installed elasticsearch via composer and my class is as follows
use Elasticsearch\ClientBuilder;
trait Elasticable {
public abstract function getType();
public abstract function getIndex();
public function getClient() {
$elastic= Elasticsearch\ClientBuilder::create()
->setHosts(\Config::get('app.esserver'))
->build();
return $elastic;
}
However, I keep getting
Symfony\Component\Debug\Exception\FatalThrowableError: Class
'App\Traits\Elasticsearch\ClientBuilder' not found in file
Somehow Laravel / PHP is not recognizing Elasticsearch namespace or some autoload issue
I have tried composer dump-autoload and that does not help

Related

Undefined type 'Maatwebsite\Excel\Concerns\ToModel

I use the Maatwebsite package to import Excel files in Laravel 8. I also executed the following command after installing the package and adding needed aliases and providers in the app.php.
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
But when I create an import with a model Mymodel (a model that I have), I get the following error.
undefined Maatwebsite\Excel\Concerns\ToModel;
Import class
class Importer implements ToModel
{
public function model(array $row)
{
return new Universite([
//
]);
}
}
You have to restart your VS Code, because it'll apply your latest package installed
Add the following before you create the class:
use Maatwebsite\Excel\Concerns\ToModel;

Illuminate\Console\Command not found when developing laravel 5.8 package

I am developing a package for Laravel 5.8. When I try to create a console command that extends Illuminate\Console\Command then "composer dump-autoload" fails with error message:
c:\Program Files (x86)\Ampps\www\ptest>composer dump-autoload
Generating optimized autoload files> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
ReflectionException : Class TestVendor\TestPackage\TestCommand does not exist
at C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
786| if ($concrete instanceof Closure) {
787| return $concrete($this, $this->getLastParameterOverride());
788| }
789|
> 790| $reflector = new ReflectionClass($concrete);
791|
792| // If the type is not instantiable, the developer is attempting to resolve
793| // an abstract type such as an Interface or Abstract Class and there is
794| // no binding registered for the abstractions so we need to bail out.
Exception trace:
1 ReflectionClass::__construct("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
2 Illuminate\Container\Container::build("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:667
I have tried to create the package by hand inside of C:\Program Files (x86)\Ampps\www\ptest\packages folder and I tried to use the packager https://github.com/Jeroen-G/laravel-packager but the result is identical in both cases.
TestCommand.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Console\Command;
class TestCommand extends Command {
protected $signature = 'test:hello';
protected $description = 'say hello';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info("hello!");
}
}
TestServiceProvider.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}
}
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/testpackage.php', 'testpackage');
$this->app->singleton('testpackage', function ($app) {
return new TestPackage;
});
}
public function provides()
{
return ['testpackage'];
}
protected function bootForConsole()
{
// Registering package commands.
$this->commands([TestCommand::class]);
}
}
When I execute the TestCommand.php file directly from command line it fails with the error message
PHP Fatal error: Class 'Illuminate\Console\Command' not found
I have checked other working packages inside "Vendor" folder and all have the same structure as my package. It seems as if autoloading does not work properly.
The "console" folder was outside of "src" folder. Therefore it could not be discovered.
remove vendor and node_modules folder
then run following commands
composer update
npm install
it should work fine.

PHPUnit Error: Call to undefined method Controller::request()

On running this test, I get Error: Call to undefined method OrderControllerTest::request()
<?php
use PHPUnit\Framework\TestCase;
class OrderControllerTest extends TestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
PHPUNIT Version: 7.2.4. Appreciate any help
For the kenjis/ci-phpunit-test package if you run phpunit from the application/tests folder so it picks up on the included phpunit.xml and included TestCase Class then they should run.
cd application/tests
then, if you are using the composer installed version of phpunit in your project:
../../vendor/bin/phpunit
or if you're using the globally (apt etc.) installed version simply:
phpunit
see http://blog.a-way-out.net/blog/2015/06/12/codeigniter3-phpunit/#how-to-run-tests
The package kenjis/ci-phpunit-test extends the standard PHPUnit package so what you need to do is to extend the CIPHPUnitTestCase instead like in example below.
<?php
class OrderControllerTest extends CIPHPUnitTestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
You may need to configure your IDE so that it can find CIPHPUnitTestCase class.

Fix Symfony 3.4 Deprecated warnings PHPUnit

Consider this simple example:
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$client->request('GET', '/');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
When executing this via PHPUnit I get the following warning
Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won't be supported in version 4.0. You should rename (or alias) the "test.client.history" service to "Symfony\Component\BrowserKit\History" instead: 1x
1x in KitaControllerTest::testIndex from Tests\AppBundle\Controller
How can I fix this warning since this is declared in the core of Symfony.
I am running Symfony 3.4.3

Using filp/whoops as a service provider in Laravel 5.1

I'm trying to integrate the filp/whoops package into Laravel 5.1 app.
Installed the package like this:
composer require filp/whoops:~1.0
Created app/Providers/ErrorServiceProvider.php like this:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
class ErrorServiceProvider extends ServiceProvider {
public function boot()
{
$whoops = new Run;
$whoops->pushHandler(new PrettyPageHandler);
$whoops->register();
}
public function register()
{
//
}
}
And registered the service provider in config/app.php like this:
'providers' => [
App\Providers\ErrorServiceProvider::class
]
But I'm still seeing the default error pages.
run composer dump-autoload then your problem solved

Categories