use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\IntercatsWithMedia;
class Post extends Model implements HasMedia
{
use HasFactory, IntercatsWithMedia;
}
Error is following in Problems tab of VSCode
Undefined type Spatie\MediaLibrary\HasMedia;
Undefined type Spatie\MediaLibrary\InteractsWithMedia;
Error removed I used following command in windows cmd in project folder instead of integrated cmd of vscode
composer require filament/spatie-laravel-media-library-plugin:"^2.0"
I created a small symfony4 bundle to manage Mysql database backup.
I created a packagist folder to implement it easily.
after install, my package path is:
webDirectory\vendor\fpasquer\symfony-backup-bundle\BackupSymfonyBundle.php
I'm able to use every class from this bundle excepted commands.
This is one command:
<?php
namespace Fpasquer\BackupSymfony\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class BackupExportCommand extends AbstractBackup
{
protected static $defaultName = 'Fpasquer:BackupSymfony:extract';
...
}
When I run it :
php bin/console Fpasquer:BackupSymfony:extract
I get this exception:
There are no commands defined in the "Fpasquer:BackupSymfony" namespace.
I'm sure my bundle is installed correctly because in my app:controller I'm able to use DependencyInjection from this bundle
Do you have any idea what's wrong?
I have a problem getting PHP's SoapClient to work in my Laravel Job.
I created queueable job where I import the SoapClient with use SoapClient but Laravel is not able to find it.
But when I use the SoapClient in my Controllers it works flawlessly.
I checked phpini(): SoapClient is definitely enabled.
Any Ideas?
My Job Code:
<?php
namespace App\Jobs;
use SoapClient;
use Tymon\JWTAuth\Exceptions\JWTException;
use JWTAuth;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class ProcessQueuedRenderRequests implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct()
{
}
public function handle()
{
$soap = new SoapClient("http://mywsdl");
...
Errorlog:
[[2017-11-22 18:17:50] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'SoapClient' not found in /var/www/app/Jobs/ProcessQueuedRenderRequests.php:44
I use Docker with the Laradock configuration.
The joblistener ist started in this way:
docker-compose exec workspace bash
php artisan queue:listen
use
$soap = new \SoapClient("http://mywsdl");
I had the same issue once.
Found the problem. Soap was not installed in the workspace container.
When I start the joblistener like this it works:
docker-compose exec php-fpm bash
php artisan queue:listen
Still a bit strange since should also be installed in the workspace container according to my .env file.
I'm trying to inject the class HotelsTransformer without success with the next code:
UserTransformer
<?php
namespace App\Transformers;
class UserTransformer extends Transformer
{
...
}
HotelsTransformer
<?php
namespace App\Transformers;
class HotelsTransformer extends Transformer
{
...
}
ApiHotelsController
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use \App\Hotel;
use \App\Transformers\HotelsTransformer;
class ApiHotelsController extends ApiController
{
protected $HotelsTransformer;
public function __construct(HotelsTransformer $HotelsTransformer)
{
$this->HotelsTransformer = $HotelsTransformer;
dd($this->HotelsTransformer);
}
When I inject UserTransformer, it's all OK, but when I change UserTransformer with HotelsTransformer it throws me this error.
I don't know why is this happening, because I cloned UserTransformer and change its name but same error persists.
Check your following namespace. May be it does not exist or namespace path is not correct
use \App\Transformers\HotelsTransformer;
Try to run composer dumpauto command.
Try running these commands from your terminal (from the root directory of your project)
// use sudo if it asks for the root permission
composer update
composer dump-autoload
php artisan config:clear
Rerun the app/project and try again.
These 2 commands will refresh composer loaded classes and clear the cache to make the project run freshly, hope it helps.!
Ok, I have solved it, the filename was wrong, changed:
app/Transformers/HotelsTranformer.php
to:
app/Transformers/HotelsTranformer.php
1 hour spent on that like a crazy, good job.
Why I'm getting this PHP error?
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...
For those arriving here after updating phpunit to version 6 or greater released on 2017-02-03 (e.g. with composer), you may be getting this error because phpunit code is now namespaced (check changelog).
You will need to refactor things like \PHPUnit_Framework_TestCase to \PHPUnit\Framework\TestCase
The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows:
require_once ('PHPUnit/Framework/TestCase.php');
UPDATE
As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:
require_once 'PHPUnit/Autoload.php';
Thanks to Phoenix for pointing this out!
For higher version of phpunit such as 6.4
You must use the namespace PHPUnit\Framework\TestCase
use TestCase instead PHPUnit_Framework_TestCase
// use the following namespace
use PHPUnit\Framework\TestCase;
// extend using TestCase instead PHPUnit_Framework_TestCase
class SampleTest extends TestCase {
}
I was running PHPUnit tests on PHP5, and then, I needed to support PHP7 as well. This is what I did:
In composer.json:
"phpunit/phpunit": "~4.8|~5.7"
In my PHPUnit bootstrap file (in my case, /tests/bootstrap.php):
// PHPUnit 6 introduced a breaking change that
// removed PHPUnit_Framework_TestCase as a base class,
// and replaced it with \PHPUnit\Framework\TestCase
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase'))
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
In other words, this will work for tests written originally for PHPUnit 4 or 5, but then needed to work on PHPUnit 6 as well.
You may get this error because you namespaced the file. If so you will need to specify that PHPUnit_Framework_TestCase is in the global namespace by preceding it with a backslash:
namespace AcmeInc\MyApplication\Tests
class StackTest extends \PHPUnit_Framework_TestCase {}
I submitted a crude PR to start conversation for correcting the documentation.
You can simply install PHPUnit to run commands (https://github.com/sebastianbergmann/phpunit/#php-archive-phar):
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
Run single test
And then run PHPunit test:
phpunit test.php
Content of test file is following:
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
}
public function testSave()
{
}
}
Run test suite
Configuration of test suite: demosuite.xml. demo is directory containing all tests. Test files must be named as *_test.php (suffix).
<testsuites>
<testsuite name="DemoTestSuite">
<directory suffix="test.php">demo</directory>
</testsuite>
</testsuites>
Test suite runs with following commands:
phpunit -c demosuite.xml --testsuite DemoTestSuite
Assumption:
Phpunit (3.7) is available in the console environment.
Action:
Enter the following command in the console:
SHELL> phpunit "{{PATH TO THE FILE}}"
Comments:
You do not need to include anything in the new versions of PHPUnit unless you do not want to run in the console. For example, running tests in the browser.
I use ZF2 and work for me when replaced 'PHPUnit_Framework_TestCase' to '\PHPUnit\Framework\TestCase'
I got it working with
include("vendor/autoload.php");
at the top of my test function.
If you have Centos or other Linux distribution you have to install phpunit package, I did that with yum install phpunit and it worked. Maybe you can have to add a repository, but I think it has to work smooth with the default ones (I have CentOS 7)
It may well be that you're running WordPress core tests, and have recently upgraded your PhpUnit to version 6. If that's the case, then the recent change to namespacing in PhpUnit will have broken your code.
Fortunately, there's a patch to the core tests at https://core.trac.wordpress.org/changeset/40547 which will work around the problem. It also includes changes to travis.yml, which you may not have in your setup; if that's the case then you'll need to edit the .diff file to ignore the Travis patch.
Download the "Unified Diff" patch from the bottom of https://core.trac.wordpress.org/changeset/40547
Edit the patch file to remove the Travis part of the patch if you don't need that. Delete from the top of the file to just above this line:
Index: /branches/4.7/tests/phpunit/includes/bootstrap.php
Save the diff in the directory above your /includes/ directory - in my case this was the Wordpress directory itself
Use the Unix patch tool to patch the files. You'll also need to strip the first few slashes to move from an absolute to a relative directory structure. As you can see from point 3 above, there are five slashes before the include directory, which a -p5 flag will get rid of for you.
$ cd [WORDPRESS DIRECTORY]
$ patch -p5 < changeset_40547.diff
After I did this my tests ran correctly again.
NOTICE: Command php bin/console generate:doctrine:crud also create TestController in src/Tests so it can throw error when you tried to start server if you don't have UnitTests. Remove the file fix it!
For me, it was because I ran
$ phpunit .
instead of
$ phpunit
when I already had a configured phpunit.xml file in the working directory.
I am using php 5.6 on window 10 with zend 1.12 version for me adding
require_once 'PHPUnit/Autoload.php';
before
abstract class Zend_Test_PHPUnit_ControllerTestCase extends
PHPUnit_Framework_TestCase
worked. We need to add this above statement in ControllerTestCase.php file