Error "class Domdocument not found" when trying to run Phpunit

Asked

Viewed 2,181 times

2

When trying to turn the command phpunit on the command line, I received the following error:

Class 'Domdocument' not found

This wasn’t happening before. I remember having removed and installed PHP again.

What can cause this error, since DomDocument is a native PHP class?

Note: I am using Ubuntu operating system.

3 answers

3

Some Linux distributions separate the DOM extension into another package to make the installation cleaner and more organized.

I don’t remember the name of the package in Ubuntu, but it’s something similar to php-xml. After installing, make sure the extension is active in php.

You can check by looking through phpinfo();

If not active, just add the extension to your php.ini:

extension=dom.so
  • That’s right, it’s the php-xml that was missing :p

  • :) See the tour to better understand how to thank.

  • @jlHertel think he’s not thanking, he’s just probably stating it should be that way, but the question is more Ubuntu than PHP.

3


To install in Ubuntu I think this is the command, if it is php5.6:

sudo apt-get install php5.6-xml

Other linux based on Debian or older versions of Ubuntu I think works like this:

sudo apt-get install php5-dom

If it’s the 7.0 it would be this:

sudo apt-get install php7.0-xml

php7.1:

sudo apt-get install php7.1-xml

The only thing that can change is if you are using via PPA, so it will depend on how you installed it

  • Only missing the php5.6 :p

  • @Wallacemaxters thought the php5-dom adjusted to the "current" version of your php.

  • In this case, it no longer works. That’s only if you use the PPA that has been deprecated. Now you install using php5.6-* and php7.0-*

0

For those using Phpunit, a good check in addition to seeing if the php-xml is installed and active, is to call Domdocument after namespace declaration:

namespace SeuNameSpace\SeuNameSpace;

use DOMDocument; //<--- aqui
use PHPUnit\Framework\TestCase;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.