|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Overcommit::Hook::PreCommit::PhpCs do |
| 4 | + let(:config) { Overcommit::ConfigurationLoader.default_configuration } |
| 5 | + let(:context) { double('context') } |
| 6 | + subject { described_class.new(config, context) } |
| 7 | + |
| 8 | + before do |
| 9 | + subject.stub(:applicable_files).and_return(%w[sample.php]) |
| 10 | + end |
| 11 | + |
| 12 | + context 'when phpcs exits successfully' do |
| 13 | + before do |
| 14 | + sample_output = [ |
| 15 | + 'File,Line,Column,Type,Message,Source,Severity,Fixable', |
| 16 | + '' |
| 17 | + ].join("\n") |
| 18 | + |
| 19 | + result = double('result') |
| 20 | + result.stub(:success?).and_return(true) |
| 21 | + result.stub(:stdout).and_return(sample_output) |
| 22 | + result.stub(:status).and_return(0) |
| 23 | + subject.stub(:execute).and_return(result) |
| 24 | + end |
| 25 | + |
| 26 | + it { should pass } |
| 27 | + end |
| 28 | + |
| 29 | + context 'when phpcs exits unsuccessfully' do |
| 30 | + let(:result) { double('result') } |
| 31 | + |
| 32 | + before do |
| 33 | + result.stub(:success?).and_return(false) |
| 34 | + result.stub(:status).and_return(2) |
| 35 | + subject.stub(:execute).and_return(result) |
| 36 | + end |
| 37 | + |
| 38 | + context 'and it reports a warning' do |
| 39 | + before do |
| 40 | + # rubocop:disable Metrics/LineLength |
| 41 | + sample_output = [ |
| 42 | + 'File,Line,Column,Type,Message,Source,Severity,Fixable', |
| 43 | + '"/Users/craig/HelpScout/overcommit-testing/invalid.php",5,1,warning,"Possible parse error: FOREACH has no AS statement",Squiz.ControlStructures.ForEachLoopDeclaration.MissingAs,5,0' |
| 44 | + ].join("\n") |
| 45 | + # rubocop:enable Metrics/LineLength |
| 46 | + result.stub(:stdout).and_return(sample_output) |
| 47 | + end |
| 48 | + |
| 49 | + it { should warn } |
| 50 | + end |
| 51 | + |
| 52 | + context 'and it reports an error' do |
| 53 | + before do |
| 54 | + # rubocop:disable Metrics/LineLength |
| 55 | + sample_output = [ |
| 56 | + 'File,Line,Column,Type,Message,Source,Severity,Fixable', |
| 57 | + '"/Users/craig/HelpScout/overcommit-testing/invalid.php",5,1,error,"Inline control structures are not allowed",Generic.ControlStructures.InlineControlStructure.NotAllowed,5,1' |
| 58 | + ].join("\n") |
| 59 | + # rubocop:enable Metrics/LineLength |
| 60 | + result.stub(:stdout).and_return(sample_output) |
| 61 | + end |
| 62 | + |
| 63 | + it { should fail_hook } |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments