Tester\DomQuery::fromHtml error

Notice: This thread is very old.

7 years ago

xlilien
Member | 28
+
0
-

I tried to write some tests using Nette Tester, but when using the Tester\DomQuery::fromHtml($html) method, weird errors appears (something like Attribute class redefined in Entity). I tried to debug the code and I realized that when I change the line 33 in Tester\DomQuery from

<?php
$dom->loadHTML($html);
?>

to

<?php
@$dom->loadHTML($html);
?>

errors caused by DOMDocument dissappears and my tests are able to pass. So I just wanted to ask whether there is some specific reason why not to use the @ sign in the method to suppress these errors.

Last edited by xlilien (2013-06-20 19:24)

7 years ago

Honza Marek
Member | 1674
+
0
-

You should avoid using @ if possible. It hides not only errors you know about but all of them.

I once used @ in test. Few months later I realized I have a mistake in my test, but everything seemed to be ok, because test results were green.

7 years ago

Milo
Nette Core | 1153
+
0
-

@xlilien: I guess, you have an invalid HTML code. DOMDocument method loadHTML() can deal with it, it is written for this purpose and expects an errors in HTML code. Using the @ operator is legitimate in this case, but it isn't in Tester libs. It would cover more serious problem.

If you can, fix the HTML code. Use W3C Validator to found what is wrong.

The other way is to catch errors by LibXML internal error handling:

libxml_use_internal_errors(TRUE);
Tester\DomQuery::fromHtml($html);
$errors = libxml_get_errors();  // or just ignore it
libxml_use_internal_errors(FALSE);

or just

@Tester\DomQuery::fromHtml($html);

but as Honza Marek wrote, it is potentional weakness.