Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phpunit can't find abstract class

For some reason when I try to test abstract class I get PHPUnit_Framework_MockObject_RuntimeException: Class "AbstractClass" does not exist.

Here's the code

AbstractClass.php

<?php 
namespace SD\Project;

abstract class AbstractClass 
{
  public function handle()
  {

  }
}

AbstractClassTest.php

<?php 

require_once 'AbstractClass.php';

use SD\Project\AbstractClass;

class AbstractClassTest extends PHPUnit_Framework_TestCase
{
 public function testHandle()
 {
   $stub = $this->getMockForAbstractClass('AbstractClass');
 }
}

When I get rid off the namespace and use statements the code is executed successfully. What I'm doing wrong?

like image 479
MZON Avatar asked May 04 '15 19:05

MZON


1 Answers

You are not using the fully qualified path of the class.

$stub = $this->getMockForAbstractClass('\SD\Project\AbstractClass');

Read Similar: PHPUnit, Interfaces and Namespaces (Symfony2)

Examples: http://theaveragedev.com/testing-abstract-classes-with-phpunit/

like image 195
Jeremy Harris Avatar answered Nov 02 '22 15:11

Jeremy Harris