0

I am trying to run unit tests on custom functions that I have in a file '\app\Custom\custom.php`

I am trying to include the custom file in my test file with:

include(app_path().'/Custom/custom.php');

but I get the error:

PHP Fatal error:  Uncaught Error: Call to undefined method Illuminate\Container\Container::path()

As per @habeebdev's answer, I have tried:

file path added to composer.json

...
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        },
        "files": ["app/Custom/custom.php"]
    },
...

laravel/tests/Unit/PracticeTest.php

<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class PracticeTest extends TestCase
{
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function test_trueExample()
    {

        $user = User::find(1);

        $this->actingAs($user)
             ->assertEquals(true, can_transact());

    }
}

error:

 Call to undefined function Tests\Unit\can_transact()

EDIT:

I had a namespace App\Http\Controllers in my custom.php file. Once I prefixed my functions App\Http\Controllers\can_transact() it worked fine (Thanks @apokryfos)

1 Answer 1

2

You have to autoload the custom file. For this, please include custom.php in the 'autoload-dev' section of composer.json

"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
     },
     "files": ["app/Custom/custom.php"]
},

Run composer dump-autoload

Now you can call the custom function in your test script. No need to include the file.


Regarding the namespace, please change your custom.php namespace to App\Custom. Using Controller namespace for custom file is not good. Also, I recommend you to go through OOP and laravel basics.

Sign up to request clarification or add additional context in comments.

18 Comments

I'd go one step further to say that if you find yourself needing to manually include these kinds of files and are using composer then something is wrong.
Hi, I have added the line to composer.json and run composer dump-autoload which completes ok. However when I call the function in the test I still get Call to undefined function
@apokryfos can you elaborate please.
@matt you need to make sure that your composer autoloader is the only thing included. After that composer will be responsible for all other includes
@matt you probably should update your question with the code in your custom.php but it's possible the file is namespaced and therefore you need to refer to functions declared in it by providing that namespace the same way you would with classes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.