# Inspec test for recipe irrigate-php::php71

# The Inspec reference, with examples and extensive documentation, can be
# found at http://inspec.io/docs/reference/resources/

control 'php71' do
  attribute_file = '/tmp/kitchen/node_attributes.json'
  if file(attribute_file).exist?
    node = json(attribute_file).params
    case node['automatic']['platform_family']
    when 'rhel'
      if node['automatic']['platform_version'].to_s.start_with?('7')
        describe command('yum repolist') do
          its('stdout') { should include 'remi-php71' }
          its('stdout') { should include 'remi-php71-debuginfo' }
          its('stdout') { should include 'remi71-safe' }
        end

        packages = %w(
          autoconf
          bzip2-devel
          epel-release
          freetype-devel
          gcc-c++
          glibc
          gmp-devel
          httpd-devel
          ImageMagick
          ImageMagick-devel
          libcurl-devel
          libdb4-devel
          libevent
          libjpeg-turbo
          libjpeg-turbo-devel
          libmcrypt
          libmcrypt-devel
          libmemcached
          libpng-devel
          libtiff
          libtiff-devel
          libxml2-devel
          make
          memcached
          mhash
          mhash-devel
          mod_ssl
          openssl-devel
          php71
          php71-php
          php71-php-bcmath
          php71-php-cli
          php71-php-common
          php71-php-devel
          php71-php-gd
          php71-php-gmp
          php71-php-ldap
          php71-php-mbstring
          php71-php-mcrypt
          php71-php-mysqlnd
          php71-php-opcache
          php71-php-pear
          php71-php-pecl-imagick
          php71-php-pecl-imagick-devel
          php71-php-pecl-memcache
          php71-php-pecl-memcached
          php71-php-pecl-redis
          php71-php-pecl-ssh2
          php71-php-pecl-uploadprogress
          php71-php-pecl-zip
          php71-php-pgsql
          php71-php-soap
          php71-php-xml
          unixODBC
          unixODBC-devel
        )

        packages.each do |name|
          describe package name do
            it { should be_installed }
          end
        end

        require 'erb'
        node['php'] = node['default']['php']
        template1 = File.read('templates/default/remi-php71.sh.erb')
        rendered_content1 = ERB.new(template1).result(binding)
        describe file '/etc/profile.d/remi-php71.sh' do
          it { should exist }
          its('owner') { should eq 'root' }
          its('group') { should eq 'root' }
          its('mode') { should cmp '0644' }
          its('content') { should eq rendered_content1 }
        end

        template2 = File.read('templates/default/php.ini.erb')
        rendered_content2 = ERB.new(template2).result(binding)
        describe file '/etc/opt/remi/php71/php.ini' do
          it { should exist }
          its('owner') { should eq 'root' }
          its('group') { should eq 'root' }
          its('mode') { should cmp '0644' }
          its('content') { should eq rendered_content2 }
        end

        describe file '/usr/local/bin/php' do
          it { should be_symlink }
          its('link_path') { should eq '/opt/remi/php71/root/bin/php' }
        end

      end
    end
  else
    puts "File #{attribute_file} does not exist, skiping attribute-depending checks"
  end
end
