Skip to content

Commit e8c2b45

Browse files
dinamicdbu
authored andcommitted
Fix CND parsing on windows. Squashed fix #51
~ unit testing different platforms for problems with line ending characters ~ unit testing different platforms for problems with line ending characters ~ bug fix for windows environment line endings ~ refactored FileReader to use proper variable name; fileName -> filePath ~ updated unit test - unnecessary else statement ~ using look-ahead; the user no longer needs to know the exact length of the EOL character ~ removed filename and filepath renamed to path, as discussed + unix and windows eol tests ~ renamed method BufferReaderTest::test__construct -> doTestReader() ~ updated file fixtures Add support for Windows line endings in cnd. Fix buffer reader test for changed source string. Fix buffer reader test for changed source string. Fix tests. Fix path in tests bootstrap.
1 parent cfef909 commit e8c2b45

File tree

8 files changed

+89
-36
lines changed

8 files changed

+89
-36
lines changed

src/PHPCR/Util/CND/Reader/BufferReader.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,57 @@
44

55
/**
66
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
7+
* @author Nikola Petkanski <nikola@petkanski.com>
78
*/
89
class BufferReader implements ReaderInterface
910
{
11+
/**
12+
* @var string
13+
*/
1014
protected $eofMarker;
1115

16+
/**
17+
* @var string
18+
*/
1219
protected $buffer;
1320

21+
/**
22+
* @var int
23+
*/
1424
protected $startPos;
1525

26+
/**
27+
* @var int
28+
*/
1629
protected $forwardPos;
1730

31+
/**
32+
* @var int
33+
*/
1834
protected $curLine;
1935

36+
/**
37+
* @var int
38+
*/
2039
protected $curCol;
2140

41+
/**
42+
* @var int
43+
*/
2244
protected $nextCurLine;
2345

46+
/**
47+
* @var int
48+
*/
2449
protected $nextCurCol;
2550

51+
/**
52+
* @param string $buffer
53+
*/
2654
public function __construct($buffer)
2755
{
2856
$this->eofMarker = chr(1);
29-
$this->buffer = $buffer . $this->eofMarker;
57+
$this->buffer = str_replace("\r\n", "\n", $buffer) . $this->eofMarker;
3058

3159
$this->reset();
3260
}
@@ -40,6 +68,9 @@ public function reset()
4068
$this->nextCurLine = $this->nextCurCol = 1;
4169
}
4270

71+
/**
72+
* @return string
73+
*/
4374
public function getEofMarker()
4475
{
4576
return $this->eofMarker;
@@ -48,15 +79,15 @@ public function getEofMarker()
4879
/**
4980
* @return int
5081
*/
51-
function getCurrentLine()
82+
public function getCurrentLine()
5283
{
5384
return $this->curLine;
5485
}
5586

5687
/**
5788
* @return int
5889
*/
59-
function getCurrentColumn()
90+
public function getCurrentColumn()
6091
{
6192
return $this->curCol;
6293
}
@@ -75,6 +106,9 @@ public function currentChar()
75106
return substr($this->buffer, $this->forwardPos, 1);
76107
}
77108

109+
/**
110+
* @return bool
111+
*/
78112
public function isEof()
79113
{
80114
return $this->currentChar() === $this->getEofMarker()
@@ -94,7 +128,7 @@ public function forward()
94128
$this->nextCurCol++;
95129
}
96130

