# fedora spec file for php-beberlei-assert
#
# Copyright (c) 2020 Christopher Engelhard
# License: MIT
#
# Please preserve the changelog entries

# package and composer name
%global vendor      beberlei
%global project     assert

# PHP namespace and directory
%global ns_project  Assert
%global ns_dir      %{vendor}/%( echo "%{ns_project}" | sed  's|\\\\|\/|g' )

# Github
%global gh_vendor   beberlei
%global gh_project  assert
%global commit      5e721d7e937ca3ba2cdec1e1adf195f9e5188372
%global scommit     %(c=%{commit}; echo ${c:0:7})

# tests
%bcond_without tests

#-- PREAMBLE ------------------------------------------------------------------#
Name:           php-%{vendor}-%{project}
Version:        3.3.1
Release:        %autorelease
Summary:        Thin assertion library for input validation in business models

License:        BSD
URL:            https://github.com/%{gh_vendor}/%{gh_project}
Source0:        %{name}-%{version}-%{scommit}.tgz
Source1:        makesrc.sh

BuildArch:      noarch

# for the autoloader
Requires:       php-composer(fedora/autoloader)

# from composer.json
Requires:       php(language) >= 7.0
Requires:       php-simplexml
Requires:       php-mbstring
Requires:       php-ctype
Requires:       php-json
Recommends:     php-intl

# from phpcompatinfo
Requires:       php-date
Requires:       php-filter
Requires:       php-pcre
Requires:       php-reflection
Requires:       php-spl

# for autoloader check
BuildRequires:  php-composer(fedora/autoloader)
BuildRequires:  %{_bindir}/php
BuildRequires:  php(language) >= 7.0

%if %{with tests}
BuildRequires:  phpunit9
BuildRequires:  php-simplexml
BuildRequires:  php-mbstring
BuildRequires:  php-ctype
BuildRequires:  php-json
BuildRequires:  php-intl
BuildRequires:  php-date
BuildRequires:  php-filter
BuildRequires:  php-pcre
BuildRequires:  php-reflection
BuildRequires:  php-spl
BuildRequires:  php-composer(yoast/phpunit-polyfills)
%endif

# composer provides
Provides:       php-composer(%{vendor}/%{project}) = %{version}

%description
A simple php library which contains assertions and guard methods for input
validation (not filtering!) in business-model, libraries and application
low-level code.
The library can be used to implement pre-/post-conditions on input data.

The library autoloader is: %{_datadir}/php/%{ns_dir}/autoload.php


#-- PREP, BUILD & INSTALL -----------------------------------------------------#
%prep
%autosetup -p1 -n %{gh_project}-%{commit}

%build
: Nothing to build.

%install
: Create installation directory
mkdir -p   %{buildroot}%{_datadir}/php/%{ns_dir}
cp -pr lib/Assert/* %{buildroot}%{_datadir}/php/%{ns_dir}

: Generate an autoloader
cat <<'EOF' | tee %{buildroot}%{_datadir}/php/%{ns_dir}/autoload.php
<?php
require_once '%{_datadir}/php/Fedora/Autoloader/autoload.php';

// classes
\Fedora\Autoloader\Autoload::addPsr4('%{ns_project}', __DIR__);

// files & dependencies
\Fedora\Autoloader\Dependencies::required([
  __DIR__.'/functions.php',
]);
EOF

%check
: Check the autoloader
%{_bindir}/php -r "
    require_once '%{buildroot}%{_datadir}/php/%{ns_dir}/autoload.php';
    exit(
        class_exists('%{ns_project}\Assertion') &&
        function_exists('%{ns_project}\thatNullOr')
        ? 0 : 1
    );
"
%if %{with tests}
: Run tests
mkdir vendor
cat << 'EOF' | tee vendor/autoload.php
<?php
require_once '%{buildroot}%{_datadir}/php/%{ns_dir}/autoload.php';
\Fedora\Autoloader\Autoload::addPsr4('%{ns_project}\\Tests', dirname(__DIR__).'/tests/Assert/Tests');
\Fedora\Autoloader\Dependencies::required([
	'%{_datadir}/php/Yoast/PHPUnitPolyfills/autoload.php',
    dirname(__DIR__).'/tests/Assert/Tests/Fixtures/functions.php',
]);
EOF

phpunit9 --migrate-configuration
phpunit9 --verbose
%endif

#-- FILES ---------------------------------------------------------------------#
%files
%license LICENSE
%doc composer.json
%doc *md
%{_datadir}/php/%{vendor}


#-- CHANGELOG -----------------------------------------------------------------#
%changelog
%autochangelog