Skip to content

Commit ca1f10a

Browse files
committed
testing
1 parent d1858d9 commit ca1f10a

File tree

6 files changed

+191
-122
lines changed

6 files changed

+191
-122
lines changed

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
7+
- 7.2
8+
9+
env:
10+
matrix:
11+
- COMPOSER_FLAGS="--prefer-lowest"
12+
- COMPOSER_FLAGS=""
13+
14+
before_script:
15+
- travis_retry composer self-update
16+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
17+
18+
script:
19+
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
"require": {
1515
"php": "^5.6 || ^7.0"
1616
},
17+
"require-dev": {
18+
"phpunit/phpunit": "^6.3"
19+
},
1720
"autoload": {
1821
"psr-4": {
19-
"Descom\\File_Encoding\\": "src"
22+
"Descom\\File\\": "src/"
2023
}
2124
},
22-
"require-dev": {
23-
"phpunit/phpunit": "^5.7"
24-
},
2525
"autoload-dev": {
2626
"psr-4": {
27-
"Descom\\File_Encoding\\Test\\": "tests"
27+
"Descom\\File\\Test\\": "tests"
2828
}
2929
},
3030
"scripts": {
31-
"test": "phpunit"
31+
"test": "./vendor/bin/phpunit"
3232
}
3333
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="League Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

src/Encoding.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Descom\File;
4+
5+
class Encoding
6+
{
7+
/**
8+
* Encode file.
9+
*
10+
* @param string $file Original file
11+
* @param string $encoding_to Encoding to encode file
12+
* @param string $encodings_detected Ordered list of encodings
13+
*
14+
* @return bool
15+
*/
16+
public function encodeFile($file, $encoding_to = 'UTF-8', $encodings_detected = 'UTF-8,ISO-8859-1,WINDOWS-1252')
17+
{
18+
$encoding_original = $this->detectEncoding($file, $encodings_detected);
19+
20+
if ($encoding_original !== false && $encoding_original != $encoding_to) {
21+
try {
22+
$fileW = $file.'.tmp';
23+
$handleR = @fopen($file, 'r');
24+
$handleW = @fopen($fileW, 'w');
25+
if ($handleR && $handleW) {
26+
while ($line = fgets($handleR, 4096)) {
27+
if (fwrite($handleW, mb_convert_encoding($line, $encoding_to, $encoding_original), 4096) === false) {
28+
fclose($handleR);
29+
fclose($handleW);
30+
return false;
31+
}
32+
}
33+
fclose($handleR);
34+
fclose($handleW);
35+
if (rename($fileW, $file) === false) {
36+
return false;
37+
}
38+
} else {
39+
return false;
40+
}
41+
} catch (Exception $e) {
42+
return false;
43+
}
44+
}
45+
46+
return $this->checkEncoding($file, $encoding_to);
47+
}
48+
49+
/**
50+
* Detect file encoding.
51+
*
52+
* @param string $file Original file
53+
* @param string $encodings_detected Ordered list of encodings
54+
*
55+
* @return mixed String encoding or false
56+
*/
57+
public function detectEncoding($file, $encodings_detected)
58+
{
59+
$encodings = explode(',', $encodings_detected);
60+
61+
if (count($encodings)) {
62+
$handle = @fopen($file, 'r');
63+
if ($handle) {
64+
while ($line = fgets($handle, 4096)) {
65+
$encoding = mb_detect_encoding($line, $encodings_detected, true);
66+
if ($encoding != $encodings[0]) {
67+
fclose($handle);
68+
69+
return $encoding;
70+
}
71+
}
72+
fclose($handle);
73+
74+
return $encodings[0];
75+
}
76+
}
77+
78+
return false;
79+
}
80+
81+
/**
82+
* Check if file encoding matches encoding_to.
83+
*
84+
* @param string $file Original file
85+
*
86+
* @return bool
87+
*/
88+
public function checkEncoding($file, $encoding_to)
89+
{
90+
$handle = @fopen($file, 'r');
91+
92+
if ($handle) {
93+
while ($line = fgets($handle, 4096)) {
94+
if (!mb_check_encoding($line, $encoding_to)) {
95+
fclose($handle);
96+
97+
return false;
98+
}
99+
}
100+
fclose($handle);
101+
} else {
102+
return false;
103+
}
104+
105+
return true;
106+
}
107+
}

