How to resolve "class not found" in PHPUnit tests? - phpunit

I have just resolved some PHPUnit problems by upgrading to the last version (6.2.1) but and I'm now able to run my tests.
However I have a new error that I can't resolve:
1) BackBundle\Tests\Service\LdapServiceTest::testgetLdapConnect
Error: Class 'BackBundle\Service\LdapService' not found
There is my classTest:
<?php
namespace BackBundle\Tests\Service;
use PHPUnit\Framework\TestCase;
use BackBundle\Service\LdapService;
use PHPUnit_Framework_MockObject_InvocationMocker;
class LdapServiceTest extends TestCase {
public function testgetLdapConnect()
{
$ldap = new LdapService('10.0.0.100','10.0.0.100','mike','password',389,3,false,false);
$ldapMock = $this->getMockBuilder( 'LdapService')->setMethods(array('getldapBind'))->disableOriginalConstructor()->getMock();
$ldapMock->expects($this->any())
->method('getLdapBind')
->with(array('ldap_bind', 'mike', 'password'))
->will($this->returnValue(true));
$this->assertTrue($ldap->getLdapBind());
// $LdapService = new LdapService();
// $LdapService.getLdapBind();
// $ldapMock->isAuthorized('mike', 'password');
}
}
And here is my phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="app"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_DIR" value="app/" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
<exclude>
<directory>src/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Here is the content of the bootstrap.php:
<?php
if (!defined('TEST_FILES_PATH')) {
define('TEST_FILES_PATH', __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR);
}
ini_set('precision', 14);
ini_set('serialize_precision', 14);
require_once __DIR__ . '/../vendor/autoload.php';
// TODO: Figure out why (some of) these are required (the classes should be autoloaded instead)
require_once TEST_FILES_PATH . 'BeforeAndAfterTest.php';
require_once TEST_FILES_PATH . 'BeforeClassAndAfterClassTest.php';
require_once TEST_FILES_PATH . 'TestWithTest.php';
require_once TEST_FILES_PATH . 'BeforeClassWithOnlyDataProviderTest.php';
require_once TEST_FILES_PATH . 'DataProviderSkippedTest.php';
require_once TEST_FILES_PATH . 'DataProviderDependencyTest.php';
require_once TEST_FILES_PATH . 'DataProviderIncompleteTest.php';
require_once TEST_FILES_PATH . 'InheritedTestCase.php';
require_once TEST_FILES_PATH . 'NoTestCaseClass.php';
require_once TEST_FILES_PATH . 'NoTestCases.php';
require_once TEST_FILES_PATH . 'NotPublicTestCase.php';
require_once TEST_FILES_PATH . 'NotVoidTestCase.php';
require_once TEST_FILES_PATH . 'OverrideTestCase.php';
require_once TEST_FILES_PATH . 'RequirementsClassBeforeClassHookTest.php';
require_once TEST_FILES_PATH . 'NoArgTestCaseTest.php';
require_once TEST_FILES_PATH . 'Singleton.php';
require_once TEST_FILES_PATH . 'Mockable.php';
require_once TEST_FILES_PATH . 'CoverageNamespacedFunctionTest.php';
require_once TEST_FILES_PATH . 'NamespaceCoveredFunction.php';

Related

Rector Failed to create "/tmp/rector_cached_files/26": mkdir(): Permission denied

When I run the command:
vendor/bin/rector process --dry-run
I get the error:
[ERROR] Failed to create "/tmp/rector_cached_files/26": mkdir(): Permission denied
Web server processes on this system currently run as the user.
If I run the script as root it runs fine. It needs to be run by user!
I have a domain that this code runs fine on. I created a new domain called https://rector.86it.us and loaded the very same config that is already working on another domain. I did the very same setup with composer multiple times only to get the very same error over and over.
The new domain is on the same server and the configs are identical. I run PHP-fpm and I even switched back and forth from php 7.4 to php 8.0 and then php 8.1
I am running Apache with PHP 8.1 (ea-php81) (PHP-FPM)
My current rector.php config.
<?php
declare(strict_types=1);
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Core\ValueObject\PhpVersion;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->skip([
//__DIR__ . '/src/SingleFile.php',
__DIR__ . '/assets',
__DIR__ . '/blocks',
__DIR__ . '/install',
__DIR__ . '/modules/Forums',
__DIR__ . '/modules/Advertising',
__DIR__ . '/modules/Arcade_Tweaks',
__DIR__ . '/modules/Blog_Submit',
__DIR__ . '/modules/Blog_Topics',
__DIR__ . '/modules/Blogs',
__DIR__ . '/modules/Blogs_Top',
__DIR__ . '/modules/Bookmarks',
__DIR__ . '/modules/Cemetery',
__DIR__ . '/modules/cPanel_Login',
__DIR__ . '/modules/CSS_Color_Chart',
__DIR__ . '/modules/CSS_Reference',
__DIR__ . '/modules/Docs',
__DIR__ . '/modules/Donations',
__DIR__ . '/modules/ECalendar',
__DIR__ . '/modules/Evo_UserBlock',
__DIR__ . '/modules/FAQ',
__DIR__ . '/modules/Feedback',
__DIR__ . '/modules/File_Repository',
__DIR__ . '/modules/Google-Site-Map',
__DIR__ . '/modules/Groups',
__DIR__ . '/modules/HTML_Newsletter',
__DIR__ . '/modules/HTML_to_PHP',
__DIR__ . '/modules/Image_Repository',
__DIR__ . '/modules/Link_Us',
__DIR__ . '/modules/Loan_Amortization',
__DIR__ . '/modules/Member_List',
__DIR__ . '/modules/My_Forum_Topics',
__DIR__ . '/modules/Network',
__DIR__ . '/modules/Network_Advertising',
__DIR__ . '/modules/Network_Projects',
__DIR__ . '/modules/NukeSentinel',
__DIR__ . '/modules/Private_Messages',
__DIR__ . '/modules/Profile',
__DIR__ . '/modules/Proof_Of_God',
__DIR__ . '/modules/Recommend_Us',
__DIR__ . '/modules/Reviews',
__DIR__ . '/modules/Search',
__DIR__ . '/modules/Shout_Box',
__DIR__ . '/modules/Spambot_Killer',
__DIR__ . '/modules/Statistics',
__DIR__ . '/modules/Surveys',
__DIR__ . '/modules/Titanium_SandBox',
__DIR__ . '/modules/Web_Links',
__DIR__ . '/modules/Your_Account',
__DIR__ . '/admin',
//__DIR__ . '/includes',
__DIR__ . '/themes',
__DIR__ . '/dev_modules',
__DIR__ . '/cgi-bin',
__DIR__ . '/.well-known',
__DIR__ . '/vendor',
// or use fnmatch
//__DIR__ . '/src/*/Tests/*',
]);
$rectorConfig->paths([
//__DIR__ . '/admin',
//__DIR__ . '/images',
//
//__DIR__ . '/mainfile.php',
//__DIR__ . '/index.php',
//__DIR__ . '/includes/Facebook/FacebookApp.php',
//__DIR__ . '/includes/classes/class.cache.php',
//__DIR__ . '/modules/Blogs/comments.php',
__DIR__ . '/includes/classes/class.identify.php',
//__DIR__ . '/modules/Shout_Box/shout.php',
//__DIR__ . '/modules/Your_Account/public/new_user1.php',
//__DIR__ . '/modules/Forums/posting.php',
//
]);
// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
$rectorConfig->phpVersion(PhpVersion::PHP_81);
//define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
]);
};
?>
open vendor/rector/rector/config.php
goto line: 68
find:
$rectorConfig->cacheDirectory(\sys_get_temp_dir() . '/rector_cached_files');
change to:
$rectorConfig->cacheDirectory(__DIR__ . '/rector_cached_files');
This will fix the problem, CHEERS!

PHP mailer not Working with wordpress 5.5

I have a website on wordpress 5.4 and recently it was updated to version 5.5 and now i am unable to send emails using PHPMailer. The Error i get is "There has been a critical error on your website" . The below code will work perfectly for previous wordpress Versions.
include_once( ABSPATH . WPINC . '/class-phpmailer.php' );
include_once( ABSPATH . WPINC . '/includes/PHPMailer/PHPMailerAutoload.php' );
$mailserver_url = "[mailserver_url]";
$mailserver_login = "[mailserver_login]";
$mailserver_pass = '[mailserver_pass]';
$mailserver_port = '[mailserver_port]';
$email = '[email]';
$mail = new PHPMailer;
$mail->ClearAttachments();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => false
)
);
$mail->Host = $mailserver_url;
$mail->Port = $mailserver_port;
$mail->Username = $mailserver_login;
$mail->Password = $mailserver_pass;
$mail->setFrom( $email );
$mail->addReplyTo( $mailserver_login );
$mail->addAddress( $email );
$mail->Subject = 'The Subject';
$mail->isHTML();
$mail->Body = '<p>Helloo</p>';
if ( $mail->Send() ) {
echo 'sent';
}
I looked into the below article but i wasnt able to fix it https://wordpress.org/support/topic/fatal-error-after-updating-to-wp-5-5/
If you want to use it in a plugin and keep PHP Mailer compatible for older WordPress version, this is the complete solution.
global $wp_version;
if( $wp_version < '5.5') {
require_once(ABSPATH . WPINC . '/class-phpmailer.php');
require_once(ABSPATH . WPINC . '/class-smtp.php');
$mail = new PHPMailer( true );
}
else {
require_once(ABSPATH . WPINC . '/PHPMailer/PHPMailer.php');
require_once(ABSPATH . WPINC . '/PHPMailer/SMTP.php');
require_once(ABSPATH . WPINC . '/PHPMailer/Exception.php');
$mail = new PHPMailer\PHPMailer\PHPMailer( true );
}
It’s because you are attempting to load an old version of PHPMailer that no longer exists in WordPress, and getting a fatal error as a result. You should not have to load PHPMailer yourself because WordPress supplies it as standard, so refer to their docs for how to send messages, and how to create a hook to inject a custom configuration.
You can still load it yourself, but by doing so you bypass all that WP is doing for you and become liable for everything that goes with it, including loading the classes properly. To help update your code to work with PHPMailer 6.x, read the upgrade guide, or this question.
With WordPress upgrade to version 5.5 it may be necessary to include the PHPMailer SMPT.php file.
include_once (ABSPATH . WPINC . '/class-phpmailer.php');
include_once (ABSPATH . WPINC . '/PHPMailer/SMTP.php');
$mail = new PHPMailer ();
WordPress has now moved PHP mailer into a subdirecotry, you need to update your code as follows:
At the top of your function add:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
The update your code further down to:
require_once(\ABSPATH . \WPINC . "/PHPMailer/PHPMailer.php");
require_once(\ABSPATH . \WPINC . "/PHPMailer/Exception.php");
require_once(\ABSPATH . \WPINC . "/PHPMailer/SMTP.php");
$mail = new PHPMailer();

