Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Test #75

Open
wants to merge 39 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c21cc1f
Setup PHPUnit for the project
thrijith Jan 27, 2020
1ad1391
Add GitHub Action to run PHPUnit tests
thrijith Jan 27, 2020
ec22c3a
Merge pull request #63 from rtCamp/feature/setup-phpunit-tests
thrijith Jan 29, 2020
175c768
Add phpcs.xml for PHPCS ruleset
kiranpotphode Jan 29, 2020
5fc7347
Add test case for classes/class-admin.php
thrijith Jan 29, 2020
ced9320
Update PHP compatibility
kiranpotphode Jan 30, 2020
293dbf8
Merge pull request #67 from rtCamp/feature/GH-66
thrijith Jan 30, 2020
9ba6a93
Merge branch 'dev-unit-test' into add/admin-class-tests
thrijith Jan 30, 2020
53700ab
Add test case for classes/class-shortcode.php
thrijith Jan 30, 2020
d1c24a6
Ignore all files in tests directory
thrijith Jan 30, 2020
91f2f72
Set demo data for ad-manager settings
thrijith Jan 30, 2020
cc34293
Move option update to setup
thrijith Jan 30, 2020
30018ce
Update assertion use assertarrayHasKey instead of assertTrue
thrijith Jan 30, 2020
b423dd6
Add tests for class-admin.php and class-shortcode.php (#70)
thrijith Jan 30, 2020
2cb6ac7
Add test cases for AMP Admanager class
vaishaliagola27 Jan 31, 2020
4bf86b9
Add test cases for AMP Admanager class
vaishaliagola27 Jan 31, 2020
7175c6f
Resolve conflicts while merging test cases
vaishaliagola27 Jan 31, 2020
46e8af5
Change expected output for network id set
vaishaliagola27 Jan 31, 2020
1302234
Resolve phpcs errors and add amp plugin in require-dev of composer
vaishaliagola27 Jan 31, 2020
e06524b
Feedback changes in PR
vaishaliagola27 Feb 7, 2020
4cefe75
Feedback changes in PR
vaishaliagola27 Feb 7, 2020
0c2aec2
Feedback changes in PR for phpcs
vaishaliagola27 Feb 7, 2020
4215d94
Merge pull request #71 from rtCamp/dev-unit-test-ampadmanager
vaishaliagola27 Feb 7, 2020
c503aa7
Add tests folder to skip_folders list
vaishaliagola27 Feb 21, 2020
bd2222c
Update readme with Contributing guideline
vaishaliagola27 Feb 21, 2020
3327138
Update readme and remove Contributing guideline
vaishaliagola27 Feb 21, 2020
49627a6
Merge pull request #74 from rtCamp/update-gh-action
vaishaliagola27 Feb 21, 2020
4cb16d0
Merge branch 'develop' into dev-unit-test
vaishaliagola27 Feb 21, 2020
01d5b41
Resolve conflicts while merging develop
vaishaliagola27 Feb 21, 2020
7cd155b
Add docblock description for test functions
deepaklalwani97 Mar 26, 2020
8bf4a49
Merge pull request #76 from rtCamp/update-test-docs
deepaklalwani97 Mar 26, 2020
4aaf20e
Remove duplicate assertions
deepaklalwani97 Apr 1, 2020
840f63f
Remove wordpress rule from phpcs.xml
deepaklalwani97 Apr 1, 2020
5d4c1a7
Remove duplicate assertion
deepaklalwani97 Apr 2, 2020
0b26612
Fix issue with custom targeting in get_ads
deepaklalwani97 Jul 15, 2020
efd043e
Fix test cases and cover 100 percent code coverage
deepaklalwani97 Jul 15, 2020
9adbe05
Fix typo
deepaklalwani97 Jul 15, 2020
7a4b5d5
Fix phpcs
deepaklalwani97 Jul 15, 2020
92f62b6
Merge pull request #78 from rtCamp/fix/custom-targetting
deepaklalwani97 Jul 17, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
- name: Run PHPCS inspection
uses: docker://rtcamp/action-phpcs-code-review:v2.0.0
env:
SKIP_FOLDERS: "tests,.github"
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
VAULT_TOKEN: ${{ secrets.VAULT_TOKEN }}
with:
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/phpunit_on_pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
on: pull_request
name: PHPUnit
jobs:
runPHPCSInspection:
name: Run PHPUnit test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Run PHPUnit test
uses: docker://rtcamp/action-run-phpunit:v1.0.0
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
*.swp
vendor
node_modules
composer.lock
.idea
.DS_Store
.vscode
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ Once you've decided to commit the time to seeing your pull request through, plea

1. You need at least one approval and once your pull request has passed code review and tests, it will be merged into `develop` and be in the pipeline for the next release.

## Unit testing

- Setup local unit test environment by running the below script from terminal with appropriate values.
- `./bin/install-wp-tests.sh <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]`

- E.g `./bin/install-wp-tests.sh amp_admanager_test root root localhost`
- Run `phpunit` in terminal from cloned project directory to run all test cases.
- Run `phpunit ./tests/classes/test-class.php` with file path to run specific tests.

## Change Log

### v1.0.0 (27-09-2019)
Expand Down
155 changes: 155 additions & 0 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env bash

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
exit 1
fi

DB_NAME=$1
DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}
WP_VERSION=${5-latest}
SKIP_DB_CREATE=${6-false}

TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}

download() {
if [ `which curl` ]; then
curl -s "$1" > "$2";
elif [ `which wget` ]; then
wget -nv -O "$2" "$1"
fi
}

if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
WP_BRANCH=${WP_VERSION%\-*}
WP_TESTS_TAG="branches/$WP_BRANCH"

elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
WP_TESTS_TAG="branches/$WP_VERSION"
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
WP_TESTS_TAG="tags/${WP_VERSION%??}"
else
WP_TESTS_TAG="tags/$WP_VERSION"
fi
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
WP_TESTS_TAG="trunk"
else
# http serves a single offer, whereas https serves multiple. we only want one
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
if [[ -z "$LATEST_VERSION" ]]; then
echo "Latest WordPress version could not be found"
exit 1
fi
WP_TESTS_TAG="tags/$LATEST_VERSION"
fi
set -ex

install_wp() {

if [ -d $WP_CORE_DIR ]; then
return;
fi

mkdir -p $WP_CORE_DIR

if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p $TMPDIR/wordpress-nightly
download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip
unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/
mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR
else
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then
# https serves multiple offers, whereas http serves single.
download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
LATEST_VERSION=${WP_VERSION%??}
else
# otherwise, scan the releases and get the most up to date minor version of the major release
local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'`
LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1)
fi
if [[ -z "$LATEST_VERSION" ]]; then
local ARCHIVE_NAME="wordpress-$WP_VERSION"
else
local ARCHIVE_NAME="wordpress-$LATEST_VERSION"
fi
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
fi
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
fi

download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}

install_test_suite() {
# portable in-place argument for both GNU sed and Mac OSX sed
if [[ $(uname -s) == 'Darwin' ]]; then
local ioption='-i.bak'
else
local ioption='-i'
fi

# set up testing suite if it doesn't yet exist
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
fi

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
# remove all forward slashes in the end
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
fi

}

install_db() {

if [ ${SKIP_DB_CREATE} = "true" ]; then
return 0
fi

# parse DB_HOST for port or socket references
local PARTS=(${DB_HOST//\:/ })
local DB_HOSTNAME=${PARTS[0]};
local DB_SOCK_OR_PORT=${PARTS[1]};
local EXTRA=""

if ! [ -z $DB_HOSTNAME ] ; then
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
EXTRA=" --socket=$DB_SOCK_OR_PORT"
elif ! [ -z $DB_HOSTNAME ] ; then
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
fi
fi

# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
}

install_wp
install_test_suite
install_db
12 changes: 8 additions & 4 deletions classes/class-amp-admanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ public static function get_dfp_ad_targeting_data( $attr ) {

if ( is_author() ) {

// todo: Check if username is available in queried object.
/**
* Ignoring code coverage for below lines because username member is not available in queried object.
*/
if ( ! empty( $queried->data->username ) ) {
$dfp_ad_data['authorPage'] = $queried->data->username;
$dfp_ad_data['authorPage'] = $queried->data->username; // @codeCoverageIgnore
} elseif ( ! empty( $queried->username ) ) {
$dfp_ad_data['authorPage'] = $queried->username;
$dfp_ad_data['authorPage'] = $queried->username; // @codeCoverageIgnore
}
}

Expand Down Expand Up @@ -288,9 +292,9 @@ private static function filter_breakpoints( $sizes ) {
if ( 728 <= (int) $width ) {
$breakpoints = self::set_max_height_and_width( 'desktop', $breakpoints, $width, $height );
} elseif ( 300 <= (int) $width && 600 >= (int) $width ) {
$breakpoints = self::set_max_height_and_width( 'tablet', $breakpoints, $width, $height );
$breakpoints = self::set_max_height_and_width( 'tablet', $breakpoints, $width, $height );
if ( 350 >= (int) $width ) {
$breakpoints = self::set_max_height_and_width( 'mobile', $breakpoints, $width, $height );
$breakpoints = self::set_max_height_and_width( 'mobile', $breakpoints, $width, $height );
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "amp-admanager",
"description": "Amp Admanager Plugin",
"type": "wordpress-plugin",
"license": "proprietary",
"authors": [
{
"name": "rtCamp",
"homepage": "https://rtcamp.com"
}
],
"repositories":[
{
"type":"composer",
"url":"https://wpackagist.org"
}
],
"require": {
"composer/installers": "^1.6"
},
"require-dev": {
"wpackagist-plugin/amp":"1.4.2"
},
"extra": {
"installer-paths": {
"../amp/": [
"wpackagist-plugin/amp"
]
}
}
}
Loading