require_relative "spec_helper"

describe "A PHP application intended for Composer 2" do
	context "with a composer.lock generated by version 2.2 of Composer" do
		it "builds using Composer 2.2" do
			new_app_with_stack_and_platrepo('test/fixtures/composer/basic_lock_v2lts').deploy do |app|
				expect(app.output).to match(/- composer \(2\.2\./)
			end
		end
	end
	context "with a composer.lock generated by version 2.3 of Composer" do
		before(:all) do
			@app = new_app_with_stack_and_platrepo('test/fixtures/composer/basic_lock_v2')
			@app.deploy
		end
		
		after(:all) do
			@app.teardown!
		end
		
		it "builds using Composer 2.3 or later" do
			expect(@app.output).to match(/- composer \(2\.([3-9]|\d{2,})\./)
		end
		
		context "with a malformed COMPOSER_AUTH env var" do
			it "still boots" do
				# config test is enough, it's past any uses of composer on startup
				cmds = ['heroku-php-apache2', 'heroku-php-nginx'].map { |script| "#{script} -t" }
				retry_until retry: 3, sleep: 5 do
					expect_exit(code: 0) { @app.run("( set -e; #{cmds.join('; ')}; )", :return_obj => true, :heroku => {:env => "COMPOSER_AUTH=malformed"}) }
				end
			end
		end
	end
	context "with a composer.lock generated by a future version 2 of Composer" do
		it "builds using Composer 2.3 or later" do
			new_app_with_stack_and_platrepo('test/fixtures/composer/basic_lock_v2.999').deploy do |app|
				expect(app.output).to match(/- composer \(2\.([3-9]|\d{2,})\./)
			end
		end
	end
	context "with only an index.php" do
		it "builds using Composer 2.2" do
			new_app_with_stack_and_platrepo('test/fixtures/default').deploy do |app|
				expect(app.output).to match(/- composer \(2\.2\./)
			end
		end
	end
	
	context "with a 'compile' script" do
		before(:all) do
			@app = new_app_with_stack_and_platrepo_and_bin_report_dumper(
				'test/fixtures/composer/compile_script',
				config: {
					"COMPILE_SCRIPT_ECHO" => "hi from compile script",
					"COMPILE_SCRIPT_SLEEP" => 5
				}
			)
			@app.deploy
		end
		
		after(:all) do
			@app.teardown!
		end
		
		it "runs 'composer compile' at the end of the build" do
			expect(@app.output)
				 .to include("Running 'composer compile'...")
				.and include("hi from compile script")
		end
		
		it "captures the duration of the script run as part of the information about the build" do
			expect(@app.bin_report_dump).to include(
				"scripts.compile.duration" => a_kind_of(Float).and(a_value > 5),
			)
		end
	end
	
	context "with a faulty Composer plugin that prints to stderr during activation" do
		before(:all) do
			@app = new_app_with_stack_and_platrepo_and_bin_report_dumper('test/fixtures/composer/faulty_plugin')
			@app.deploy
		end
		
		after(:all) do
			@app.teardown!
		end
		
		it "builds successfully" do
			expect(@app.output).to include("Hello, I am FaultyPlugin, writing to stdout instead of stderr in PluginInterface::activate()")
		end
		
		it "captures the number of dependencies that were installed as part of the information about the build" do
			expect(@app.bin_report_dump).to include(
				"dependencies.packages.installed_count" => 1,
			)
		end
		
		it "boots and serves traffic" do
			expect(successful_body(@app))
		end
	end
end
