I created a vendor library with this configuration:
"autoload": {
"psr-4": { "Company\\PhpUtils\\": "src/" }
},
Directory structure:
php-utils/
---src/
------JSON/
--------Json.php
Json Class:
<?php
namespace Company\PhpUtils\JSON;
Now I include it as a dependency:
"company/php-utils": "1.0.0"
and use it in a symfony test:
<?php
namespace Tests\AppBundle\Controller;
use Company\PhpUtils\JSON\Json;
Run tests:
./vendor/phpunit/phpunit/phpunit ./tests
PHPUnit 4.8.22 by Sebastian Bergmann and contributors.
PHP Fatal error: Class 'Company\PhpUtils\JSON\Json' not found in...
The class Json is static... well... follows singleton pattern.
So...Easy question. What am I doing wrong?
In your phpunit command there's no bootstrap, change from:
./vendor/phpunit/phpunit/phpunit ./tests
To:
./vendor/phpunit/phpunit/phpunit ./tests --bootstrap ./vendor/autoload.php
Related
I have a problem with my composer and I don't know where to run. I took several classes on how to do this, but my composer doesn't work well. It manages to import my database connection class, but the rest of the classes, in other files, cannot.
My composer.json it is like this:
{
"autoload":{
"psr-4":{
"Src\\": "src/"
}
}
}
The namespace in the file looks like this:
namespace Src\Model;
And the instance happens this way:
$instance = new \Src\Model\CrudUsuario();
The error is as follows:
Fatal error: Uncaught Error: Class 'Src\Model\CrudUsuario' not found in C:\wamp64\www\Src\Controller\crud_usuario.php on line 79
I'm struggling to have my custom classes autoloaded with composer.
my directory structure:
--muttley
--library
--MyClass.php
--public
--index.php
--vendor
--composer.json
in my composer.json:
"autoload": {
"psr-4": {
"Library\\": "library/"
}
}
MyClass.php:
namespace Library\MyClass;
class MyClass {
}
in index.php:
use Library\MyClass;
require_once dirname(__FILE__).'/../vendor/autoload.php';
the root directory is defined using DocumentRoot /www/muttley/public/. I keep getting the error:
Fatal error: Class 'Library\MyClass' not found in /var/www/muttley/public/index.php on line 58
Is there anything that I might be missing?
Simple mistake. Change:
namespace Library\MyClass;
to
namespace Library;
Make sure you have ran composer dumpautoload too!
I want to test my service in Symfony 4 using phpunit bridge, but when i launch the test i get :
Error: Class 'App\Service\CompanyManager' not found
My service is located at src/Service/CompanyManager.php
tests/Service/CompanyManagerTest.php :
namespace App\Tests\Service;
use App\Service\CompanyManager;
use PHPUnit\Framework\TestCase;
use App\Entity\Company;
class CompanyManagerTest extends TestCase
{
public function testGetCompany()
{
$companyManager = new CompanyManager();
$company = $companyManager->getCompany(2);
$this->assertInstanceOf(Company::class,$company);
$company = $companyManager->getCompany(1000);
$this->assertNull($company);
}
}
In config/services_test.yaml, there is this statement :
# If you need to access services in a test, create an alias
# and then fetch that alias from the container. As a convention,
# aliases are prefixed with test. For example:
#
# test.App\Service\MyService: '#App\Service\MyService'
So i tried to add :
test.App\Service\CompanyManager: '#App\Service\CompanyManager'
But i still get the error :
$ ./vendor/bin/simple-phpunit tests
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
Testing tests
E 1 / 1
(100%)
Time: 364 ms, Memory: 4.00MB
There was 1 error:
1) App\Tests\Service\CompanyManagerTest::testGetCompany
Error: Class 'App\Service\CompanyManager' not found
C:\...\web\vp20\tests\Service\CompanyManagerTest.php:22
Line 22 is :
$companyManager = new CompanyManager();
Any idea ?
PS : sounds like someone has the same problem there : PHPUnit Error: Class not found
I have just had this problem.
Not sure why, but I didn't have a phpunit.xml.dist in the root of my project.
None of the examples shows this, but if you add bootstrap= and then include the autoloaded. It should start to find your classes.
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
I think you should extend KernelTestCase (Symfony\Bundle\FrameworkBundle\Test\KernelTestCase) instead of TestCase
It is possible that a new class is not yet present in composers list. So... try to run
composer dump-autoload
I'll suggest to use this composer configuration
{
"autoload": {
"psr-4": {
"": ["src", "testS"]
}
}
}
Once you've defined this autoloading configuration in composer.json, and after a composer dump-autoload you should never have issues with autoloading.
I scrapped the earlier form of my question because it was too convoluted. Here's the new version.
I want to use phpspec with my psr-4 formatted projects.
Here's the way I tried to set up a test project:
Created a new folder for the project:
cd ~/Desktop/
mkdir TestPhpSpec
cd TestPhpSpec
create a new composer.json file and require phpspec:
composer require phpspec/phpspec
Which creates my composer.json file:
{
"require": {
"phpspec/phpspec": "^2.3"
}
}
I add my psr-4 namespace to the autoload property of my composer.json file:
{
"require": {
"phpspec/phpspec": "^2.3"
},
"autoload": {
"psr-4": {
"Acme\\": "src/Acme"
}
}
}
Then I dump my autoload to make sure my namespace is loaded: composer dumpautoload
After that, I create my phpspec.yml to describe the namespace to phpspec:
suites:
acme_suite:
namespace: Acme
psr4_prefix: Acme
Then I describe the class I want to start building:
phpspec describe Acme/Markdown
This is where I run into the first problem. Even though I specify the Acme namespace in my describe command, the spec does not get placed in a folder matching the namespace:
Though the class it creates is namespaced correctly:
<?php
namespace spec\Acme; // correct namespace
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class MarkdownSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Acme\Markdown');
}
}
Then if I try to run the test to start TDD-ing.
phpspec run
It offers to create the class for me and I let it. From there I get the second problem; I get the error message:
[PhpSpec\Process\Prerequisites\PrerequisiteFailedException]
The type Acme\Markdown was generated but could not be loaded. Do you need to configure an autoloader?
And the class it creates is not in it's namespaced folder:
The class it creates is also namespaced correctly:
<?php
namespace Acme; // correct namespace
class Markdown
{
}
I've looked over the docs and can't figure out what I'm doing wrong. Any suggestions?
Try with
suites:
acme_suite:
src_path: Acme/src
spec_path: Acme/spec
I run a portail with composer's autoloading class system:
"autoload": {
"psr-4": {
"Portal\\": "src/"
}
}
It works when I run composer.phar dump -o, for instance my class Boostrap is well referenced into vendor/composer/autoload_classmap.php file:
'Portal\\Core\\Bootstrap' => $baseDir . '/src/core/Bootstrap.php',
But when I don't run the optimized option on autoload dumping, the autoloading system doesn't works anymore:
Fatal error: Class 'Portal\Core\Bootstrap' not found in /var/www/portail/prod/web/index.php on line 7
How can I make autoloading works without -o option?
There are two ways to fix it.
change composer.json to
"Portal\\Core\\": "src/core/"
Or rename the core directory to Core
https://getcomposer.org/doc/04-schema.md#psr-4
The subdirectory name MUST match the case of the sub-namespace names.
http://www.php-fig.org/psr/psr-4/