简体   繁体   中英

Symfony Unit testing Service with mocks doesn't find the class name

I try to test on a pure unit test my Symfony Service (located in src/AppBundle/Services/CapthaServiceAdapter ):

namespace AppBundle\Services;

use AppBundle\Interfaces\CapthaBuilderInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Gregwar\Captcha\CaptchaBuilder;

/**
* The following class is an adapter for Gregwar Captcha Builder
* I do not set Gregwar's Captha Builder as a Service
* Because I want to stay hidden fromt he service container.
*/
class CapthaServiceAdapter implements CapthaBuilderInterface
{

  const IMAGE_INLINE=0;
  const IMAGE_NORMAL=1;

  /**
  * @var Session;
  */
  private $session=null;

  /**
  * @var CaptchaBuilder
  */
  private $capthaBuilder=null;

  public function __construct(Session $sessionManager)
  {
    $this->session=$sessionManager;
    $this->capthaBuilder=new CaptchaBuilder();
  }

  /**
  * @inheritdoc
  * @throws \InvalidArgumentException when type is not either self::IMAGE_INLINE or self::IMAGE_NORMAL
  */
  public function build($identifier,$type)
  {
    if($type!==self::IMAGE_INLINE || $type!==IMAGE_NORMAL){
      throw new \InvalidArgumentException("Type should be either CapthaService::IMAGE_INLINE or CapthaService::IMAGE_NORMAL you provided the value: ".$type);
    }

    $this->builder->build();
    $this->session->set($sessionKey,$this->builder->getPhrase());

    if($type==self::IMAGE_INLINE){
      return $this->builder->inline();
    }

    return $this->builder->output();
  }

  /**
  * @inheritdoc
  */
  public function verify($identifier,$value)
  {
    $capthaSessionValue=$session->get($identifier);
    return $capthaSessionValue && $value===$capthaSessionValue;
  }

}

So I did the following Test case (located in tests/AppBundle/Services/CapthcaServiceTest.php ):

namespace Tests\AppBundle\Services;

use PHPUnit\Framework\TestCase;
use AppBundle\Services\CapthaServiceAdapter;
use Symfony\Component\HttpFoundation\Session\Session;


class CapthcaServiceTest extends TestCase
{

  private function getServiceForBuild()
  {
    $mock=$this->createMock(Session::class);
    return new CapthaServiceAdapter($mock);
  }

  public function testBuildInline()
  {
    $service=$this->getServiceForBuild();
    $capthaValue=$service->build('somevalue',CapthaService::IMAGE_INLINE);

     $this->assertRegExp('/^data:image\/jpeg;base64,(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)$/i',$element);
  }
}

But when I try to run it I get the following error:

Error: Class 'AppBundle\\Services\\CapthaServiceAdapter' not found

So how I can make my test to look for my class?

Also my pgpunit.xml contains the following content:

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="vendor/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <server name="KERNEL_CLASS" value="AppKernel" />
    </php>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>tests</directory>
            <exclude>./tests/Appbundle/Controller/DefaultControllerTest.php</exclude>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory>src</directory>
            <exclude>
                <directory>src/*Bundle/Resources</directory>
                <directory>src/*/*Bundle/Resources</directory>
                <directory>src/*/Bundle/*Bundle/Resources</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

Edit 1

By doing changes mentioned in the comments (updated the code above) I get the error:

Error: Class 'Tests\\AppBundle\\Services\\CapthaService' not found

Do you know how I can fix that?

Edit 2:

I also have seen this answer so I post my composer.json as well:

{
    "name": "pcmagas/ellakcy_member_app",
    "license": "AGPL-v3",
    "type": "project",
    "autoload": {
        "psr-4": {
            "AppBundle\\": "src/AppBundle"
        },
        "classmap": [
            "app/AppKernel.php",
            "app/AppCache.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        },
        "files": [
            "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
        ]
    },
    "require": {
        "php": ">=5.5.9",
        "captcha-com/symfony-captcha-bundle": "4.*",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/orm": "^2.5",
        "dunglas/angular-csrf-bundle": "^1.1",
        "gregwar/captcha": "^1.1",
        "sensio/distribution-bundle": "^5.0.19",
        "sensio/framework-extra-bundle": "^5.0.0",
        "symfony/dom-crawler": "^4.1",
        "symfony/monolog-bundle": "^3.1.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.6.4",
        "symfony/symfony": "3.4.*",
        "symfony/templating": "^3.4",
        "symfony/twig-bundle": "^4.1",
        "twig/twig": "^1.0||^2.0"
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.0",
        "felixfbecker/language-server": "^5.4",
        "jetbrains/phpstorm-stubs": "dev-master",
        "phpunit/phpunit": "^6.5",
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^4.1"
    },
    "scripts": {
        "symfony-scripts": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-install-cmd": [
            "@symfony-scripts"
        ],
        "post-update-cmd": [
            "@symfony-scripts"
        ]
    },
    "config": {
        "sort-packages": true
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-bin-dir": "bin",
        "symfony-var-dir": "var",
        "symfony-web-dir": "web",
        "symfony-tests-dir": "tests",
        "symfony-assets-install": "relative",
        "branch-alias": null
    }
}

As you can see it has the autoloading class as well:

"autoload": {
            "psr-4": {
                "AppBundle\\": "src/AppBundle"
            },
            "classmap": [
                "app/AppKernel.php",
                "app/AppCache.php"
            ]
        },
        "autoload-dev": {
            "psr-4": {
                "Tests\\": "tests/"
            },
            "files": [
                "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
            ]
        },

Also By changing into this:

"autoload": {
        "psr-4": {
            "AppBundle\\": "src/AppBundle",
            "AppBundle\\Services":"src/AppBundle/Services",
        },
        "classmap": [
            "app/AppKernel.php",
            "app/AppCache.php"
        ]
    },

Results into this error:

Error: Class 'Tests\\AppBundle\\Services\\CapthaService' not found

Hos I can make the phphunit to look into the appropriate namespace?

Over your code there is a line that mentions:

$capthaValue=$service->build('somevalue',CapthaService::IMAGE_INLINE);

The class CapthaService where is defined? Did you mean CapthaServiceAdapter (perhaps leftover from a refactor?)

Change into that and that should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM