4

I have refactored some PHP code and putting it through a series of PHPUnit classes.

I get the above Fatal Error when running PHPUnit (3.7.28) on it (through console).

PHP version is 5.4.6-1ubuntu1.4 (cli).

I know the Zip class is working and available as it works when running the code normally (also via console)

Thoughts / ideas appreciated.

Thanks!

<?php

namespace phpUnit\Test;

Class MyTest extends \PHPUnit_Framework_TestCase
{
Public Function setUp()
    {
    $this->zip = new ZipArchive();
    }
}
peedeeaay
  • 116
  • 1
  • 7
  • 2
    This [looks like a module](http://www.php.net/manual/en/zip.installation.php) that needs enabling. Maybe PHPUnit is using a different version of PHP? – halfer Dec 22 '13 at 23:54
  • Yes, thanks, I have found the php.ini file and examined - zip.so is not contained within it. I have requested the server admins to correct this and we'll see. – peedeeaay Dec 23 '13 at 02:01
  • 2
    Besides your code above will never work. You'd have to write ``$this->zip = new \ZipArchive()`` unless this class is defined within the namespace ``phpUnit\Test``. – l-x Dec 23 '13 at 09:53

1 Answers1

1

Inside a namespace, you have to reference classes (other than functions) with their fully qualified class name or import them first:

$this->zip = new \ZipArchive();

or

namespace phpUnit\Test;
use ZipArchive;

Your "normal" code is probably not using namespaces if it works there.

Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
  • From the discussion above with l-x we'd established that ZipArchive() was not fully qualified using the \ symbol, – peedeeaay Dec 21 '15 at 22:22