Skip to content

Commit af9652d

Browse files
authored
Add test case for #133 (#146)
* prove that #133 is not a bug * Switch base image to Ubuntu and update dependencies to sync local dev env with prod env
1 parent 7a16edc commit af9652d

4 files changed

Lines changed: 84 additions & 9 deletions

File tree

Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
FROM php:8.5.4-cli AS base
1+
FROM ubuntu:latest AS base
22
RUN apt-get update \
3-
&& apt-get install -y libmagickwand-dev tesseract-ocr unzip \
4-
&& pecl install imagick \
5-
&& docker-php-ext-enable imagick
3+
&& apt-get install -y libmagickwand-dev tesseract-ocr unzip php-cli php-imagick php-xml php-mbstring
64

75
FROM base AS build
86
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
@@ -18,7 +16,10 @@ COPY . .
1816

1917
FROM build AS test
2018
# Install xdebug
21-
RUN pecl install xdebug && docker-php-ext-enable xdebug
19+
RUN apt install -y php-xdebug
20+
# Copy current source again to ensure it's not cached from build stage if it was already existing
21+
COPY classes/ ./classes/
22+
COPY tests/ ./tests/
2223
# Run Static Analysis
2324
RUN vendor/bin/phpstan analyse --no-progress src classes
2425
# Run Unit Tests

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"thiagoalessio/tesseract_ocr": "^2.13",
1616
"nohn/analogmeterreader": "^1.2",
1717
"php": ">=8.3",
18-
"ext-imagick": "^3.8",
18+
"ext-imagick": "^3.7",
1919
"ext-json": ">=8.3"
2020
},
2121
"require-dev": {

tests/WatermeterReaderVariantTest.php

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@
2626

2727
use nohn\Watermeter\Reader;
2828
use PHPUnit\Framework\TestCase;
29+
use function PHPUnit\Framework\assertEmpty;
2930

3031
class WatermeterReaderVariantTest extends TestCase
3132
{
3233
private $variants = array(
3334
'demo' => array(
3435
'lastValue' => 819.7668,
36+
'expectedReadout' => 819.7797,
3537
'expectedValue' => 819.7797,
3638
'hasErrors' => false,
37-
'expectedErrors' => array(),
39+
'expectedErrors' => false,
3840
'config' => array(
3941
'logging' => false,
4042
'maxThreshold' => 0.1,
@@ -60,6 +62,7 @@ class WatermeterReaderVariantTest extends TestCase
6062
),
6163
'demo_offset_one_off_pass' => array(
6264
'lastValue' => 819.6798,
65+
'expectedReadout' => 819.7797,
6366
'expectedValue' => 819.7797,
6467
'hasErrors' => false,
6568
'expectedErrors' => array(),
@@ -88,6 +91,7 @@ class WatermeterReaderVariantTest extends TestCase
8891
),
8992
'demo_offset_one_off_fail' => array(
9093
'lastValue' => 819.6796,
94+
'expectedReadout' => 819.6796,
9195
'expectedValue' => 819.6796,
9296
'hasErrors' => true,
9397
'expectedErrors' => array(
@@ -123,6 +127,7 @@ class WatermeterReaderVariantTest extends TestCase
123127
'regular1' =>
124128
array(
125129
'lastValue' => 1189.1668,
130+
'expectedReadout' => 1189.2776,
126131
'expectedValue' => 1189.2776,
127132
'hasErrors' => false,
128133
'expectedErrors' => array(),
@@ -198,6 +203,7 @@ class WatermeterReaderVariantTest extends TestCase
198203
'regular1_with_offset' =>
199204
array(
200205
'lastValue' => 1189.1668,
206+
'expectedReadout' => 1189.2776,
201207
'expectedValue' => 3189.2776,
202208
'hasErrors' => false,
203209
'expectedErrors' => array(),
@@ -274,6 +280,7 @@ class WatermeterReaderVariantTest extends TestCase
274280
'regular1_with_negative_offset' =>
275281
array(
276282
'lastValue' => 1189.1668,
283+
'expectedReadout' => 1189.2776,
277284
'expectedValue' => 189.2776,
278285
'hasErrors' => false,
279286
'expectedErrors' => array(),
@@ -350,6 +357,7 @@ class WatermeterReaderVariantTest extends TestCase
350357
'regular1_decreasing' =>
351358
array(
352359
'lastValue' => 1189.2777,
360+
'expectedReadout' => 1189.2777,
353361
'expectedValue' => 1189.2777,
354362
'hasErrors' => true,
355363
'expectedErrors' => array(
@@ -431,6 +439,7 @@ class WatermeterReaderVariantTest extends TestCase
431439
'regular2_ocr_failing' =>
432440
array(
433441
'lastValue' => 1189.1668,
442+
'expectedReadout' => 1189.2776,
434443
'expectedValue' => 1189.2776,
435444
'hasErrors' => true,
436445
'expectedErrors' => array(
@@ -508,6 +517,7 @@ class WatermeterReaderVariantTest extends TestCase
508517
'regular2_with_brightness_contrast' =>
509518
array(
510519
'lastValue' => 1189.1668,
520+
'expectedReadout' => 1189.2776,
511521
'expectedValue' => 1189.2776,
512522
'hasErrors' => false,
513523
'expectedErrors' => array(),
@@ -585,6 +595,7 @@ class WatermeterReaderVariantTest extends TestCase
585595
'regular3_with_full_image_processing' =>
586596
array(
587597
'lastValue' => 1189.2668,
598+
'expectedReadout' => 1189.3858,
588599
'expectedValue' => 1189.3858,
589600
'hasErrors' => false,
590601
'expectedErrors' => array(),
@@ -667,6 +678,7 @@ class WatermeterReaderVariantTest extends TestCase
667678
'regular4_with_ocr_passing' =>
668679
array(
669680
'lastValue' => 1189.9216,
681+
'expectedReadout' => 1189.9244,
670682
'expectedValue' => 1189.9244,
671683
'hasErrors' => false,
672684
'expectedErrors' => array(),
@@ -742,6 +754,7 @@ class WatermeterReaderVariantTest extends TestCase
742754
'regular5_with_ocr_failing_smaller' =>
743755
array(
744756
'lastValue' => 1189.9216,
757+
'expectedReadout' => 1189.9216,
745758
'expectedValue' => 1189.9216,
746759
'hasErrors' => true,
747760
'expectedErrors' => array(
@@ -823,6 +836,7 @@ class WatermeterReaderVariantTest extends TestCase
823836
'regular6_with_ocr_failing_larger' =>
824837
array(
825838
'lastValue' => 1189.9244,
839+
'expectedReadout' => 1189.9244,
826840
'expectedValue' => 1189.9244,
827841
'hasErrors' => true,
828842
'expectedErrors' => array(
@@ -904,6 +918,7 @@ class WatermeterReaderVariantTest extends TestCase
904918
'regular7_with_ocr_completely_failing' =>
905919
array(
906920
'lastValue' => 1189.9383,
921+
'expectedReadout' => 1189.9594,
907922
'expectedValue' => 1189.9594,
908923
'hasErrors' => true,
909924
'expectedErrors' => array(
@@ -981,6 +996,7 @@ class WatermeterReaderVariantTest extends TestCase
981996
'regular8_post_decimal_colored_digits' =>
982997
array(
983998
'lastValue' => 206.9227,
999+
'expectedReadout' => 206.9228,
9841000
'expectedValue' => 206.9228,
9851001
'hasErrors' => false,
9861002
'expectedErrors' => array(),
@@ -1074,6 +1090,7 @@ class WatermeterReaderVariantTest extends TestCase
10741090
'inverted9' =>
10751091
array(
10761092
'lastValue' => 364595.0,
1093+
'expectedReadout' => 364596.0,
10771094
'expectedValue' => 364596.0,
10781095
'hasErrors' => false,
10791096
'expectedErrors' => array(),
@@ -1144,6 +1161,7 @@ class WatermeterReaderVariantTest extends TestCase
11441161
),
11451162
'10_without_gauges_or_digits' => array(
11461163
'lastValue' => 1834,
1164+
'expectedReadout' => 1835,
11471165
'expectedValue' => 1835,
11481166
'hasErrors' => false,
11491167
'expectedErrors' => array(),
@@ -1198,18 +1216,74 @@ class WatermeterReaderVariantTest extends TestCase
11981216
'postDecimalDigits' => NULL,
11991217
),
12001218
),
1219+
'gh-issue-133' => array(
1220+
'lastValue' => 239,
1221+
'expectedReadout' => 240,
1222+
'expectedValue' => 240,
1223+
'hasErrors' => false,
1224+
'expectedErrors' => array(),
1225+
'config' => array(
1226+
'logging' => false,
1227+
'maxThreshold' => '1.0',
1228+
'sourceImage' => __DIR__ . '/data/variants/11-gh-issue-133.png',
1229+
'sourceImageRotate' => '0',
1230+
'sourceImageCropSizeX' => '0',
1231+
'sourceImageCropSizeY' => '0',
1232+
'sourceImageCropStartX' => '0',
1233+
'sourceImageCropStartY' => '0',
1234+
'sourceImageBrightness' => '0',
1235+
'sourceImageContrast' => '0',
1236+
'postprocessing' => false,
1237+
'digitalDigits' => array(
1238+
1 =>
1239+
array (
1240+
'x' => '280',
1241+
'y' => '188',
1242+
'width' => '20',
1243+
'height' => '45',
1244+
),
1245+
2 =>
1246+
array (
1247+
'x' => '300',
1248+
'y' => '188',
1249+
'width' => '20',
1250+
'height' => '45',
1251+
),
1252+
3 =>
1253+
array (
1254+
'x' => '330',
1255+
'y' => '188',
1256+
'width' => '20',
1257+
'height' => '45',
1258+
),
1259+
),
1260+
'analogGauges' => array(),
1261+
'offsetValue' => '',
1262+
'postDecimalDigits' => array(),
1263+
'sourceImageEqualize' => false,
1264+
'digitDecolorization' => false,
1265+
'digitalDigitsInversion' => true,
1266+
),
1267+
),
12011268
);
12021269

12031270
public function testVariants(): void
12041271
{
12051272
foreach ($this->variants as $variant_id => $variant) {
12061273
$reader = new Reader(false, $variant['config'], $variant['lastValue']);
1274+
$readout = $reader->getReadout();
12071275
$actualValue = $reader->getValue();
1208-
# $actualErrors = $reader->getErrors();
1276+
$actualErrors = $reader->getErrors();
12091277
$actualHasErrors = $reader->hasErrors();
1278+
$this->assertEqualsWithDelta($variant['expectedReadout'], $readout, 0.00001, 'Readout mismatch for variant ' . $variant_id);
12101279
$this->assertEqualsWithDelta($variant['expectedValue'], $actualValue, 0.00001, 'Value mismatch for variant ' . $variant_id);
12111280
$this->assertEquals($variant['hasErrors'], $actualHasErrors, 'Error flag mismatch for variant ' . $variant_id);
1212-
# $this->assertEquals($variant['expectedErrors'], $actualErrors, 'Errors mismatch for variant ' . $variant_id);
1281+
if (!$actualHasErrors) {
1282+
assertEmpty($actualErrors, 'Errors should be empty for variant ' . $variant_id);
1283+
}
1284+
# } else {
1285+
# $this->assertContains($variant['expectedErrors'], $actualErrors, 'Errors mismatch for variant ' . $variant_id);
1286+
# }
12131287
}
12141288
}
12151289
}
347 KB
Loading

0 commit comments

Comments
 (0)