1
0

提交代码

This commit is contained in:
2020-08-06 14:50:07 +08:00
parent 9d0d5f4be9
commit d7a848c824
11299 changed files with 1321854 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* @author Jean-François Simon <contact@jfsimon.fr>
*/
abstract class AbstractHandlerTest extends TestCase
{
/** @dataProvider getHandleValueTestData */
public function testHandleValue($value, Token $expectedToken, $remainingContent)
{
$reader = new Reader($value);
$stream = new TokenStream();
$this->assertTrue($this->generateHandler()->handle($reader, $stream));
$this->assertEquals($expectedToken, $stream->getNext());
$this->assertRemainingContent($reader, $remainingContent);
}
/** @dataProvider getDontHandleValueTestData */
public function testDontHandleValue($value)
{
$reader = new Reader($value);
$stream = new TokenStream();
$this->assertFalse($this->generateHandler()->handle($reader, $stream));
$this->assertStreamEmpty($stream);
$this->assertRemainingContent($reader, $value);
}
abstract public function getHandleValueTestData();
abstract public function getDontHandleValueTestData();
abstract protected function generateHandler();
protected function assertStreamEmpty(TokenStream $stream)
{
$property = new \ReflectionProperty($stream, 'tokens');
$property->setAccessible(true);
$this->assertEquals([], $property->getValue($stream));
}
protected function assertRemainingContent(Reader $reader, $remainingContent)
{
if ('' === $remainingContent) {
$this->assertEquals(0, $reader->getRemainingLength());
$this->assertTrue($reader->isEOF());
} else {
$this->assertEquals(\strlen($remainingContent), $reader->getRemainingLength());
$this->assertEquals(0, $reader->getOffset($remainingContent));
}
}
}

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\CommentHandler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
class CommentHandlerTest extends AbstractHandlerTest
{
/** @dataProvider getHandleValueTestData */
public function testHandleValue($value, Token $unusedArgument, $remainingContent)
{
$reader = new Reader($value);
$stream = new TokenStream();
$this->assertTrue($this->generateHandler()->handle($reader, $stream));
// comments are ignored (not pushed as token in stream)
$this->assertStreamEmpty($stream);
$this->assertRemainingContent($reader, $remainingContent);
}
public function getHandleValueTestData()
{
return [
// 2nd argument only exists for inherited method compatibility
['/* comment */', new Token(null, null, null), ''],
['/* comment */foo', new Token(null, null, null), 'foo'],
];
}
public function getDontHandleValueTestData()
{
return [
['>'],
['+'],
[' '],
];
}
protected function generateHandler()
{
return new CommentHandler();
}
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\HashHandler;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
class HashHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return [
['#id', new Token(Token::TYPE_HASH, 'id', 0), ''],
['#123', new Token(Token::TYPE_HASH, '123', 0), ''],
['#id.class', new Token(Token::TYPE_HASH, 'id', 0), '.class'],
['#id element', new Token(Token::TYPE_HASH, 'id', 0), ' element'],
];
}
public function getDontHandleValueTestData()
{
return [
['id'],
['123'],
['<'],
['<'],
['#'],
];
}
protected function generateHandler()
{
$patterns = new TokenizerPatterns();
return new HashHandler($patterns, new TokenizerEscaping($patterns));
}
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\IdentifierHandler;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
class IdentifierHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return [
['foo', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), ''],
['foo|bar', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), '|bar'],
['foo.class', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), '.class'],
['foo[attr]', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), '[attr]'],
['foo bar', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), ' bar'],
];
}
public function getDontHandleValueTestData()
{
return [
['>'],
['+'],
[' '],
['*|foo'],
['/* comment */'],
];
}
protected function generateHandler()
{
$patterns = new TokenizerPatterns();
return new IdentifierHandler($patterns, new TokenizerEscaping($patterns));
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\NumberHandler;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
class NumberHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return [
['12', new Token(Token::TYPE_NUMBER, '12', 0), ''],
['12.34', new Token(Token::TYPE_NUMBER, '12.34', 0), ''],
['+12.34', new Token(Token::TYPE_NUMBER, '+12.34', 0), ''],
['-12.34', new Token(Token::TYPE_NUMBER, '-12.34', 0), ''],
['12 arg', new Token(Token::TYPE_NUMBER, '12', 0), ' arg'],
['12]', new Token(Token::TYPE_NUMBER, '12', 0), ']'],
];
}
public function getDontHandleValueTestData()
{
return [
['hello'],
['>'],
['+'],
[' '],
['/* comment */'],
];
}
protected function generateHandler()
{
$patterns = new TokenizerPatterns();
return new NumberHandler($patterns);
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\StringHandler;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
class StringHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return [
['"hello"', new Token(Token::TYPE_STRING, 'hello', 1), ''],
['"1"', new Token(Token::TYPE_STRING, '1', 1), ''],
['" "', new Token(Token::TYPE_STRING, ' ', 1), ''],
['""', new Token(Token::TYPE_STRING, '', 1), ''],
["'hello'", new Token(Token::TYPE_STRING, 'hello', 1), ''],
["'foo'bar", new Token(Token::TYPE_STRING, 'foo', 1), 'bar'],
];
}
public function getDontHandleValueTestData()
{
return [
['hello'],
['>'],
['1'],
[' '],
];
}
protected function generateHandler()
{
$patterns = new TokenizerPatterns();
return new StringHandler($patterns, new TokenizerEscaping($patterns));
}
}

