|
| 1 | +#!/usr/bin/env bash |
| 2 | +# @tags result-cache |
| 3 | + |
| 4 | +function set_up() { |
| 5 | + e2e::set_up |
| 6 | +} |
| 7 | + |
| 8 | +function tear_down() { |
| 9 | + e2e::tear_down |
| 10 | +} |
| 11 | + |
| 12 | +function assert_same_as_cold_run() { |
| 13 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$? |
| 14 | + echo "$OUTPUT" |
| 15 | + rm -rf tmp/resultCache.php |
| 16 | + COLD_OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && COLD_CODE=0 || COLD_CODE=$? |
| 17 | + echo "$COLD_OUTPUT" |
| 18 | + assert_same "$COLD_CODE" "$CODE" |
| 19 | + assert_same "$COLD_OUTPUT" "$OUTPUT" |
| 20 | +} |
| 21 | + |
| 22 | +function test_inline_ignored_error_waits_for_the_symbol() { |
| 23 | + # src/User.php has no error left after its ignore, but the ignored error waits for the class. |
| 24 | + printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nfunction doUser(): object\n{\n\treturn new Thing(); // @phpstan-ignore class.notFound\n}\n' > src/User.php |
| 25 | + ../../bin/phpstan analyse > /dev/null 2>&1 || true |
| 26 | + # Declaring it re-analyses src/User.php too, where the ignore now matches nothing. |
| 27 | + printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nclass Thing\n{\n\n}\n' > src/Thing.php |
| 28 | + INFO=$(../../bin/phpstan result-cache-info --json) |
| 29 | + echo "$INFO" |
| 30 | + assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')" |
| 31 | + assert_equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')" |
| 32 | + assert_same_as_cold_run |
| 33 | + assert_contains 'User.php:7:No error with identifier class.notFound is reported on line 7.' "$OUTPUT" |
| 34 | +} |
| 35 | + |
| 36 | +function test_inline_ignored_error_waits_for_a_symbol_in_an_existing_file() { |
| 37 | + # src/User.php has no error left after its ignore, but the ignored error waits for the class. |
| 38 | + printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nfunction doUser(): object\n{\n\treturn new Thing(); // @phpstan-ignore class.notFound\n}\n' > src/User.php |
| 39 | + ../../bin/phpstan analyse > /dev/null 2>&1 || true |
| 40 | + # The same when the class is declared in a file that already exists. |
| 41 | + printf '\nclass Thing\n{\n\n}\n' >> src/Other.php |
| 42 | + INFO=$(../../bin/phpstan result-cache-info --json) |
| 43 | + echo "$INFO" |
| 44 | + assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')" |
| 45 | + assert_equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')" |
| 46 | + assert_same_as_cold_run |
| 47 | + assert_contains 'User.php:7:No error with identifier class.notFound is reported on line 7.' "$OUTPUT" |
| 48 | +} |
| 49 | + |
| 50 | +function test_deleted_file_removes_a_symbol() { |
| 51 | + # src/Caller.php has an error because App\doThing() exists, but it does not depend on its file: |
| 52 | + # the function is only named in a string. |
| 53 | + printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nfunction doThing(): void\n{\n}\n' > src/thing.php |
| 54 | + printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nfunction doCaller(): ?\\ReflectionFunction\n{\n\ttry {\n\t\treturn new \\ReflectionFunction('"'"'ResultCacheE2ESymbolSweep\\\\doThing'"'"');\n\t} catch (\\ReflectionException $e) {\n\t\treturn null;\n\t}\n}\n' > src/Caller.php |
| 55 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) || true |
| 56 | + echo "$OUTPUT" |
| 57 | + assert_contains 'Caller.php:9:Dead catch - ReflectionException is never thrown in the try block.' "$OUTPUT" |
| 58 | + # Deleting the file re-analyses src/Caller.php, the file with an error. |
| 59 | + rm src/thing.php |
| 60 | + INFO=$(../../bin/phpstan result-cache-info --json) |
| 61 | + echo "$INFO" |
| 62 | + assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')" |
| 63 | + assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')" |
| 64 | + assert_same_as_cold_run |
| 65 | + assert_not_contains 'Dead catch' "$OUTPUT" |
| 66 | +} |
0 commit comments