diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..03efbe6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +tests export-ignore +bin export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.travis.yml export-ignore +phpcs.xml export-ignore +README.md export-ignore +LICENSE export-ignore \ No newline at end of file diff --git a/EventManager.php b/EventManager.php new file mode 100644 index 0000000..16f190b --- /dev/null +++ b/EventManager.php @@ -0,0 +1,136 @@ +plugin_api_manager = $plugin_api_manager; + } + + /** + * Adds the given event listener to the list of event listeners + * + * @param string $event_name Name of the event. + * @param callable $listener Listener Callback function. + * @param int $priority Priority. + * @param int $accepted_args Number of arguments. + */ + public function addListener($event_name, $listener, $priority = 10, $accepted_args = 1) + { + $this->plugin_api_manager->addCallback($event_name, $listener, $priority, $accepted_args); + } + + /** + * Adds an event subscriber. + * + * The event manager adds the given subscriber to the list of event listeners + * for all the events that it wants to listen to. + * + * @param SubscriberInterface $subscriber SubscriberInterface implementation. + */ + public function addSubscriber(SubscriberInterface $subscriber) + { + if ($subscriber instanceof EventManagerAwareSubscriberInterface) { + $subscriber->setEventManager($this); + } + + foreach ($subscriber->getSubscribedEvents() as $event_name => $parameters) { + $this->addSubscriberListener($subscriber, $event_name, $parameters); + } + } + + /** + * Removes the given event listener from the list of event listeners + * that listen to the given event. + * + * @param string $event_name Event name. + * @param callable $callback Callback. + * @param int $priority Priority. + * + * @return bool + */ + public function removeListener($event_name, $callback, $priority = 10) + { + return $this->plugin_api_manager->removeCallback($event_name, $callback, $priority); + } + + /** + * Removes an event subscriber. + * + * The event manager removes the given subscriber from the list of event listeners + * for all the events that it wants to listen to. + * + * @param SubscriberInterface $subscriber SubscriberInterface implementation. + */ + public function removeSubscriber(SubscriberInterface $subscriber) + { + foreach ($subscriber->getSubscribedEvents() as $event_name => $parameters) { + $this->removeSubscriberListener($subscriber, $event_name, $parameters); + } + } + + /** + * Adds the given subscriber listener to the list of event listeners + * that listen to the given event. + * + * @param SubscriberInterface $subscriber SubscriberInterface implementation. + * @param string $event_name Event name. + * @param mixed $parameters Parameters, can be a string, an array or a multidimensional array. + */ + private function addSubscriberListener(SubscriberInterface $subscriber, $event_name, $parameters) + { + if (is_string($parameters)) { + $this->addListener($event_name, [ $subscriber, $parameters ]); + } elseif (is_array($parameters) && count($parameters) !== count($parameters, COUNT_RECURSIVE)) { + foreach ($parameters as $parameter) { + $this->addSubscriberListener($subscriber, $event_name, $parameter); + } + } elseif (is_array($parameters) && isset($parameters[0])) { + $this->addListener($event_name, [ $subscriber, $parameters[0] ], isset($parameters[1]) ? $parameters[1] : 10, isset($parameters[2]) ? $parameters[2] : 1); + } + } + + /** + * Removes the given subscriber listener to the list of event listeners + * that listen to the given event. + * + * @param SubscriberInterface $subscriber SubscriberInterface implementation. + * @param string $event_name Event name. + * @param mixed $parameters Parameters, can be a string, an array or a multidimensional array. + */ + private function removeSubscriberListener(SubscriberInterface $subscriber, $event_name, $parameters) + { + if (is_string($parameters)) { + $this->removeListener($event_name, [ $subscriber, $parameters ]); + } elseif (is_array($parameters) && count($parameters) !== count($parameters, COUNT_RECURSIVE)) { + foreach ($parameters as $parameter) { + $this->removeSubscriberListener($subscriber, $event_name, $parameter); + } + } elseif (is_array($parameters) && isset($parameters[0])) { + $this->removeListener($event_name, [ $subscriber, $parameters[0] ], isset($parameters[1]) ? $parameters[1] : 10); + } + } +} diff --git a/EventManagerAwareSubscriberInterface.php b/EventManagerAwareSubscriberInterface.php new file mode 100644 index 0000000..4b02709 --- /dev/null +++ b/EventManagerAwareSubscriberInterface.php @@ -0,0 +1,18 @@ + 'method_name') + * * array('event_name' => array('method_name', $priority)) + * * array('event_name' => array('method_name', $priority, $accepted_args)) + * * array('event_name' => array(array('method_name_1', $priority_1, $accepted_args_1)), array('method_name_2', $priority_2, $accepted_args_2))) + * + * @return array + */ + public static function getSubscribedEvents(); +} diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh new file mode 100644 index 0000000..878881f --- /dev/null +++ b/bin/install-wp-tests.sh @@ -0,0 +1,152 @@ +#!/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} + +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]+$ ]]; 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 diff --git a/composer.json b/composer.json index 8c7ed7c..a83fe78 100644 --- a/composer.json +++ b/composer.json @@ -31,13 +31,14 @@ }, "autoload": { "psr-4": { - "WP_Media\\EventManager\\": "src/" + "WPMedia\\EventManager\\": "" } }, "autoload-dev": { "psr-4": { - "WP_Media\\EventManager\\Tests\\Unit\\": "test/Unit", - "WP_Media\\EventManager\\Tests\\Integration\\": "test/Integration" + "WPMedia\\EventManager\\Tests\\Unit\\": "tests/Unit", + "WPMedia\\EventManager\\Tests\\Integration\\": "tests/Integration", + "WPMedia\\EventManager\\Tests\\Fixtures\\": "tests/Fixtures" } }, "scripts": { diff --git a/tests/Fixtures/DummySubscriber.php b/tests/Fixtures/DummySubscriber.php new file mode 100644 index 0000000..31f5a44 --- /dev/null +++ b/tests/Fixtures/DummySubscriber.php @@ -0,0 +1,22 @@ + 'bar', + 'foofoo' => [ 'barbar', 20 ], + 'foobar' => [ 'foobarbar', 50, 3 ], + 'barfoo' => [ + [ 'filter_this' ], + [ 'filter_that', 10, 2 ], + ], + ]; + } +} diff --git a/tests/Integration/EventManager/addSubscriber.php b/tests/Integration/EventManager/addSubscriber.php new file mode 100644 index 0000000..63c5383 --- /dev/null +++ b/tests/Integration/EventManager/addSubscriber.php @@ -0,0 +1,47 @@ +addSubscriber($subscriber); + + $this->assertSame( + 10, + has_filter('foo', [ $subscriber, 'bar' ]) + ); + $this->assertSame( + 20, + has_filter('foofoo', [ $subscriber, 'barbar' ]) + ); + $this->assertSame( + 50, + has_filter('foobar', [ $subscriber, 'foobarbar' ]) + ); + $this->assertSame( + 10, + has_filter('barfoo', [ $subscriber, 'filter_this' ]) + ); + $this->assertSame( + 10, + has_filter('barfoo', [ $subscriber, 'filter_that' ]) + ); + } +} diff --git a/tests/Integration/EventManager/removeSubscriber.php b/tests/Integration/EventManager/removeSubscriber.php new file mode 100644 index 0000000..7aca294 --- /dev/null +++ b/tests/Integration/EventManager/removeSubscriber.php @@ -0,0 +1,33 @@ +addSubscriber($subscriber); + $manager->removeSubscriber($subscriber); + + $this->assertFalse(has_filter('foo', [ $subscriber, 'bar' ])); + $this->assertFalse(has_filter('foofoo', [ $subscriber, 'barbar' ])); + $this->assertFalse(has_filter('foobar', [ $subscriber, 'foobarbar' ])); + $this->assertFalse(has_filter('barfoo', [ $subscriber, 'filter_this' ])); + $this->assertFalse(has_filter('barfoo', [ $subscriber, 'filter_that' ])); + } +} diff --git a/tests/Integration/PluginApiManager/addCallback.php b/tests/Integration/PluginApiManager/addCallback.php new file mode 100644 index 0000000..7fbec41 --- /dev/null +++ b/tests/Integration/PluginApiManager/addCallback.php @@ -0,0 +1,39 @@ +addCallback('the_content', 'strtolower'); + + $this->assertSame( + 10, + has_filter('the_content', 'strtolower') + ); + } + + /** + * Test should add a new filter with a priority 100 and 3 arguments + */ + public function testShouldAddNewFilterWithAllParameters() { + $plugin_api_manager = new PluginApiManager(); + $plugin_api_manager->addCallback('the_content', 'strtolower', 100, 3); + + $this->assertSame( + 100, + has_filter('the_content', 'strtolower') + ); + } +} diff --git a/tests/Integration/PluginApiManager/execute.php b/tests/Integration/PluginApiManager/execute.php new file mode 100644 index 0000000..575d7ae --- /dev/null +++ b/tests/Integration/PluginApiManager/execute.php @@ -0,0 +1,26 @@ +execute('my_action'); + + $this->assertSame(1, did_action('my_action')); + } +} \ No newline at end of file diff --git a/tests/Integration/PluginApiManager/filter.php b/tests/Integration/PluginApiManager/filter.php new file mode 100644 index 0000000..09626cc --- /dev/null +++ b/tests/Integration/PluginApiManager/filter.php @@ -0,0 +1,31 @@ +filter('my_filter', 'filter value'); + + add_filter('my_filter', 'strtoupper'); + + $this->assertSame( + 10, + has_filter('my_filter', 'strtoupper') + ); + } +} diff --git a/tests/Integration/PluginApiManager/hasCallback.php b/tests/Integration/PluginApiManager/hasCallback.php new file mode 100644 index 0000000..ecf81ff --- /dev/null +++ b/tests/Integration/PluginApiManager/hasCallback.php @@ -0,0 +1,59 @@ +assertFalse($plugin_api_manager->hasCallback('my_custom_filter')); + } + + /** + * Test should return true when a callback is attached to the given hook. + */ + public function testShouldReturnTrueWhenCallbackAttached() { + add_filter('the_content', 'strtolower'); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertTrue($plugin_api_manager->hasCallback('the_content')); + } + + /** + * Test should return false when the given callback is not attached to the given hook. + */ + public function testShouldReturnFalseWhenCallbackNotAttached() { + add_filter('the_content', 'strtolower'); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertFalse($plugin_api_manager->hasCallback('the_content', 'strtoupper')); + } + + /** + * Test should return true when the given callback is attached to the given hook. + */ + public function testShouldReturnPriorityIntWhenCallbackAttached() { + add_filter('the_content', 'strtolower', 11); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertSame( + 11, + $plugin_api_manager->hasCallback('the_content', 'strtolower') + ); + } +} diff --git a/tests/Integration/PluginApiManager/removeCallback.php b/tests/Integration/PluginApiManager/removeCallback.php new file mode 100644 index 0000000..732f632 --- /dev/null +++ b/tests/Integration/PluginApiManager/removeCallback.php @@ -0,0 +1,58 @@ +assertTrue($plugin_api_manager->removeCallback('the_content', 'strtolower')); + } + + /** + * Test should return false when the given callback doesn't exist for the given hook + */ + public function testShouldReturnFalseWhenCallbackDoesNotExist() { + add_filter('the_content', 'strtolower'); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertFalse($plugin_api_manager->removeCallback('the_content', 'strtoupper')); + } + + /** + * Test should return false when the given callback priority doesn't match with the priority when added + */ + public function testShouldReturnFalseWhenPriorityDoesNotMatch() { + add_filter('the_content', 'strtolower'); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertFalse($plugin_api_manager->removeCallback('the_content', 'strtolower', 100)); + } + + /** + * Test should return true when the given callback priority matches with the priority when added + */ + public function testShouldReturnTrueWhenPriorityMatches() { + add_filter('the_content', 'strtolower', 100); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertTrue($plugin_api_manager->removeCallback('the_content', 'strtolower', 100)); + } +} diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php new file mode 100644 index 0000000..ecc9ad4 --- /dev/null +++ b/tests/Integration/TestCase.php @@ -0,0 +1,31 @@ + + + + + + . + TestCase.php + + + \ No newline at end of file diff --git a/tests/TestCaseTrait.php b/tests/TestCaseTrait.php new file mode 100644 index 0000000..f3f3c15 --- /dev/null +++ b/tests/TestCaseTrait.php @@ -0,0 +1,59 @@ +getMethod( $method_name ); + $method->setAccessible( true ); + return $method; + } + + /** + * Get reflective access to the private/protected property. + * + * @param string $property Property name for which to gain access. + * @param string|mixed $class Class name or instance. + * + * @return \ReflectionProperty|string + * @throws \ReflectionException Throws an exception if property does not exist. + */ + protected function get_reflective_property( $property, $class ) { + $class = new \ReflectionClass( $class ); + $property = $class->getProperty( $property ); + $property->setAccessible( true ); + return $property; + } + + /** + * Set the value of a property or private property. + * + * @param mixed $value The value to set for the property. + * @param string $property Property name for which to gain access. + * @param mixed $instance Instance of the target object. + * + * @return \ReflectionProperty|string + * @throws \ReflectionException Throws an exception if property does not exist. + */ + protected function set_reflective_property( $value, $property, $instance ) { + $property = $this->get_reflective_property( $property, $instance ); + $property->setValue( $instance, $value ); + $property->setAccessible( false ); + return $property; + } +} diff --git a/tests/Unit/EventManager/addSubscriber.php b/tests/Unit/EventManager/addSubscriber.php new file mode 100644 index 0000000..a0a3785 --- /dev/null +++ b/tests/Unit/EventManager/addSubscriber.php @@ -0,0 +1,29 @@ +shouldReceive('addCallback') + ->times(5); + + $manager = new EventManager($plugin_api_manager); + + $manager->addSubscriber(new DummySubscriber()); + } +} diff --git a/tests/Unit/EventManager/removeSubscriber.php b/tests/Unit/EventManager/removeSubscriber.php new file mode 100644 index 0000000..767597b --- /dev/null +++ b/tests/Unit/EventManager/removeSubscriber.php @@ -0,0 +1,29 @@ +shouldReceive('removeCallback') + ->times(5); + + $manager = new EventManager($plugin_api_manager); + + $manager->removeSubscriber(new DummySubscriber()); + } +} diff --git a/tests/Unit/PluginApiManager/addCallback.php b/tests/Unit/PluginApiManager/addCallback.php new file mode 100644 index 0000000..030c712 --- /dev/null +++ b/tests/Unit/PluginApiManager/addCallback.php @@ -0,0 +1,37 @@ +with('strtolower', 10, 1); + + $plugin_api_manager = new PluginApiManager(); + $plugin_api_manager->addCallback('the_content', 'strtolower'); + } + + /** + * Test should add a new filter with a priority 100 and 3 arguments + */ + public function testShouldAddNewFilterWithAllParameters() { + Filters\expectAdded('the_content') + ->with('strtolower', 100, 3); + + $plugin_api_manager = new PluginApiManager(); + $plugin_api_manager->addCallback('the_content', 'strtolower', 100, 3); + } +} diff --git a/tests/Unit/PluginApiManager/execute.php b/tests/Unit/PluginApiManager/execute.php new file mode 100644 index 0000000..4cc3c80 --- /dev/null +++ b/tests/Unit/PluginApiManager/execute.php @@ -0,0 +1,26 @@ +execute('my_action'); + + $this->assertSame(1, did_action('my_action')); + } +} diff --git a/tests/Unit/PluginApiManager/filter.php b/tests/Unit/PluginApiManager/filter.php new file mode 100644 index 0000000..24d6177 --- /dev/null +++ b/tests/Unit/PluginApiManager/filter.php @@ -0,0 +1,26 @@ +filter('my_filter', 'filter value'); + + $this->assertTrue(Filters\applied('my_filter') > 0); + } +} diff --git a/tests/Unit/PluginApiManager/hasCallback.php b/tests/Unit/PluginApiManager/hasCallback.php new file mode 100644 index 0000000..ccbf1db --- /dev/null +++ b/tests/Unit/PluginApiManager/hasCallback.php @@ -0,0 +1,56 @@ +assertFalse($plugin_api_manager->hasCallback('the_content')); + } + + /** + * Test should return true when a callback is attached to the given hook. + */ + public function testShouldReturnTrueWhenCallbackAttached() { + add_filter('the_content', 'strtolower'); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertTrue($plugin_api_manager->hasCallback('the_content', null)); + } + + /** + * Test should return false when the given callback is not attached to the given hook. + */ + public function testShouldReturnFalseWhenCallbackNotAttached() { + add_filter('the_content', 'strtolower'); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertFalse($plugin_api_manager->hasCallback('the_content', 'strtoupper')); + } + + /** + * Test should return true when the given callback is attached to the given hook. + */ + public function testShouldReturnPriorityIntWhenCallbackAttached() { + add_filter('the_content', 'strtolower', 11); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertTrue($plugin_api_manager->hasCallback('the_content', 'strtolower')); + } +} diff --git a/tests/Unit/PluginApiManager/removeCallback.php b/tests/Unit/PluginApiManager/removeCallback.php new file mode 100644 index 0000000..556f616 --- /dev/null +++ b/tests/Unit/PluginApiManager/removeCallback.php @@ -0,0 +1,58 @@ +assertTrue($plugin_api_manager->removeCallback('the_content', 'strtolower')); + } + + /** + * Test should return false when the given callback doesn't exist for the given hook + */ + public function testShouldReturnFalseWhenCallbackDoesNotExist() { + add_filter('the_content', 'strtolower'); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertFalse($plugin_api_manager->removeCallback('the_content', 'strtoupper')); + } + + /** + * Test should return false when the given callback priority doesn't match with the priority when added + */ + public function testShouldReturnFalseWhenPriorityDoesNotMatch() { + add_filter('the_content', 'strtolower'); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertFalse($plugin_api_manager->removeCallback('the_content', 'strtolower', 100)); + } + + /** + * Test should return true when the given callback priority matches with the priority when added + */ + public function testShouldReturnTrueWhenPriorityMatches() { + add_filter('the_content', 'strtolower', 100); + + $plugin_api_manager = new PluginApiManager(); + + $this->assertTrue($plugin_api_manager->removeCallback('the_content', 'strtolower', 100)); + } +} diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php new file mode 100644 index 0000000..706b8b8 --- /dev/null +++ b/tests/Unit/TestCase.php @@ -0,0 +1,33 @@ + + + + + + . + TestCase.php + + + + + + . + + + \ No newline at end of file diff --git a/tests/bootstrap-functions.php b/tests/bootstrap-functions.php new file mode 100644 index 0000000..8e4c114 --- /dev/null +++ b/tests/bootstrap-functions.php @@ -0,0 +1,48 @@ +