1

I have a class for dynamoDB in location

src/Dynamo/shop.php 

My class looks like below

<?php
    
    namespace App\Dynamo;
    
    
    class Shop
    {
        -----
    }
    
?>

I am trying to implement unit test for this class, So I have created a folder class dynamo in below location.

app/cake/tests/TestCase/Dynamo

In Dynamo folder I have created a class with file name ShopTest.php

For create a unit testing I have written this class like below

<?php
declare(strict_types=1);

namespace App\Test\TestCase\Dynamo;

use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;

class ShopTest{
    
    public function setUp()
    {
        $this->shop = new Shop;
    }

    public function testconnectDynamoDB()
    {
        debug($this->shop->connectDynamoDB());

        $this->assertNotEmpty($this->shop->connectDynamoDB());

    }
}

Now after run phpunit command

vendor/bin/phpunit tests/TestCase/Dynamo/ShopTest.php 

I am getting

Class 'App\Test\TestCase\Dynamo\ShopTest' could not be found in '/var/www/html/tests/TestCase/Dynamo/ShopTest.php'.

Class is present in this location , Why I am getting Class could not be found ?

root@0ceda1df4444:/var/www/html# cd /var/www/html/tests/TestCase/Dynamo/
    root@0ceda1df4444:/var/www/html/tests/TestCase/Dynamo# ls
    ShopTest.php

root@0ceda1df4444:/var/www/html/tests/TestCase/Dynamo# cat ShopTest.php 
<?php
declare(strict_types=1);

namespace App\Test\TestCase\Dynamo;

use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;

class ShopTest{

Also I am trying to run all test case by below command , I am getting a warning.

root@0ceda1df4444:/var/www/html# vendor/bin/phpunit                
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

No tests executed!
Niloy Rony
  • 602
  • 1
  • 8
  • 23
  • Is not a factor in the question but:`src/Dynamo/shop.php` should be `src/Dynamo/Shop.php` to match PSR-4. – AD7six Sep 10 '21 at 09:13

1 Answers1

1

There is no test class

The error message there is a bit confusing, but the crux is this is not a test class:

<?php
declare(strict_types=1);

namespace App\Test\TestCase\Dynamo;

use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;

class ShopTest{ # <--

It is just a class (which coincidentally has the word Test in its name).

Compare to an example from the documentation:

namespace App\Test\TestCase\View\Helper;

use App\View\Helper\ProgressHelper;
use Cake\TestSuite\TestCase;
use Cake\View\View;

class ProgressHelperTest extends TestCase # <--
{

To be detected as a test the class must extend TestClass - therefore to correct this:

...
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;

class ShopTest extends TestCase # <--
{

With that change the test class will load and some easier-to-solve problems will become apparent:

$ vendor/bin/phpunit tests/TestCase/Dynamo/ShopTest.php
PHP Fatal error:  Declaration of ShopTest::setUp() must be compatible with Cake\TestSuite\TestCase::setUp(): void in ~/repos/cakephp/app/tests/TestCase/Dynamo/ShopTest.php on line 11

Fatal error: Declaration of ShopTest::setUp() must be compatible with Cake\TestSuite\TestCase::setUp(): void in ~/repos/cakephp/app/tests/TestCase/Dynamo/ShopTest.php on line 11
AD7six
  • 63,116
  • 12
  • 91
  • 123