1

I'm doing a unit testing with my CVRClient file using the code below.

//CvrapiTest.php

<?php

namespace Drupal\cvr\test\Cvrapi;

use PHPUnit_Framework_TestCase;
use cvr\src\Cvrapi\CVRClient;

class CvrapiTest extends PHPUnit_Framework_TestCase {

public function testSearchOnlyVAT()
    {
        $result = \Cvrapi\CVRClient::request('29910251', 'dk', 'vat', 'Unit Testing');

        $this->assertTrue(property_exists($result, 'vat'));
        $this->assertTrue((int)$result->vat === 29910251);
    }

I have a CVRClient class under cvr/src/Cvrapi folder that queries data from an api.

I know that phpunit command is working fine but I'm getting the error below.

Fatal error: Class 'cvr\src\Cvrapi\CVRClient' not found in /Applications/MAMP/htdocs/query/modules/custom/cvr/test/Cvrapi/CvrapiTest.php on line 17

NOTE: It's the \Cvrapi\CVRClient:: that causes the error. If I remove this line, I need to have a code to assign values to $result.

1 Answer 1

0

The problem is that your use declaration is incorrect, and is causing your call to the client to look in the wrong namespace. You probably need to change this:

use cvr\src\Cvrapi\CVRClient;

To this:

use Cvrapi\CVRClient;

And change this:

$result = \Cvrapi\CVRClient::request('29910251', 'dk', 'vat', 'Unit Testing');

To this:

$result = CVRClient::request('29910251', 'dk', 'vat', 'Unit Testing');
6
  • Still giving me the same error but this time: Class 'Cvrapi\CVRClient' Commented Feb 7, 2017 at 16:23
  • 1
    Open the file cvr/src/Cvrapi/CVRClient.php, and paste the namespace declaration inside it here.
    – Jaypan
    Commented Feb 7, 2017 at 16:51
  • this is the namespace of my CVRClient.php namespace Drupal\cvr\src\Cvrapi; Commented Feb 8, 2017 at 7:38
  • I think I might see the problem. It looks like you are using PHPUnit outside of Drupal, so it's likely it's not able to find autoloaded classes from within Drupal. Are you using Drupal 8? If so, you should be extending UnitTestCase. If D7, I'm not sure what you should be using. You may want to check out a presentation I did recently on Unit Testing in D8: youtube.com/edit?video_id=becQyZZu2lw (associated slideshow: slideshare.net/JayFriendly/…)
    – Jaypan
    Commented Feb 8, 2017 at 7:52
  • Yes, I'm using PHPUnit to test in Drupal 8 site, but in D8 they already included PHPUnit and actually it works fine when I try basic testing, on empty classes. Alright, thanks..will try UnitTestCase. BTW, your youtube link is redirected to my uploaded videos :D Thanks for your help. Commented Feb 8, 2017 at 8:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.