Skip to content

Commit

Permalink
test: add test cases for php from @terales
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 29, 2021
1 parent 0924e3b commit 6698f0a
Show file tree
Hide file tree
Showing 309 changed files with 1,670 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/fixtures/php/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"i18n-ally.enabledFrameworks": ["laravel"],
"i18n-ally.enabledParsers": ["json"],
"i18n-ally.localesPaths": ".",
"i18n-ally.pathMatcher": "messages.{locale}.json",
"i18n-ally.keystyle": "flat",
}
64 changes: 64 additions & 0 deletions test/fixtures/php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Extraction tests runs automatically.

Prepare tests in this format:
```
source class name/
├─ expression_from_extraction (concatenation, interpolation, functions, methods)/
│ ├─ test_case_name/
│ │ ├─ input/
│ │ │ ├─ source files
│ │ │ ├─ sink file
│ │ ├─ output/
│ │ │ ├─ changed input files (source, sinks, etc)
```

# Test structure
```
<?php // setup test data so test compiles
$name = 'John'; //
$msg = sprintf('Welcome, %s!', $name); // prepare the last expression
// that should trigger the extraction intention.
// ONLY the last intention would be executed
```

# Style guide

### Use underscores to separate words in folder and file names
```
// bad
function as placeholder name
function-as-placeholder-name
function_as-placeholder-name
// GOOD
function_as_placeholder_name
```

### Use lower case letters in files and folders names
```
// bad
Skip_html
skip_HTML
// GOOD
skip_html
```

### Omit articles (`a`, `the`)
```
// bad
skip_the_html
// GOOD
skip_html
```

### Omit strings word as we always deal with strings during code inspections/extraction
```
// bad
strings_interpolation
// GOOD
interpolation
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$key = 'key';
$arrayExample = ['key' => 'value'];

$msg = "Both '" . $arrayExample['key'] . "' and '" . $arrayExample[$key] . "' are valid";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"both_and_are_valid": "Both '{arrayExample}' and '{arrayExample_2}' are valid"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$key = 'key';
$arrayExample = ['key' => 'value'];

$msg = $translator->trans('both_and_are_valid', ['arrayExample' => $arrayExample['key'], 'arrayExample_2' => $arrayExample[$key]]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$very = 'Very';
$name = 'John';

$msg = $very. " welcome, ". $name;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome": "{very} welcome, {name}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$very = 'Very';
$name = 'John';

$msg = $translator->trans('welcome', ['very' => $very, 'name' => $name]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"keyNamingPattern": "DEFAULT_PATTERN",
"name": "messages",
"sink": {
"placeHolderName": "{%name%}",
"sinkType": "xliffSink",
"sinkFileName": "messages.xlf"
},
"source": {
"functionName": "$translator->trans",
"placeholderArgumentsTemplate": "'%key%', %map%",
"simpleArgumentsTemplate": "'%key%'",
"sourceType": "plainPhpSource",
"sourceFileName": "source.php"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$very = 'Very';
$name = 'John';

$msg = $very. " welcome, ". $name;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="welcome">
<source>welcome</source>
<target>{very} welcome, {name}</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$very = 'Very';
$name = 'John';

$msg = $translator->trans('welcome', ['very' => $very, 'name' => $name]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$msg = 'Welcome, ' . __FILE__ . '!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome": "Welcome, {file}!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$msg = $translator->trans('welcome', ['file' => __FILE__]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

throw new \Exception('Welcome, ' . $name . '!');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome": "Welcome, {name}!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

throw new \Exception($translator->trans('welcome', ['name' => $name]));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$name = 'John';
$generic = 'customer';

$msg = 'Welcome, ' . (!empty($name) ? $name : $generic) . '!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome": "Welcome, {expr}!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$name = 'John';
$generic = 'customer';

$msg = $translator->trans('welcome', ['expr' => (!empty($name) ? $name : $generic)]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

$msg = "Welcome, " . trim($name) . "!";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome": "Welcome, {trim}!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

$msg = $translator->trans('welcome', ['trim' => trim($name)]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

$msg = "Welcome, " . trim($name) . " and " . trim(strip_tags($name)) . "!";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome_and": "Welcome, {trim} and {trim_2}!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

$msg = $translator->trans('welcome_and', ['trim' => trim($name), 'trim_2' => trim(strip_tags($name))]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

$msg = "Welcome, " . trim($name) . " and " . trim($name) . "!";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome_and": "Welcome, {trim} and {trim}!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

$msg = $translator->trans('welcome_and', ['trim' => trim($name)]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$name = 'John';

$msg = purpose('Welcome ' . $name);

$msg = notInWhileList('Welcome ' . $name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome": "Welcome {name}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$name = 'John';

$msg = purpose($translator->trans('welcome', ['name' => $name]));

$msg = notInWhileList('Welcome ' . $name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$arrayExample = ['key' => 'value'];

$msg = "Both '" . $arrayExample['key'] . "' and '" . $arrayExample['key'] . "' are valid";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"both_and_are_valid": "Both '{arrayExample}' and '{arrayExample}' are valid"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$arrayExample = ['key' => 'value'];

$msg = $translator->trans('both_and_are_valid', ['arrayExample' => $arrayExample['key']]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

echo 'Welcome, ' . $name . '!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome": "Welcome, {name}!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

echo $translator->trans('welcome', ['name' => $name]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

$arrayExample = ['key' => 'Welcome, ' . $name . '!'];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome": "Welcome, {name}!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$name = 'John';

$arrayExample = ['key' => $translator->trans('welcome', ['name' => $name])];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$key = 'key';
$key2 = 'key2';
$arrayExample = ['key' => 'value'];

$msg = "User-facing string: '" . $arrayExample[$key] . "' and '" . $arrayExample[$key2] . "'";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"userfacing_string_and": "User-facing string: '{arrayExample}' and '{arrayExample_2}'"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$key = 'key';
$key2 = 'key2';
$arrayExample = ['key' => 'value'];

$msg = $translator->trans('userfacing_string_and', ['arrayExample' => $arrayExample[$key], 'arrayExample_2' => $arrayExample[$key2]]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$name = 'John';
$price = 10.50;

$msg = "Price for " . $name . ": " . $price;
Loading

0 comments on commit 6698f0a

Please sign in to comment.