PHP code example of wmde / hamcrest-html-matchers

1. Go to this page and download the library: Download wmde/hamcrest-html-matchers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wmde / hamcrest-html-matchers example snippets


$html = '<div>
            <form>
                <input name="password" value="strong password"/>
            </form>
         </div>';

assertThat($html, is(htmlPiece(
    havingChild(
        both(withTagName('form'))
        ->andAlso(
            havingDirectChild(
                allOf(
                    withTagName('input'),
                    withAttribute('name')->havingValue('password'),
                    withAttribute('value')->havingValue(not('weak password')))))))));

    assertThat('<p></p>', is(htmlPiece())); // Just checking that string is a valid piece of HTML
    

    assertThat('<p></p>', htmlPiece(havingRootElement(withTagName('p'))));
    

    assertThat('<p><b></b></p>', htmlPiece(havingRootElement(havingDirectChild(withTagName('b')))));
    

    assertThat('<p><b></b></p>', htmlPiece(havingChild(withTagName('b'))));
    

    assertThat('<p><b></b></p>', htmlPiece(havingChild(withTagName(
        either(equalTo('i'))->orElse(equalTo('b'))
    ))));
    

    assertThat('<p><input 

    assertThat('<p><input data-value="some data"></p>', htmlPiece(havingChild(
        withAttribute(startsWith('data-'))->havingValue('some data'))));
    

  assertThat('<p class="class1 class2 class3"></p>', htmlPiece(havingChild(
        withClass('class2'))));
  

    assertThat('<div><p>this is Some Text</p></div>', htmlPiece(havingChild(
        havingTextContents(containsString('some text')->ignoringCase()))));
    

  assertThat('<form><input id="id-pass" name="password" class="pretty green" word" class="green"