View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\WhitespaceHandler;
use Symfony\Component\CssSelector\Parser\Token;
class WhitespaceHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return [
[' ', new Token(Token::TYPE_WHITESPACE, ' ', 0), ''],
["\n", new Token(Token::TYPE_WHITESPACE, "\n", 0), ''],
["\t", new Token(Token::TYPE_WHITESPACE, "\t", 0), ''],
[' foo', new Token(Token::TYPE_WHITESPACE, ' ', 0), 'foo'],
[' .foo', new Token(Token::TYPE_WHITESPACE, ' ', 0), '.foo'],
];
}
public function getDontHandleValueTestData()
{
return [
['>'],
['1'],
['a'],
];
}
protected function generateHandler()
{
return new WhitespaceHandler();
}
}

View File

@@ -0,0 +1,253 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use Symfony\Component\CssSelector\Node\FunctionNode;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Parser;
use Symfony\Component\CssSelector\Parser\Token;
class ParserTest extends TestCase
{
/** @dataProvider getParserTestData */
public function testParser($source, $representation)
{
$parser = new Parser();
$this->assertEquals($representation, array_map(function (SelectorNode $node) {
return (string) $node->getTree();
}, $parser->parse($source)));
}
/** @dataProvider getParserExceptionTestData */
public function testParserException($source, $message)
{
$parser = new Parser();
try {
$parser->parse($source);
$this->fail('Parser should throw a SyntaxErrorException.');
} catch (SyntaxErrorException $e) {
$this->assertEquals($message, $e->getMessage());
}
}
/** @dataProvider getPseudoElementsTestData */
public function testPseudoElements($source, $element, $pseudo)
{
$parser = new Parser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($element, (string) $selector->getTree());
$this->assertEquals($pseudo, (string) $selector->getPseudoElement());
}
/** @dataProvider getSpecificityTestData */
public function testSpecificity($source, $value)
{
$parser = new Parser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($value, $selector->getSpecificity()->getValue());
}
/** @dataProvider getParseSeriesTestData */
public function testParseSeries($series, $a, $b)
{
$parser = new Parser();
$selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
$this->assertCount(1, $selectors);
/** @var FunctionNode $function */
$function = $selectors[0]->getTree();
$this->assertEquals([$a, $b], Parser::parseSeries($function->getArguments()));
}
/** @dataProvider getParseSeriesExceptionTestData */
public function testParseSeriesException($series)
{
$parser = new Parser();
$selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
$this->assertCount(1, $selectors);
/** @var FunctionNode $function */
$function = $selectors[0]->getTree();
$this->expectException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
Parser::parseSeries($function->getArguments());
}
public function getParserTestData()
{
return [
['*', ['Element[*]']],
['*|*', ['Element[*]']],
['*|foo', ['Element[foo]']],
['foo|*', ['Element[foo|*]']],
['foo|bar', ['Element[foo|bar]']],
['#foo#bar', ['Hash[Hash[Element[*]#foo]#bar]']],
['div>.foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
['div> .foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
['div >.foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
['div > .foo', ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
["div \n> \t \t .foo", ['CombinedSelector[Element[div] > Class[Element[*].foo]]']],
['td.foo,.bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
['td.foo, .bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
["td.foo\t\r\n\f ,\t\r\n\f .bar", ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
['td.foo,.bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
['td.foo, .bar', ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
["td.foo\t\r\n\f ,\t\r\n\f .bar", ['Class[Element[td].foo]', 'Class[Element[*].bar]']],
['div, td.foo, div.bar span', ['Element[div]', 'Class[Element[td].foo]', 'CombinedSelector[Class[Element[div].bar] <followed> Element[span]]']],
['div > p', ['CombinedSelector[Element[div] > Element[p]]']],
['td:first', ['Pseudo[Element[td]:first]']],
['td :first', ['CombinedSelector[Element[td] <followed> Pseudo[Element[*]:first]]']],
['a[name]', ['Attribute[Element[a][name]]']],
["a[ name\t]", ['Attribute[Element[a][name]]']],
['a [name]', ['CombinedSelector[Element[a] <followed> Attribute[Element[*][name]]]']],
['[name="foo"]', ["Attribute[Element[*][name = 'foo']]"]],
["[name='foo[1]']", ["Attribute[Element[*][name = 'foo[1]']]"]],
["[name='foo[0][bar]']", ["Attribute[Element[*][name = 'foo[0][bar]']]"]],
['a[rel="include"]', ["Attribute[Element[a][rel = 'include']]"]],
['a[rel = include]', ["Attribute[Element[a][rel = 'include']]"]],
["a[hreflang |= 'en']", ["Attribute[Element[a][hreflang |= 'en']]"]],
['a[hreflang|=en]', ["Attribute[Element[a][hreflang |= 'en']]"]],
['div:nth-child(10)', ["Function[Element[div]:nth-child(['10'])]"]],
[':nth-child(2n+2)', ["Function[Element[*]:nth-child(['2', 'n', '+2'])]"]],
['div:nth-of-type(10)', ["Function[Element[div]:nth-of-type(['10'])]"]],
['div div:nth-of-type(10) .aclass', ["CombinedSelector[CombinedSelector[Element[div] <followed> Function[Element[div]:nth-of-type(['10'])]] <followed> Class[Element[*].aclass]]"]],
['label:only', ['Pseudo[Element[label]:only]']],
['a:lang(fr)', ["Function[Element[a]:lang(['fr'])]"]],
['div:contains("foo")', ["Function[Element[div]:contains(['foo'])]"]],
['div#foobar', ['Hash[Element[div]#foobar]']],
['div:not(div.foo)', ['Negation[Element[div]:not(Class[Element[div].foo])]']],
['td ~ th', ['CombinedSelector[Element[td] ~ Element[th]]']],
['.foo[data-bar][data-baz=0]', ["Attribute[Attribute[Class[Element[*].foo][data-bar]][data-baz = '0']]"]],
];
}
public function getParserExceptionTestData()
{
return [
['attributes(href)/html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()],
['attributes(href)', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()],
['html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '/', 4))->getMessage()],
[' ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 1))->getMessage()],
['div, ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 5))->getMessage()],
[' , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 1))->getMessage()],
['p, , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 3))->getMessage()],
['div > ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 6))->getMessage()],
[' > div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '>', 2))->getMessage()],
['foo|#bar', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_HASH, 'bar', 4))->getMessage()],
['#.foo', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '#', 0))->getMessage()],
['.#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()],
[':#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()],
['[*]', SyntaxErrorException::unexpectedToken('"|"', new Token(Token::TYPE_DELIMITER, ']', 2))->getMessage()],
['[foo|]', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_DELIMITER, ']', 5))->getMessage()],
['[#]', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_DELIMITER, '#', 1))->getMessage()],
['[foo=#]', SyntaxErrorException::unexpectedToken('string or identifier', new Token(Token::TYPE_DELIMITER, '#', 5))->getMessage()],
[':nth-child()', SyntaxErrorException::unexpectedToken('at least one argument', new Token(Token::TYPE_DELIMITER, ')', 11))->getMessage()],
['[href]a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_IDENTIFIER, 'a', 6))->getMessage()],
['[rel:stylesheet]', SyntaxErrorException::unexpectedToken('operator', new Token(Token::TYPE_DELIMITER, ':', 4))->getMessage()],
['[rel=stylesheet', SyntaxErrorException::unexpectedToken('"]"', new Token(Token::TYPE_FILE_END, '', 15))->getMessage()],
[':lang(fr', SyntaxErrorException::unexpectedToken('an argument', new Token(Token::TYPE_FILE_END, '', 8))->getMessage()],
[':contains("foo', SyntaxErrorException::unclosedString(10)->getMessage()],
['foo!', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '!', 3))->getMessage()],
];
}
public function getPseudoElementsTestData()
{
return [
['foo', 'Element[foo]', ''],
['*', 'Element[*]', ''],
[':empty', 'Pseudo[Element[*]:empty]', ''],
[':BEfore', 'Element[*]', 'before'],
[':aftER', 'Element[*]', 'after'],
[':First-Line', 'Element[*]', 'first-line'],
[':First-Letter', 'Element[*]', 'first-letter'],
['::befoRE', 'Element[*]', 'before'],
['::AFter', 'Element[*]', 'after'],
['::firsT-linE', 'Element[*]', 'first-line'],
['::firsT-letteR', 'Element[*]', 'first-letter'],
['::Selection', 'Element[*]', 'selection'],
['foo:after', 'Element[foo]', 'after'],
['foo::selection', 'Element[foo]', 'selection'],
['lorem#ipsum ~ a#b.c[href]:empty::selection', 'CombinedSelector[Hash[Element[lorem]#ipsum] ~ Pseudo[Attribute[Class[Hash[Element[a]#b].c][href]]:empty]]', 'selection'],
['video::-webkit-media-controls', 'Element[video]', '-webkit-media-controls'],
];
}
public function getSpecificityTestData()
{
return [
['*', 0],
[' foo', 1],
[':empty ', 10],
[':before', 1],
['*:before', 1],
[':nth-child(2)', 10],
['.bar', 10],
['[baz]', 10],
['[baz="4"]', 10],
['[baz^="4"]', 10],
['#lipsum', 100],
[':not(*)', 0],
[':not(foo)', 1],
[':not(.foo)', 10],
[':not([foo])', 10],
[':not(:empty)', 10],
[':not(#foo)', 100],
['foo:empty', 11],
['foo:before', 2],
['foo::before', 2],
['foo:empty::before', 12],
['#lorem + foo#ipsum:first-child > bar:first-line', 213],
];
}
public function getParseSeriesTestData()
{
return [
['1n+3', 1, 3],
['1n +3', 1, 3],
['1n + 3', 1, 3],
['1n+ 3', 1, 3],
['1n-3', 1, -3],
['1n -3', 1, -3],
['1n - 3', 1, -3],
['1n- 3', 1, -3],
['n-5', 1, -5],
['odd', 2, 1],
['even', 2, 0],
['3n', 3, 0],
['n', 1, 0],
['+n', 1, 0],
['-n', -1, 0],
['5', 0, 5],
];
}
public function getParseSeriesExceptionTestData()
{
return [
['foo'],
['n+'],
];
}
}

View File

@@ -0,0 +1,102 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Parser\Reader;
class ReaderTest extends TestCase
{
public function testIsEOF()
{
$reader = new Reader('');
$this->assertTrue($reader->isEOF());
$reader = new Reader('hello');
$this->assertFalse($reader->isEOF());
$this->assignPosition($reader, 2);
$this->assertFalse($reader->isEOF());
$this->assignPosition($reader, 5);
$this->assertTrue($reader->isEOF());
}
public function testGetRemainingLength()
{
$reader = new Reader('hello');
$this->assertEquals(5, $reader->getRemainingLength());
$this->assignPosition($reader, 2);
$this->assertEquals(3, $reader->getRemainingLength());
$this->assignPosition($reader, 5);
$this->assertEquals(0, $reader->getRemainingLength());
}
public function testGetSubstring()
{
$reader = new Reader('hello');
$this->assertEquals('he', $reader->getSubstring(2));
$this->assertEquals('el', $reader->getSubstring(2, 1));
$this->assignPosition($reader, 2);
$this->assertEquals('ll', $reader->getSubstring(2));
$this->assertEquals('lo', $reader->getSubstring(2, 1));
}
public function testGetOffset()
{
$reader = new Reader('hello');
$this->assertEquals(2, $reader->getOffset('ll'));
$this->assertFalse($reader->getOffset('w'));
$this->assignPosition($reader, 2);
$this->assertEquals(0, $reader->getOffset('ll'));
$this->assertFalse($reader->getOffset('he'));
}
public function testFindPattern()
{
$reader = new Reader('hello');
$this->assertFalse($reader->findPattern('/world/'));
$this->assertEquals(['hello', 'h'], $reader->findPattern('/^([a-z]).*/'));
$this->assignPosition($reader, 2);
$this->assertFalse($reader->findPattern('/^h.*/'));
$this->assertEquals(['llo'], $reader->findPattern('/^llo$/'));
}
public function testMoveForward()
{
$reader = new Reader('hello');
$this->assertEquals(0, $reader->getPosition());
$reader->moveForward(2);
$this->assertEquals(2, $reader->getPosition());
}
public function testToEnd()
{
$reader = new Reader('hello');
$reader->moveToEnd();
$this->assertTrue($reader->isEOF());
}
private function assignPosition(Reader $reader, $value)
{
$position = new \ReflectionProperty($reader, 'position');
$position->setAccessible(true);
$position->setValue($reader, $value);
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Shortcut\ClassParser;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class ClassParserTest extends TestCase
{
/** @dataProvider getParseTestData */
public function testParse($source, $representation)
{
$parser = new ClassParser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($representation, (string) $selector->getTree());
}
public function getParseTestData()
{
return [
['.testclass', 'Class[Element[*].testclass]'],
['testel.testclass', 'Class[Element[testel].testclass]'],
['testns|.testclass', 'Class[Element[testns|*].testclass]'],
['testns|*.testclass', 'Class[Element[testns|*].testclass]'],
['testns|testel.testclass', 'Class[Element[testns|testel].testclass]'],
];
}
}

View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Shortcut\ElementParser;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class ElementParserTest extends TestCase
{
/** @dataProvider getParseTestData */
public function testParse($source, $representation)
{
$parser = new ElementParser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($representation, (string) $selector->getTree());
}
public function getParseTestData()
{
return [
['*', 'Element[*]'],
['testel', 'Element[testel]'],
['testns|*', 'Element[testns|*]'],
['testns|testel', 'Element[testns|testel]'],
];
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class EmptyStringParserTest extends TestCase
{
public function testParse()
{
$parser = new EmptyStringParser();
$selectors = $parser->parse('');
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals('Element[*]', (string) $selector->getTree());
$selectors = $parser->parse('this will produce an empty array');
$this->assertCount(0, $selectors);
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Shortcut\HashParser;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class HashParserTest extends TestCase
{
/** @dataProvider getParseTestData */
public function testParse($source, $representation)
{
$parser = new HashParser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($representation, (string) $selector->getTree());
}
public function getParseTestData()
{
return [
['#testid', 'Hash[Element[*]#testid]'],
['testel#testid', 'Hash[Element[testel]#testid]'],
['testns|#testid', 'Hash[Element[testns|*]#testid]'],
['testns|*#testid', 'Hash[Element[testns|*]#testid]'],
['testns|testel#testid', 'Hash[Element[testns|testel]#testid]'],
];
}
}

View File

@@ -0,0 +1,96 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
class TokenStreamTest extends TestCase
{
public function testGetNext()
{
$stream = new TokenStream();
$stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$stream->push($t2 = new Token(Token::TYPE_DELIMITER, '.', 2));
$stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'title', 3));
$this->assertSame($t1, $stream->getNext());
$this->assertSame($t2, $stream->getNext());
$this->assertSame($t3, $stream->getNext());
}
public function testGetPeek()
{
$stream = new TokenStream();
$stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$stream->push($t2 = new Token(Token::TYPE_DELIMITER, '.', 2));
$stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'title', 3));
$this->assertSame($t1, $stream->getPeek());
$this->assertSame($t1, $stream->getNext());
$this->assertSame($t2, $stream->getPeek());
$this->assertSame($t2, $stream->getPeek());
$this->assertSame($t2, $stream->getNext());
}
public function testGetNextIdentifier()
{
$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$this->assertEquals('h1', $stream->getNextIdentifier());
}
public function testFailToGetNextIdentifier()
{
$this->expectException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));
$stream->getNextIdentifier();
}
public function testGetNextIdentifierOrStar()
{
$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$this->assertEquals('h1', $stream->getNextIdentifierOrStar());
$stream->push(new Token(Token::TYPE_DELIMITER, '*', 0));
$this->assertNull($stream->getNextIdentifierOrStar());
}
public function testFailToGetNextIdentifierOrStar()
{
$this->expectException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));
$stream->getNextIdentifierOrStar();
}
public function testSkipWhitespace()
{
$stream = new TokenStream();
$stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$stream->push($t2 = new Token(Token::TYPE_WHITESPACE, ' ', 2));
$stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'h1', 3));
$stream->skipWhitespace();
$this->assertSame($t1, $stream->getNext());
$stream->skipWhitespace();
$this->assertSame($t3, $stream->getNext());
}
}