|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +function set_up() { |
| 4 | + e2e::set_up |
| 5 | +} |
| 6 | + |
| 7 | +function tear_down() { |
| 8 | + e2e::tear_down |
| 9 | +} |
| 10 | + |
| 11 | +# A rule on CollectedDataNode reports that a call has no effect only when the declaration of the called |
| 12 | +# function is analysed too. The collector tracks the file of the declaration, see |
| 13 | +# DependencyTracker::trackAnalysedFileDependency(). |
| 14 | +function test_call_to_pure_function_declared_in_directory_not_analysed() { |
| 15 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls 2>&1) && CODE=0 || CODE=$? |
| 16 | + echo "$OUTPUT" |
| 17 | + assert_same "0" "$CODE" |
| 18 | +} |
| 19 | + |
| 20 | +function test_call_to_pure_function_with_its_declaration() { |
| 21 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls srcc 2>&1) && CODE=0 || CODE=$? |
| 22 | + echo "$OUTPUT" |
| 23 | + assert_same "1" "$CODE" |
| 24 | + assert_not_contains 'function.resultUnused' "$OUTPUT" |
| 25 | + assert_contains '#never in CallsPureFunction# in path' "$OUTPUT" |
| 26 | + # the called function calls a function from src/Pure, which is not analysed |
| 27 | + assert_not_contains 'CallsTransitivelyPureFunction' "$OUTPUT" |
| 28 | +} |
| 29 | + |
| 30 | +# The called function is pure only when the function it calls is pure, so the call is reported |
| 31 | +# only when both declarations are analysed. |
| 32 | +function test_call_to_transitively_pure_function() { |
| 33 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls src/Pure 2>&1) && CODE=0 || CODE=$? |
| 34 | + echo "$OUTPUT" |
| 35 | + assert_same "0" "$CODE" |
| 36 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls src/Pure srcc 2>&1) && CODE=0 || CODE=$? |
| 37 | + echo "$OUTPUT" |
| 38 | + assert_same "1" "$CODE" |
| 39 | + assert_not_contains 'function.resultUnused' "$OUTPUT" |
| 40 | + assert_contains '#never in CallsPureFunction# in path' "$OUTPUT" |
| 41 | + assert_contains '#never in CallsTransitivelyPureFunction# in path' "$OUTPUT" |
| 42 | +} |
| 43 | + |
| 44 | +function test_call_to_pure_function_in_only_files() { |
| 45 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls/CallsPureFunction.php 2>&1) && CODE=0 || CODE=$? |
| 46 | + echo "$OUTPUT" |
| 47 | + assert_same "0" "$CODE" |
| 48 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls/CallsPureFunction.php srcc/pure-function.php 2>&1) && CODE=0 || CODE=$? |
| 49 | + echo "$OUTPUT" |
| 50 | + assert_same "1" "$CODE" |
| 51 | + assert_not_contains 'function.resultUnused' "$OUTPUT" |
| 52 | + assert_contains '#never in CallsPureFunction# in path' "$OUTPUT" |
| 53 | +} |
| 54 | + |
| 55 | +# The dependencies of the files restored from the result cache come from the cache. |
| 56 | +function test_result_cache_of_call_to_pure_function() { |
| 57 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls src/Pure 2>&1) && CODE=0 || CODE=$? |
| 58 | + echo "$OUTPUT" |
| 59 | + assert_same "0" "$CODE" |
| 60 | + echo '// changed' >> src/Pure/transitively-pure-function.php |
| 61 | + OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw src/Calls src/Pure 2>&1) && CODE=0 || CODE=$? |
| 62 | + echo "$OUTPUT" |
| 63 | + assert_same "0" "$CODE" |
| 64 | + assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT" |
| 65 | +} |
| 66 | + |
| 67 | +# Whether a call with a named argument is reported depends on the subclasses renaming the parameter, |
| 68 | +# which can be anywhere, see DependencyTracker::trackAnalysedProjectDependency(). |
| 69 | +function test_named_argument_depends_on_the_whole_project() { |
| 70 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Named srcc 2>&1) && CODE=0 || CODE=$? |
| 71 | + echo "$OUTPUT" |
| 72 | + assert_same "1" "$CODE" |
| 73 | + assert_not_contains 'CallsWithNamedArgument' "$OUTPUT" |
| 74 | + OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$? |
| 75 | + echo "$OUTPUT" |
| 76 | + assert_same "1" "$CODE" |
| 77 | + assert_not_contains 'argument.parameterRenamedInSubtype' "$OUTPUT" |
| 78 | + assert_contains '#never in CallsWithNamedArgument# in path' "$OUTPUT" |
| 79 | + assert_contains '#never in CallsPureFunction# in path' "$OUTPUT" |
| 80 | + assert_contains '#never in CallsTransitivelyPureFunction# in path' "$OUTPUT" |
| 81 | +} |
0 commit comments