'Class "X" not found (autoloader with PHPUnit and composer)

I'm learning about namespaces and class autoloader in PHP, for this I'm using PHPUnit to register the autoload with phpunit.xml and placing the namespace in composer.json, but I don't know what I'm doing wrong, I get: class not found.

My file structure:

juan-searching

--src
------Index.php
------Car.php
--tests
------Test.php
--vendor
--composer.json
--phpunit.xml

My composer.json

{
    "name": "juan/searching",
    "description": "Searching",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "Juan",
            "email": "[email protected]"
        }
    ],
    "autoload": {
        "psr-4": {"Scraping\\": "src/"}
    },
    "optimize-autoloader": true,
    "require": {},
    "require-dev": {
        "phpunit/phpunit": "^9.5"
    }
}

My phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="/vendor/autoload.php"
         colors="true"
         stopOnFailure="true">

    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">tests</directory>
        </testsuite>
    </testsuites>

        <coverage processUncoveredFiles="true">
            <include>
            <directory suffix=".php">src</directory>
            </include>
            <exclude>
                <directory>./tests</directory>
                <directory>./vendor</directory>
            </exclude>
        </coverage>

</phpunit>

My class (in /src/Car.php):

<?php
namespace Scraping;
class Car {
    protected $placa;
    public function searching(string $placa): ?string {
        return "DATA SENT";
    }
}

My index.php (for instance class Car):

<?php
namespace Scraping;
use Scraping\Car;
$data = new Car();
$data = $data->searching("123456789");
print $data;

Note: After adding autoload and the phpunit.xml I did composer update + composer dumpautoload, I have generated the autoload, but when starting the Index.php I get: "Class "Car" not found".

Thanks

Solved:

<?php
require '../vendor/autoload.php';
...
php


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source