commit 007afeb2ea8178d0479827c37fb199984d207c89 Author: Leo Germani Date: Fri Oct 20 12:30:04 2017 -0200 primeira versã odo repo com testes e build diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..9fb7ecd4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build-config.sh +src/style.css +src/style.css.map +src/scss/.sass-cache \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 000000000..1301f2d4c --- /dev/null +++ b/README.md @@ -0,0 +1,61 @@ +# Repositorio de testes + +Repositorio pra testar uma estrutura possível para o novo repositorio do WP + +## Descrição do que a gente quer + +A gente quer um repositorio onde teremos o codigo fonte do plugin, além de outras ferramentas necessárias para o desenvolvimento, como uma suíte de testes e componentes necessários pra desenvolvimento dos componentes JS + +Nesse repositorio, quando tivermos uma versão para publicar, rodaremos um script de build e ele gerará uma outra pasta, com o código do plugin todo pronto, sass e js compilado, etc. + +## Descrição desse esqueleto + +Nesse repositorio temos duas pastas: + +* src/ - onde fica o código do plugins +* tests/ - onde ficam as ferramentas de testes + +Na pasta `src` temos os arquivos normais de um plugin de WP. Essa será a pasta base do plugin e apenas o que estiver aí dentro é o que vai pra distribuição final do plugin. Já criei uns métodos quaisquer lá pra poder testar uma classe fictícia `TainacanCollections`. + +Aí dentro podem haver arquivos sass e js que ainda serão compilados no build. + +Na pasta `tests` temos a suíte de testes e as ferramentas para configurá-la. Já tem uns primeiros testes lá de exemplo que testa a classe `TainacanCollections`. + +Na raíz temos alguns scripts importantes: + +* build.sh - é o que faz o build (tá bem tosco ainda) +* compile-sass.sh - compila os arquivos scss do plugin +* run-tests.sh - monta o ambiente de testes e roda os testes +* outros arquivos do phpunit que podemos testar pra ver se precisam ficar assim na raíz mesmo. + + +## Montando ambiente e rodando testes + +Dá pra perceber que, como esse repositorio tem outras pastas além do plugin, não dá pra gente fazer o clone dela dentro da pasta de plugin do WP e sair usando. + +Temos que pensar como fazer isso, talvez um esquema que fique rodando o build e jogando o plugin pra dentro da nossa instalação de testes do WP. É assim que o `build.sh` ta fazendo hoje. + +Pra montar o ambiente de testes, foram seguidas essas instruções: https://make.wordpress.org/cli/handbook/plugin-unit-tests/ + +Basicamente você vai precisar instalar o `phpunit` e aí rodar o comando `tests/bin/install-wp-tests.sh` **passando os parâmetros certos**. Por exemplo: + +``` +tests/bin/install-wp-tests.sh wordpress_test root '' localhost latest +``` +Os parâmetros são: + +* nome do DB +* usuario do MySQL +* senha do MySQL +* host do MySQL +* versão do WP + +Esse comando só precisa rodar uma vez. pra instalar o ambiente. + +Depois disso, pra rodar os testes, é só rodar o comando `phpunit`. + +**Extras** + +Vale a pena ver essa série de posts: https://pippinsplugins.com/unit-tests-wordpress-plugins-introduction/ + +E pra usuários Windows, tem umas instruções lá de como fazer rodar: https://make.wordpress.org/cli/handbook/plugin-unit-tests/#integrating-wp-unit-testing-in-windows diff --git a/build-config-sample.sh b/build-config-sample.sh new file mode 100644 index 000000000..f32ea2442 --- /dev/null +++ b/build-config-sample.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +destination=~/devel/wordpress/wp-content/plugins/Test_Tainacan \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100644 index 000000000..afac59dee --- /dev/null +++ b/build.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +#source build-config.sh +destination=~/devel/wordpress/wp-content/plugins/test-tainacan + +sh compile-sass.sh + +rm -r $destination +mkdir $destination +cp -R src/* $destination/ +rm -rf $destination/scss diff --git a/compile-sass.sh b/compile-sass.sh new file mode 100644 index 000000000..963d9d66a --- /dev/null +++ b/compile-sass.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# Executa o comando 'sass' para verificar se existe (veja http://stackoverflow.com/a/677212/329911) +command -v sass >/dev/null 2>&1 || { + echo >&2 "SASS parece não está disponivel."; + exit 1; +} + +# Define o caminho. +echo "Compilando Sass..." +cd src/scss + +sass -E 'UTF-8' style.scss:../style.css +echo "Sass Compilado" + +echo "Compilação do Sass Concluído!" +exit 0 diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 000000000..2c9e09116 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,17 @@ + + + Generally-applicable sniffs for WordPress plugins + + + + + + + . + + + + + */node_modules/* + */vendor/* + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 000000000..44f0fdb69 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,14 @@ + + + + ./tests/ + + + diff --git a/src/scss/style.scss b/src/scss/style.scss new file mode 100644 index 000000000..3bf6ff116 --- /dev/null +++ b/src/scss/style.scss @@ -0,0 +1,4 @@ + +html { + margin-top: 0 !important; /* resolução do erro que estava dando no Firefox com espaço no topo*/ +} diff --git a/src/test-tainacan.php b/src/test-tainacan.php new file mode 100644 index 000000000..c24f1b787 --- /dev/null +++ b/src/test-tainacan.php @@ -0,0 +1,70 @@ + 'Collections', + 'singular_name' => 'Collections', + 'add_new' => 'Adicionar Novo', + 'add_new_item' =>'Adicionar Collections', + 'edit_item' => 'Editar', + 'new_item' => 'Novo Collections', + 'view_item' => 'Visualizar', + 'search_items' => 'Pesquisar', + 'not_found' => 'Nenhum ticket encontrado', + 'not_found_in_trash' => 'Nenhum Collections encontrado na lixeira', + 'parent_item_colon' => 'Collections acima:', + 'menu_name' => 'Collections' + ); + $args = array( + 'labels' => $labels, + 'hierarchical' => true, + //'supports' => array('title'), + //'taxonomies' => array(self::TAXONOMY), + 'public' => true, + 'show_ui' => false, + 'show_in_menu' => false, + //'menu_position' => 5, + //'show_in_nav_menus' => false, + 'publicly_queryable' => true, + 'exclude_from_search' => true, + 'has_archive' => true, + 'query_var' => true, + 'can_export' => true, + 'rewrite' => true, + 'capability_type' => 'post', + ); + register_post_type(self::POST_TYPE, $args); + } + + function add($title) { + $post = [ + 'post_title' => $title, + 'post_type' => self::POST_TYPE + ]; + return wp_insert_post($post); + } + + function getCollectionById($id) { + return get_post($id); + } + +} + +global $TainacanCollections; +$TainacanCollections = new TainacanCollections(); \ No newline at end of file diff --git a/tests/bin/install-wp-tests.sh b/tests/bin/install-wp-tests.sh new file mode 100755 index 000000000..73bb4c787 --- /dev/null +++ b/tests/bin/install-wp-tests.sh @@ -0,0 +1,127 @@ +#!/usr/bin/env bash + +if [ $# -lt 3 ]; then + echo "usage: $0 [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} + +WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} +WP_CORE_DIR=${WP_CORE_DIR-/tmp/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]+(\.[0-9]+)? ]]; then + WP_TESTS_TAG="tags/$WP_VERSION" +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 /tmp/wordpress-nightly + download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip + unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/ + mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR + else + if [ $WP_VERSION == 'latest' ]; then + local ARCHIVE_NAME='latest' + else + local ARCHIVE_NAME="wordpress-$WP_VERSION" + fi + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz + tar --strip-components=1 -zxmf /tmp/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 diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 000000000..ed908d095 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,25 @@ +add($testTitle); + + $check = $TainacanCollections->getCollectionById($newId); + + $this->assertEquals($check->ID, $newId); + $this->assertEquals($check->post_title, $testTitle); + + } +}