src/FileEncoding.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

tests/FileEncodingTest.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,56 @@
11
<?php
22

3-
namespace Descom\FileEncoding\Test;
3+
namespace Descom\File\Test;
44

5-
use Descom\FileEncoding;
5+
use Descom\File\Encoding;
6+
use PHPUnit\Framework\TestCase;
67

78
class FileEncodingTest extends TestCase
89
{
910
public function setUp()
1011
{
1112
parent::setUp();
12-
copy('files/test_iso88591.txt', 'iso88591.txt');
13-
copy('files/test_utf8.txt', 'utf8.txt');
13+
copy(__DIR__.'/files/test_iso-8859-1.txt', __DIR__.'/files/iso88591.txt');
14+
copy(__DIR__.'/files/test_utf8.txt', __DIR__.'/files/utf8.txt');
15+
}
16+
17+
public function close()
18+
{
19+
unlink(__DIR__.'/files/iso88591.txt');
20+
unlink(__DIR__.'/files/utf8.txt');
1421
}
1522

1623
/** @test */
1724
public function test_encoding_iso88591_as_UTF8()
1825
{
19-
$encoding = new FileEncoding();
20-
$this->assertTrue($encoding->detectEncoding('iso88591.txt', 'UTF-8,ISO-8859-1,WINDOWS-1252') == 'ISO-8859-1');
21-
$this->assertTrue($encoding->encodeFile('iso88591.txt', 'UTF-8', 'UTF-8,ISO-8859-1,WINDOWS-1252'));
22-
$this->assertTrue($encoding->checkEncoding('iso88591.txt','UTF-8'));
26+
$encoding = new Encoding();
27+
28+
$this->assertTrue($encoding->detectEncoding(__DIR__.'/files/iso88591.txt', 'UTF-8,ISO-8859-1,WINDOWS-1252') == 'ISO-8859-1');
29+
$this->assertTrue($encoding->encodeFile(__DIR__.'/files/iso88591.txt', 'UTF-8', 'UTF-8,ISO-8859-1,WINDOWS-1252'));
30+
$this->assertTrue($encoding->checkEncoding(__DIR__.'/files/iso88591.txt','UTF-8'));
31+
32+
$this->close();
2333
}
2434

2535
/** @test */
2636
public function test_encoding_UTF8_as_UTF8()
2737
{
28-
$encoding = new FileEncoding();
29-
$this->assertTrue($encoding->detectEncoding('utf8.txt', 'UTF-8,ISO-8859-1,WINDOWS-1252') == 'UTF-8');
30-
$this->assertTrue($result = $encoding->encodeFile('utf8.txt'));
31-
$this->assertTrue($encoding->checkEncoding('utf8.txt','UTF-8'));
38+
$encoding = new Encoding();
39+
$this->assertTrue($encoding->detectEncoding('tests/files/utf8.txt', 'UTF-8,ISO-8859-1,WINDOWS-1252') == 'UTF-8');
40+
$this->assertTrue($result = $encoding->encodeFile('tests/files/utf8.txt'));
41+
$this->assertTrue($encoding->checkEncoding('tests/files/utf8.txt','UTF-8'));
42+
43+
$this->close();
3244
}
3345

3446
/** @test */
3547
public function test_encoding_file_not_exists()
3648
{
37-
$encoding = new FileEncoding();
38-
$this->assertFalse($encoding->detectEncoding('test.txt', 'UTF-8,ISO-8859-1,WINDOWS-1252') == 'UTF-8');
39-
$this->assertFalse($result = $encoding->encodeFile('test.txt'));
40-
$this->assertFalse($encoding->checkEncoding('test.txt','UTF-8'));
49+
$encoding = new Encoding();
50+
$this->assertFalse($encoding->detectEncoding('tests/files/test.txt', 'UTF-8,ISO-8859-1,WINDOWS-1252') == 'UTF-8');
51+
$this->assertFalse($result = $encoding->encodeFile('tests/files/test.txt'));
52+
$this->assertFalse($encoding->checkEncoding('tests/files/test.txt','UTF-8'));
53+
54+
$this->close();
4155
}
4256
}

0 commit comments

Comments
 (0)