PHPUnit class not found in test - php

I have an existing PHP project. I need to test a class MyClass. I created a test under the same package.
When running with IntelliJ 2016.1 I get error Fatal error: Class 'MyClass' not found in
This is example of the test
<?php
class MyClassTest extends PHPUnit_Framework_TestCase
In Run Configuration I entered Custom working directory as the Composer vendor package.
PHPUnit is loaded from the include path
Under Settings->PHP I added the phpunit Composer package and my class package to the include path.
Under Settings->PHP->PHPUnit default config file and default bootstrap are unchecked.
What else should I configure so I can run MyClassTest without errors and test the class MyClass?

If the class is part of a composer package, or you use the composer autoloader for your project files, you can set up vendor/autoload.php as bootstrap file.
If you have set up a custom autoloader for project classes, you need to create your own boostrap.php file for PHPUnit and register the autoloader there.

Related

Namespace that seems it doesn't exist is working but why?

I have this exercice folder that gave me instructions on how to install the web application:
So this folder contains a .sh file that installs the necessary things:
npm install
curl -o phpunit -L https://phar.phpunit.de/phpunit-7.phar
chmod +x phpunit
./phpunit --version
So basically this adds a file called phpunit inside root folder. So by far I kind of understand what's going on, so this installs node dependencies and also installs phpunit (I think so), but this project has a test example unit:
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class HelloWorldTest extends TestCase
{
public function testWhenGetDataFromHelloWorld(): void
{
$this->assertEquals('Hello World', 'Hello World');
}
}
At this point is where I don't get how is use PHPUnit\Framework\TestCase; working, since I don't have any folder called PHPUnit, I get that use is something like importing a class, but still, how is this working (when I run the test it does not give me any kind of error)?, also, it appears that vscode detects the import as an error because it seems that it doesn't exist.
My file tree is as follows:
Can someone explain me, please?
This is because all the dependencies are in the phpunit-7.phar file, which was named phpunit by that script.
"The easiest way to obtain PHPUnit is to download a PHP Archive (PHAR) that has all required (as well as some optional) dependencies of PHPUnit bundled in a single file."
PHPUnit - Installing PHPUnit - PHP Archive (PHAR)

How autoload parent PrestaShop classes in codeception?

I have PrestaShop module project under PrestaShop modules folder name mymodule
Prestashop
admin
classes
modules
mymodule - sub project root
I want test module using codeception so I install it under mymodule folder.
Prestashop
admin
classes
modules
mymodule - sub project root
tests
vendor
codeception.yml
composer.json
...
All PrestaShop modules extends Module class which is under classes folder and use other classes from parent directory.
When I run codecept run inside my root project (module) I'm getting error
PHP Fatal error: Class 'Module' not found
How can I autoload parent classes in codeception so I can run test under mymodule folder ?
There are rules about the file creation in modules, you should take a look at the official documentation, which is clear about this, and basically is, if your folder is called mymodule, PrestaShop will try to find the file called mymodule.php, and this file should extend the class Module, here the folder structure.

Class for command controller not put to autoload

