'); - } - - /** - * @test - */ - public function assertFails_WhenTagInHtmlDoesNotHaveExpectedAttribute() { - $html = ''; - - $this->setExpectedException(AssertionError::class); - assertThat($html, is(htmlPiece(havingChild( - ComplexTagMatcher::tagMatchingOutline(''))))); - } - - /** - * @test - */ - public function assertPasses_WhenTagInHtmlHasExpectedAttribute() { - $html = ''; - - assertThat($html, is(htmlPiece(havingChild( - ComplexTagMatcher::tagMatchingOutline(''))))); - } - - /** - * @test - */ - public function assertFails_WhenTagInHtmlDoesNotHaveAllExpectedAttribute() { - $html = ''; - - $this->setExpectedException(AssertionError::class); - assertThat($html, is(htmlPiece(havingChild( - ComplexTagMatcher::tagMatchingOutline(''))))); - } - - /** - * @test - */ - public function assertPasses_WhenExpectBooleanAttributeButItIsThereWithSomeValue() { - $html = ''; - - assertThat($html, is(htmlPiece(havingChild( - ComplexTagMatcher::tagMatchingOutline(''))))); - } - - /** - * @test - */ - public function assertFails_WhenExpectAttributeWithEmptyValueButItIsNotEmpty() { - $html = ''; - - $this->setExpectedException(AssertionError::class); - assertThat($html, is(htmlPiece(havingChild( - ComplexTagMatcher::tagMatchingOutline(''))))); - } - - /** - * @test - */ - public function assertPasses_WhenGivenTagHasExpectedClass() { - $html = ''; - - assertThat($html, is(htmlPiece(havingChild( - ComplexTagMatcher::tagMatchingOutline(''))))); - } - - /** - * @test - */ - public function assertFails_WhenGivenTagDoesNotHaveExpectedClass() { - $html = ''; - - $this->setExpectedException(AssertionError::class); - assertThat($html, is(htmlPiece(havingChild( - ComplexTagMatcher::tagMatchingOutline(''))))); - } - - /** - * @test - */ - public function toleratesExtraSpacesInClassDescription() { - $html = ''; - - assertThat($html, is(htmlPiece(havingChild( - ComplexTagMatcher::tagMatchingOutline(''))))); - } +class ComplexTagMatcherTest extends \PHPUnit\Framework\TestCase { + + /** + * @test + */ + public function assertPasses_WhenTagInHtmlHasSameTagName() { + $html = ''; + + assertThat( $html, is( htmlPiece( havingChild( ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } + + /** + * @test + */ + public function assertFails_WhenTagInHtmlIsDiffersFromGivenTagName() { + $html = ''; + + $this->setExpectedException( AssertionError::class ); + assertThat( $html, is( htmlPiece( havingChild( ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } + + /** + * @test + */ + public function canNotCreateMatcherWithEmptyDescription() { + $this->setExpectedException( \Exception::class ); + ComplexTagMatcher::tagMatchingOutline( '' ); + } + + /** + * @test + */ + public function canNotCreateMatcherExpectingTwoElements() { + $this->setExpectedException( \Exception::class ); + ComplexTagMatcher::tagMatchingOutline( '' ); + } + + /** + * @test + */ + public function canNotCreateMatcherWithChildElement() { + $this->setExpectedException( \Exception::class ); + ComplexTagMatcher::tagMatchingOutline( '
' ); + } + + /** + * @test + */ + public function assertFails_WhenTagInHtmlDoesNotHaveExpectedAttribute() { + $html = ''; + + $this->setExpectedException( AssertionError::class ); + assertThat( $html, is( htmlPiece( havingChild( + ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } + + /** + * @test + */ + public function assertPasses_WhenTagInHtmlHasExpectedAttribute() { + $html = ''; + + assertThat( $html, is( htmlPiece( havingChild( + ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } + + /** + * @test + */ + public function assertFails_WhenTagInHtmlDoesNotHaveAllExpectedAttribute() { + $html = ''; + + $this->setExpectedException( AssertionError::class ); + assertThat( $html, is( htmlPiece( havingChild( + ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } + + /** + * @test + */ + public function assertPasses_WhenExpectBooleanAttributeButItIsThereWithSomeValue() { + $html = ''; + + assertThat( $html, is( htmlPiece( havingChild( + ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } + + /** + * @test + */ + public function assertFails_WhenExpectAttributeWithEmptyValueButItIsNotEmpty() { + $html = ''; + + $this->setExpectedException( AssertionError::class ); + assertThat( $html, is( htmlPiece( havingChild( + ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } + + /** + * @test + */ + public function assertPasses_WhenGivenTagHasExpectedClass() { + $html = ''; + + assertThat( $html, is( htmlPiece( havingChild( + ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } + + /** + * @test + */ + public function assertFails_WhenGivenTagDoesNotHaveExpectedClass() { + $html = ''; + + $this->setExpectedException( AssertionError::class ); + assertThat( $html, is( htmlPiece( havingChild( + ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } + + /** + * @test + */ + public function toleratesExtraSpacesInClassDescription() { + $html = ''; + + assertThat( $html, is( htmlPiece( havingChild( + ComplexTagMatcher::tagMatchingOutline( '' ) ) ) ) ); + } } diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 11a423b..75d464d 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -5,198 +5,194 @@ use Hamcrest\AssertionError; use Hamcrest\Matcher; -class FunctionsTest extends \PHPUnit_Framework_TestCase -{ +class FunctionsTest extends \PHPUnit\Framework\TestCase { - /** - * @test - */ - public function havingRootElement_MultipleRootTags_ThrowsException() - { - //TODO Does it make sense? - $html = ''; + /** + * @test + */ + public function havingRootElement_MultipleRootTags_ThrowsException() { + // TODO Does it make sense? + $html = ''; - $this->setExpectedException(AssertionError::class); - assertThat($html, is(htmlPiece(havingRootElement()))); - } + $this->setExpectedException( AssertionError::class ); + assertThat( $html, is( htmlPiece( havingRootElement() ) ) ); + } - /** - * @test - * @dataProvider dataProvider_ElementExists - */ - public function matcherCanFindElement($html, $matcher) { - assertThat($html, is(htmlPiece($matcher))); - } + /** + * @test + * @dataProvider dataProvider_ElementExists + */ + public function matcherCanFindElement( $html, $matcher ) { + assertThat( $html, is( htmlPiece( $matcher ) ) ); + } - /** - * @test - * @dataProvider dataProvider_ElementDoesNotExist - */ - public function matcherCantFindElement($html, $matcher, Matcher $messageMatcher) { - $thrownException = null; - try { - assertThat($html, is(htmlPiece($matcher))); - } catch (\Exception $e) { - $thrownException = $e; - } + /** + * @test + * @dataProvider dataProvider_ElementDoesNotExist + */ + public function matcherCantFindElement( $html, $matcher, Matcher $messageMatcher ) { + $thrownException = null; + try { + assertThat( $html, is( htmlPiece( $matcher ) ) ); + } catch ( \Exception $e ) { + $thrownException = $e; + } - assertThat($thrownException, is(anInstanceOf(AssertionError::class))); - assertThat($thrownException->getMessage(), $messageMatcher); - } + assertThat( $thrownException, is( anInstanceOf( AssertionError::class ) ) ); + assertThat( $thrownException->getMessage(), $messageMatcher ); + } - public function dataProvider_ElementExists() - { - return [ - 'htmlPiece - simple case' => [ - '', - null - ], - 'havingRootElement - has root element' => [ - '', - havingRootElement() - ], - 'withTagName - simple case' => [ - '
', - havingRootElement(withTagName('p')) - ], - 'havingDirectChild - without qualifier' => [ - '', - havingDirectChild() - ], - 'havingDirectChild - nested structure' => [ - '
', - havingDirectChild(havingDirectChild(withTagName('b'))) - ], - 'havingChild - target tag is not first' => [ - '', - havingChild(withTagName('b')) - ], - 'havingChild - target tag is nested' => [ - '
', - havingChild(withTagName('i')) - ], - 'withAttribute - select element by attribute name only' => [ - '', - havingChild(withAttribute('name')) - ], - 'withAttribute - select element by attribute name and value' => [ - '', - havingChild(withAttribute('name')->havingValue('something')) - ], - 'withClass - exact match' => [ - '', - havingChild(withClass('test-class')) - ], - 'withClass - one of the classes' => [ - '', - havingChild(withClass('class2')) - ], - 'withClass - classes separated with tab' => [ - "", - havingChild(withClass('class2')) - ], - 'havingTextContents' => [ - '
this is some text
', - havingChild(havingTextContents(containsString('some text'))) - ], - 'havingTextContents - unicode text' => [ - 'какой-то текст
', - havingChild(havingTextContents(containsString('какой-то текст'))) - ], - 'tagMatchingOutline' => [ - '', - havingChild(tagMatchingOutline('')) - ], - ]; - } + public function dataProvider_ElementExists() { + return [ + 'htmlPiece - simple case' => [ + '', + null + ], + 'havingRootElement - has root element' => [ + '', + havingRootElement() + ], + 'withTagName - simple case' => [ + '', + havingRootElement( withTagName( 'p' ) ) + ], + 'havingDirectChild - without qualifier' => [ + '', + havingDirectChild() + ], + 'havingDirectChild - nested structure' => [ + '
', + havingDirectChild( havingDirectChild( withTagName( 'b' ) ) ) + ], + 'havingChild - target tag is not first' => [ + '', + havingChild( withTagName( 'b' ) ) + ], + 'havingChild - target tag is nested' => [ + '
', + havingChild( withTagName( 'i' ) ) + ], + 'withAttribute - select element by attribute name only' => [ + '', + havingChild( withAttribute( 'name' ) ) + ], + 'withAttribute - select element by attribute name and value' => [ + '', + havingChild( withAttribute( 'name' )->havingValue( 'something' ) ) + ], + 'withClass - exact match' => [ + '', + havingChild( withClass( 'test-class' ) ) + ], + 'withClass - one of the classes' => [ + '', + havingChild( withClass( 'class2' ) ) + ], + 'withClass - classes separated with tab' => [ + "", + havingChild( withClass( 'class2' ) ) + ], + 'havingTextContents' => [ + '
this is some text
', + havingChild( havingTextContents( containsString( 'some text' ) ) ) + ], + 'havingTextContents - unicode text' => [ + 'какой-то текст
', + havingChild( havingTextContents( containsString( 'какой-то текст' ) ) ) + ], + 'tagMatchingOutline' => [ + '', + havingChild( tagMatchingOutline( '' ) ) + ], + ]; + } - public function dataProvider_ElementDoesNotExist() - { - return [ - 'htmlPiece - messed up tags' => [ - '', - null, - allOf(containsString('html piece'), containsString('there was parsing error')) - ], - 'htmlPiece - prints passed html on failure' => [ - '', - havingRootElement(withTagName('b')), - containsString('') - ], - 'withTagName - simple case' => [ - '', - havingRootElement(withTagName('b')), - allOf(containsString('having root element'), - containsString('with tag name "b"'), - containsString('root element tag name was "p"')), - ], - 'havingDirectChild - no direct child' => [ - '', - havingDirectChild(havingDirectChild()), - allOf(containsString('having direct child'), - containsString('with direct child with no direct children')), - ], - 'havingDirectChild - single element' => [ - '', - havingDirectChild(withTagName('b')), - allOf(containsString('having direct child'), - containsString('with tag name "b"')), - ], - 'havingDirectChild - nested matcher' => [ - '
', - havingDirectChild(havingDirectChild(withTagName('p'))), - both(containsString('having direct child having direct child with tag name "p"')) - ->andAlso(containsString('direct child with direct child tag name was "b"')) - ], - 'havingChild - no children' => [ - '', - havingDirectChild(havingChild()), - both(containsString('having direct child having child')) - ->andAlso(containsString('having no children')) - ], - 'havingChild - target tag is absent' => [ - '
', - havingChild(withTagName('br')), - both(containsString('having child with tag name "br"')) - ->andAlso(containsString('having no children with tag name "br"')) - ], + public function dataProvider_ElementDoesNotExist() { + return [ + 'htmlPiece - messed up tags' => [ + '', + null, + allOf( containsString( 'html piece' ), containsString( 'there was parsing error' ) ) + ], + 'htmlPiece - prints passed html on failure' => [ + '', + havingRootElement( withTagName( 'b' ) ), + containsString( '' ) + ], + 'withTagName - simple case' => [ + '
', + havingRootElement( withTagName( 'b' ) ), + allOf( containsString( 'having root element' ), + containsString( 'with tag name "b"' ), + containsString( 'root element tag name was "p"' ) ), + ], + 'havingDirectChild - no direct child' => [ + '', + havingDirectChild( havingDirectChild() ), + allOf( containsString( 'having direct child' ), + containsString( 'with direct child with no direct children' ) ), + ], + 'havingDirectChild - single element' => [ + '', + havingDirectChild( withTagName( 'b' ) ), + allOf( containsString( 'having direct child' ), + containsString( 'with tag name "b"' ) ), + ], + 'havingDirectChild - nested matcher' => [ + '
', + havingDirectChild( havingDirectChild( withTagName( 'p' ) ) ), + both( containsString( 'having direct child having direct child with tag name "p"' ) ) + ->andAlso( containsString( 'direct child with direct child tag name was "b"' ) ) + ], + 'havingChild - no children' => [ + '', + havingDirectChild( havingChild() ), + both( containsString( 'having direct child having child' ) ) + ->andAlso( containsString( 'having no children' ) ) + ], + 'havingChild - target tag is absent' => [ + '
', + havingChild( withTagName( 'br' ) ), + both( containsString( 'having child with tag name "br"' ) ) + ->andAlso( containsString( 'having no children with tag name "br"' ) ) + ], - 'withAttribute - select element by attribute name only' => [ - '', - havingChild(withAttribute('value')), - both(containsString('having child with attribute "value"')) - ->andAlso(containsString('having no children with attribute "value"')) - ], - 'withAttribute - select element by attribute name and value' => [ - '', - havingChild(withAttribute('name')->havingValue('something')), - both(containsString('having child with attribute "name" having value "something"')) - ->andAlso(containsString('having no children with attribute "name" having value "something"')) - ], - 'withClass - no class' => [ - '', - havingChild(withClass('test-class')), - both(containsString('having child with class "test-class"')) - ->andAlso(containsString('having no children with class "test-class"')) - ], - 'havingTextContents' => [ - '
this is some text
this is some text