97-
if ($this->current() === PHP_EOL) {
131+
if ($this->current() === "\n") {
98132
$this->nextCurLine++;
99133
$this->nextCurCol = 1;
100134
}

src/PHPCR/Util/CND/Reader/FileReader.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,35 @@
44

55
/**
66
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
7+
* @author Nikola Petkanski <nikola@petkanski.com>
78
*/
89
class FileReader extends BufferReader
910
{
10-
protected $fileName;
11+
/**
12+
* @var string
13+
*/
14+
protected $filePath;
1115

12-
public function __construct($fileName)
16+
/**
17+
* @param string $path
18+
* @throws \InvalidArgumentException
19+
*/
20+
public function __construct($path)
1321
{
14-
if (!file_exists($fileName)) {
15-
throw new \InvalidArgumentException(sprintf("Invalid file '%s'", $fileName));
22+
if (!file_exists($path)) {
23+
throw new \InvalidArgumentException(sprintf("Invalid file '%s'", $path));
1624
}
1725

18-
$this->fileName = $fileName;
26+
$this->path = $path;
1927

20-
parent::__construct(file_get_contents($fileName));
28+
parent::__construct(file_get_contents($path));
2129
}
2230

23-
public function getFileName()
31+
/**
32+
* @return string
33+
*/
34+
public function getPath()
2435
{
25-
return $this->fileName;
36+
return $this->path;
2637
}
2738
}

src/PHPCR/Util/CND/Reader/ReaderInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
interface ReaderInterface
99
{
1010
/**
11-
* @return bool
11+
* @return string
1212
*/
1313
public function getEofMarker();
1414

@@ -17,6 +17,9 @@ public function getEofMarker();
1717
*/
1818
public function currentChar();
1919

20+
/**
21+
* @return bool
22+
*/
2023
public function isEof();
2124

2225
/**

src/PHPCR/Util/CND/Scanner/GenericScanner.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* the token generation to your needs.
1313
*
1414
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
15+
* @author Nikola Petkanski <nikola@petkanski.com>
1516
*/
1617
class GenericScanner extends AbstractScanner
1718
{
@@ -83,13 +84,13 @@ protected function consumeSpaces(ReaderInterface $reader)
8384
*/
8485
protected function consumeNewLine(ReaderInterface $reader)
8586
{
86-
if ($reader->currentChar() === PHP_EOL) {
87+
if ($reader->currentChar() === "\n") {
8788

88-
$token = new GenericToken(GenericToken::TK_NEWLINE, PHP_EOL);
89+
$token = new GenericToken(GenericToken::TK_NEWLINE, "\n");
8990
$this->addToken($reader, $token);
9091

9192

92-
while ($reader->forward() === PHP_EOL) {
93+
while ($reader->forward() === "\n") {
9394
$reader->consume();
9495
$reader->forward();
9596
}
@@ -116,7 +117,7 @@ protected function consumeString(ReaderInterface $reader)
116117
$char = $reader->forwardChar();
117118
while ($char !== $curDelimiter) {
118119

119-
if ($char === PHP_EOL) {
120+
if ($char === "\n") {
120121
throw new ScannerException($reader, "Newline detected in string");
121122
}
122123

@@ -228,7 +229,7 @@ protected function consumeLineComments(ReaderInterface $reader)
228229

229230
// consume to end of line
230231
$char = $reader->currentChar();
231-
while (!$reader->isEof() && $char !== PHP_EOL) {
232+
while (!$reader->isEof() && $char !== "\n") {
232233
$char = $reader->forwardChar();
233234
}
234235
$token = new GenericToken(GenericToken::TK_COMMENT, $reader->consume());

tests/PHPCR/Tests/Util/CND/Reader/BufferReaderTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class BufferReaderTest extends \PHPUnit_Framework_TestCase
88
{
99
public function test__construct()
1010
{
11-
$buffer = "Some random\nstring";
11+
$buffer = "Some random\nor\r\nstring";
1212
$reader = new BufferReader($buffer);
1313

1414
$this->assertInstanceOf('\PHPCR\Util\CND\Reader\BufferReader', $reader);
15-
$this->assertAttributeEquals($buffer . $reader->getEofMarker(), 'buffer', $reader);
15+
$this->assertAttributeEquals(str_replace("\r\n", "\n", $buffer) . $reader->getEofMarker(), 'buffer', $reader);
1616
$this->assertAttributeEquals(0, 'startPos', $reader);
1717
$this->assertAttributeEquals(0, 'forwardPos', $reader);
1818

@@ -55,11 +55,19 @@ public function test__construct()
5555

5656
$this->assertEquals(12, $reader->getCurrentColumn());
5757

58-
$this->assertEquals(PHP_EOL, $reader->forward());
59-
$this->assertEquals(PHP_EOL, $reader->consume());
58+
$this->assertEquals("\n", $reader->forward());
59+
$this->assertEquals("\n", $reader->consume());
6060

6161
$this->assertEquals(2, $reader->getCurrentLine());
6262
$this->assertEquals(1, $reader->getCurrentColumn());
63+
$this->assertEquals('o', $reader->forward());
64+
$this->assertEquals('or', $reader->forward());
65+
$this->assertEquals('or', $reader->consume());
66+
$this->assertEquals("\n", $reader->forward());
67+
$this->assertEquals("\n", $reader->consume());
68+
69+
$this->assertEquals(3, $reader->getCurrentLine());
70+
$this->assertEquals(1, $reader->getCurrentColumn());
6371

6472
$this->assertEquals('s', $reader->forward());
6573
$this->assertEquals('st', $reader->forward());
@@ -69,7 +77,7 @@ public function test__construct()
6977
$this->assertEquals('string', $reader->forward());
7078
$this->assertEquals('string', $reader->consume());
7179

72-
$this->assertEquals(2, $reader->getCurrentLine());
80+
$this->assertEquals(3, $reader->getCurrentLine());
7381
$this->assertEquals(7, $reader->getCurrentColumn());
7482

7583
$this->assertEquals($reader->getEofMarker(), $reader->forward());

tests/PHPCR/Tests/Util/CND/Reader/FileReaderTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class FileReaderTest extends \PHPUnit_Framework_TestCase
88
{
99
public function setUp()
1010
{
11-
$this->filename = __DIR__ . '/../Fixtures/files/TestFile.txt';
12-
$this->reader = new FileReader($this->filename);
11+
$this->filepath = __DIR__ . '/../Fixtures/files/TestFile.txt';
12+
$this->reader = new FileReader($this->filepath);
1313

1414
$this->lines = array(
1515
'This is a test file...',
@@ -18,7 +18,6 @@ public function setUp()
1818
''
1919
);
2020

21-
$this->content = file_get_contents($this->filename);
2221
$this->chars = array_merge(
2322
preg_split('//', $this->lines[0], -1, PREG_SPLIT_NO_EMPTY),
2423
array("\n", "\n"),
@@ -35,9 +34,9 @@ public function test__construct_fileNotFound()
3534
$reader = new FileReader('unexisting_file');
3635
}
3736

38-
public function testGetFileName()
37+
public function testGetPath()
3938
{
40-
$this->assertEquals($this->filename, $this->reader->getFileName());
39+
$this->assertEquals($this->filepath, $this->reader->getPath());
4140
}
4241

4342
public function testGetNextChar()
@@ -54,8 +53,6 @@ public function testGetNextChar()
5453
break;
5554
}
5655

57-
//var_dump('Expected:' . $this->chars[$i] . ', found: ' . $peek);
58-
5956
$this->assertEquals($curLine, $this->reader->getCurrentLine());
6057
$this->assertEquals($curCol, $this->reader->getCurrentColumn());
6158

tests/PHPCR/Tests/Util/CND/Scanner/GenericScannerTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ protected function assertTokens($tokens, TokenQueue $queue)
149149

150150
$this->assertFalse($queue->isEof(), 'There is no more tokens, expected = ' . $expectedToken[1]);
151151

152-
//var_dump("Expected: {$expectedToken[1]}, Found: {$token->getData()}");
153-
154152
$this->assertToken($expectedToken[0], $expectedToken[1], $token);
155153

156154
$token = $queue->next();
@@ -162,7 +160,6 @@ protected function assertTokens($tokens, TokenQueue $queue)
162160

163161
protected function assertToken($type, $data, Token $token)
164162
{
165-
//var_dump($token);
166163
$this->assertEquals($type, $token->getType(),
167164
sprintf('Expected token [%s, %s], found [%s, %s]', Token::getTypeName($type), $data, Token::getTypeName($token->getType()), $token->getData()));
168165

tests/bootstrap.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
die('PHPUnit MockObject plugin is required, at least 1.0.8 version');
1717
}
1818

19-
$vendorDir = __DIR__.'/../vendor';
20-
21-
$file = $vendorDir.'/autoload.php';
19+
// $file2 for run tests if phpcr-utils lib inside of vendor directory.
20+
$file = __DIR__.'/../vendor/autoload.php';
21+
$file2 = __DIR__.'/../../../autoload.php';
2222
if (file_exists($file)) {
2323
$autoload = require_once $file;
24+
} elseif (file_exists($file2)) {
25+
$autoload = require_once $file2;
2426
} else {
2527
throw new RuntimeException('Install dependencies to run test suite.');
2628
}

0 commit comments

Comments
 (0)