Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Test php unit in php native

Hello,

I am already installing phpunit in my php native code:

composer require --dev phpunit/phpunit ^9.5

ref: https://phpunit.readthedocs.io/en/9.5/installation.html

Then, when trying to run the test I get an error message:

E:\xampp75\htdocs\phpunit2\phpunit-49\vendor\bin>phpunit app___Controllers___CategoryController

PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

Cannot open file "app___Controllers___CategoryController".

E:\xampp75\htdocs\phpunit2\phpunit-49\vendor\bin>

What's missing?

0 likes
19 replies
martinbean's avatar

@davy_yg Well what do your test look like? This isn’t an installation problem; it’s clearly an error with one of your test cases.

davy_yg's avatar
Level 27

app___Controllers___CategoryController.php

<?php
namespace App\Controllers;

use App\Controllers\BaseController;

class CategoryController extends BaseController {


public function deleteCategory($request, $response, $args)
{
    $category_id = $args['id'];
    // TO DO: delete category by id from the database

    $response = $this->container->view->render($response, 'view.phtml',['category_deleted'=>true]);

    return $response;
}

public function showCategory($request, $response, $args)
{
    $category_id = $args['id'];
    // TO DO: get category by id from the database
    $category = 'Electronics';

    $response = $this->container->view->render($response, 'view.phtml',['category'=>$category]);

    return $response;
}

public function editCategory($request, $response, $args)
{
    $category_id = $args['id'];
    // TO DO: get category by id from the database
    $category = ['name'=>'Electronics','parent'=>null];

    $response = $this->container->view->render($response, 'view.phtml',['editedCategory'=>$category]);

    return $response;
}

public function saveCategory($request, $response, $args)
{
    $data = $request->getParsedBody();
    if(empty($data['category_name']) || empty($data['category_description']))
    $categorySaved = false;
    else
    $categorySaved = true;

    $response = $this->container->view->render($response, 'view.phtml',['categorySaved'=>$categorySaved]);

    return $response;
}
}
davy_yg's avatar
Level 27

@martinbean I am still get the same error message:

E:\xampp75\htdocs\phpunit2\phpunit-49\vendor\bin>phpunit app___Controllers___CategoryController PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

Cannot open file "app___Controllers___CategoryController".

frankielee's avatar

E:\xampp75\htdocs\phpunit2\phpunit-49\vendor\bin>phpunit app___Controllers___CategoryController

There is no app/Controllers/CategoryController in the directory vendor/bin. Also, there is a command php artisan test

davy_yg's avatar
Level 27

@frankielee I am using php native instead of laravel.

There are only 4 files ini vendor/bin

php-parse php-parse.bat phpunit phpunit.bat

frankielee's avatar

@davy_yg The correct command should be

vendor/bin/phpunit "the path of the test files"
//example
vendor/bin/phpunit "test/Feature/PlayerTest.php"

You should configure your xml file correctly.

davy_yg's avatar
Level 27

I get this error:

E:\xampp75\htdocs\phpunit2\phpunit-49>vendor/bin/phpunit "app___Controllers___CategoryController.php"

'vendor' is not recognized as an internal or external command, operable program or batch file.

This is the only command that I have run in php native: composer require --dev phpunit/phpunit ^9.5

How to configure the xml file correctly. I don't see any xml file in my folder.

frankielee's avatar

@davy_yg

'vendor' is not recognized

....... your should run the command at the project's root folder.

Example

image

The command is vendor/bin/phpunit StackTest.php or ./vendor/bin/phpunit StackTest.php

davy_yg's avatar
Level 27

I already run the command in the root folder:

	E:\xampp75\htdocs\phpunit2\phpunit-49>vendor/bin/phpunit "app___Controllers___CategoryController.php"
	'vendor' is not recognized as an internal or external command,

operable program or batch file.

What's wrong?

Tray2's avatar

What does your phpunit.xml look like?

frankielee's avatar

@Tray2

How to configure the xml file correctly. I don't see any xml file in my folder.

No, he configures nothing.

davy_yg's avatar
Level 27

I can only find phpunit.xml in vendor\sebastian\object-enumerator after searching for it

phpunit.xml

	<?xml version="1.0" encoding="UTF-8"?>
	<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
     bootstrap="vendor/autoload.php"
     executionOrder="depends,defects"
     forceCoversAnnotation="true"
     beStrictAboutCoversAnnotation="true"
     beStrictAboutOutputDuringTests="true"
     beStrictAboutTodoAnnotatedTests="true"
     failOnRisky="true"
     failOnWarning="true"
     verbose="true">
			<testsuites>
    		<testsuite name="default">
        		<directory suffix="Test.php">tests</directory>
    		</testsuite>
		</testsuites>

		<coverage processUncoveredFiles="true">
    		<include>
        		<directory suffix=".php">src</directory>
    		</include>
		</coverage>
	</phpunit>
martinbean's avatar

@davy_yg That’s not your phpunit.xml file. It’s a packages since it’s in the vendor directory.

davy_yg's avatar
Level 27

How to create the phpunit.xml ? since I don't have it.

Please or to participate in this conversation.