Getting wordpress to work with the same wp-config.php on wamp localhost and live server

To facilitate my localhost -> live server migrations, I would like my local Wordpress site (powered by wamp) to work with the exact same wp-config.php file than the one I have online:
//Define new directory path
define ('WP_CONTENT_DIR', ABSPATH . WP_CONTENT_FOLDERNAME);
//Define new directory URL
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_CONTENT_URL', WP_SITEURL . WP_CONTENT_FOLDERNAME);
So far this is not working, so I have to use this other configuration:
define( 'WP_CONTENT_DIR', 'C:\wamp64\www\domain.dev\content' );
define( 'WP_CONTENT_URL', 'http://localhost/domain.dev/content' );
What should I do to be able to use the first configuration?
You could use the php $_SERVER array to get the host $_SERVER['HTTP_HOST'] and compare if it is the dev or the live environment.
So, your wp-config.php file will looks something like this:
$cur_env = $_SERVER['HTTP_HOST'];
if($cur_env == 'localhost'){ //Or could be '127.0.0.1' - Or whatever your wamp base url is set.
define( 'WP_CONTENT_DIR', 'C:\wamp64\www\domain.dev\content' );
define( 'WP_CONTENT_URL', 'http://localhost/domain.dev/content' );
}else{
define('WP_CONTENT_DIR', ABSPATH . WP_CONTENT_FOLDERNAME);
define('WP_CONTENT_URL', WP_SITEURL . WP_CONTENT_FOLDERNAME);
}

How to include Guzzle in a wordpress theme/plugin

How can i include Guzzle into a wordpress theme or plugin. Since the theme/plugins will be distributed to non tech users i cannot ask them to do that via Composer.
I have uploaded the guzzle pack in a theme folder in libs/resources/ and in functions.php and I have defined this autoloader for guzzle classes only
spl_autoload_register( 'guzzle_autoloader' );
function guzzle_autoloader( $class_name ) {
if ( false !== strpos( $class_name, 'GuzzleHttp' ) ) {
$classes_dir = realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'libs' . DIRECTORY_SEPARATOR.'resources'. DIRECTORY_SEPARATOR .'guzzle-master'. DIRECTORY_SEPARATOR .'src'. DIRECTORY_SEPARATOR ;
$replace="GuzzleHttp\\";
$class_file = str_replace($replace,'', $class_name);
print 'request '.$classes_dir . $class_file.'</br>';
require_once $classes_dir . $class_file.'.php';
}
}
However i'm still getting this error "call to undefined function GuzzleHttp\choose_handler() "
What about composer autoload?
Simply Require Guzzle packagist repo with composer
composer require guzzlehttp/guzzle
Install
composer install
Simply autoload Guzzle Classes in your Wordpress plugin
require 'vendor/autoload.php';
//use GuzzleHttp\Client;

Wordpress - loadin admin.css at last

This is my code to load a CSS file in the wordpress backend:
function customAdminStyles() {
$url = get_bloginfo('template_url') . '/wp-admin.css';
echo '<!-- custom admin css -->
<link rel="stylesheet" type="text/css" href="' . $url . '" />
<!-- /end custom adming css -->';
}
add_action('admin_head', 'customAdminStyles');
The problem is that the styles are overwritten by original wordpress CSS declarations, so i have to put a "!important" behind every declaration - i don't like that. Any idea how to load it at last? I don't want to use a plugin.
Try this code . This will load admin-style.css in the backend
add_action( 'admin_enqueue_scripts','admin_styles',10 );
function admin_styles() {
wp_register_style( 'custom_admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
wp_enqueue_style( 'custom_admin_css' );
}

Resources