I have a composer based TYPO3 7.6 installation and want to create an Extbase extension with a command controller.
The controller is registered in ext_localconf.php but the commmand controller isn't found because the class is not found in typo3/sysext/extbase/Classes/Mvc/Cli/CommandManager.php in public function getAvailableCommands()
Namespace is also set: namespace Foo\FooT3monitoringNotification\Command;
The class is here Classes/Command/NotificationCommandController.php.
I've cleared all TYPO3 caches and did composer dump-autoload. Any idea, what I missed to do or what I can do to find out, why my class isn't put to autoload?
As your newly created extension is not installed via composer, you need to define where to look for the classes. Therefore you need to add an autoload section to your root composer.json (that' means not in your extension, but in your TYPO3 distribution root folder):
"autoload": {
"psr-4": {
"Foo\\FooT3monitoringNotification\\": "web/typo3conf/ext/foo_t3monitoring_notification/Classes"
}
}
More information: https://usetypo3.com/typo3-and-composer.html#c67

Using phpunit how to make composer autoloader check global as well as local vendor directory for classes?

So I installed phpunit globally using composer. But I want to use it in a project that also uses composer, but doesn't have phpunit installed (the reasons for this are boring).
So I run the first test below and get the error.
Fatal error: Class 'PHPUnit\Framework\TestCase' not found in /vagrant/sites/core/trunk/Notify/php/tests/MyFirstTest.php on line 5
I thought that loading the global composer autoloader would let phpunit look in the global composer directory for the phpunit classes but it doesn't seem to be able to find it. I guess it's setting $vendorDir (below) to the local composer directory instead of the global one. Is there a way to make it check both?
Here's the line from /home/vagrant/.composer/vendor/composer/autoload_classmap.php that shows the class is in the global autoload classmap.
'PHPUnit_Framework_TestCase' => $vendorDir . '/phpunit/phpunit/src/Framework/TestCase.php',
Here's the command line I'm using to start the process.
phpunit --bootstrap tests/bootstrap.php tests/MyFirstTest.php
Here's MyFirstTest.php
<?php
use PHPUnit\Framework\TestCase;
class MyFirstTest extends TestCase {
public function testOneIsOne() {
$this->assertEquals(1, 1);
}
}
Here's bootstrap.php where I load the global phpunit autoloader.
<?php
require_once('/home/vagrant/.composer/vendor/autoload.php');
Writing out this question has helped to clarify my thoughts a lot. If anyone's got a nice solution that'd be great. I'm starting to think I should just install phpunit locally.
Cheers for any advice.
The problem was that I was using PhpUnit 4.8 because the project I'm working on uses php 5.5.9 and the current version of PhpUnit, 5.0 and above requires php 5.6.0.
The tutorial on phpunit.de/getting-started.html is for PhpUnit 5.0 and above. It looks like PhpUnit 5.0 introduced namespacing to the framework. Before that the PHPUnit\Framework\TestCase class I was trying to load was PHPUnit_Framework_TestCase instead.
You can actually see that in the line from the classmap file:
'PHPUnit_Framework_TestCase' => $vendorDir . '/phpunit/phpunit/src/Framework/TestCase.php',
But I thought the underscores in the name of the class were just some name wrangling composer's classmap was doing to represent the namespacing.
As soon as I changed MyFirstTest.php to look for the right class PhpUnit_Framework_TestCase and dropped the use statement everything worked fine. There was no need to install PhpUnit locally. It found the proper class in the global composer vendor directory.
<?php
class MyFirstTest extends PhpUnit_Framework_TestCase {
public function testOneIsOne() {
$this->assertEquals(1, 1);
}
}

How can I get IDE autocomplete for PHPUnit?

I am using Symfony2, everything is installed, my tests work so good so far.
I'd like to get an autocompletion of PHPUnit's methods.
Symfony's WebTestCase class extends from PHPUnit_Framework_TestCase just like below:
abstract class WebTestCase extends \PHPUnit_Framework_TestCase
The parent class is highlighted as not existing although.
How can I tell my IDE to use PHPUnit's library?
I am using PHPStorm
PHPUnit is available by path /Users/myUser/pear/share/pear/PHPUnit/
Add it as a library... in the project that you are editing add it to 'External Libraries'.
It should then be included.
For me (Ubuntu 12.04) it was adding this folder as external library:
/usr/share/php/PHPUnit
For PHPStorm users, go to File -> Settings -> Project Settings ->PHP and add the path in there.
PhpStorm 2016.2 introduces a feature which - in this case - is also a bug.
Autocompletion now no longer includes static methods as an option when in $this-> context. (https://blog.jetbrains.com/phpstorm/2016/07/completion-changes-in-phpstorm/#more-10425)
As phpunit tests are defined as static methods but called via $this->, autocomplete for phpunit is now effectively broken.
They've rolled back this change for phpunit in the next EAP (https://youtrack.jetbrains.com/issue/WI-32530).
Workaround until the next stable release: Press CTRL-Space twice; this will then show static methods in the autocomplete field.
You can add package phpunit/phpunit to the require-dev section of your composer.json file.
After running composer install, PHPStorm will know about the PHPUnit classes.
I have OSX, phpunit installed by homebrew and phpstorm 9.0
So how it works for me: open preferences or press cmd+, -> Languages&Frameworks -> PHP -> Include path -> add "/usr/local/Cellar/phpunit/4.7.6/libexec/"
I have PHPStorm 2017.1.4 and my OS is Ubuntu 16.04. I already have phpunit.phar installed in my /usr/local/bin.
I will use ~/WORK/.. for the example paths but you should use the full path /home/myname/WORK/..
What I did is just go to a folder inside my home(like: ~/WORK/) and run:
composer require phpunit/phpunit
After composer was done downloading phpunit I added a new project include path to:
~/WORK/vendor/phpunit/phpunit/src
Now I have all of the PHPUnit autocompletion, I can jump into PHPUnit's source code directly and I can keep the PHPUnit's code updated with composer. I also removed the phpunit.phar from /usr/local/bin and replaced it with a link to ~/WORK/vendor/bin/phpunit

Categories