I followed the tutorial on github how to implemented a Plugin as user-defined function.
I used exactly the same code as in the example:
$dwoo->addPlugin('email_safe', 'fix_address');
But I get this error:
Error: Class 'fix_address', does not exist
Thanks!
Instead of using a function
function fix_address(Dwoo\Core $core, $str) {
return $value;
}
it works when using a class, which is odd because it says functions are allowed too
class fix_address{
public static function process($value) {
return $value;
}
}
Related
I just upgraded the phpunit 7.5.20 to phpunit 9.5.0 and I'm facing a lot of errors (good ones actually), but not 100% sure how to workaround with some of those errors.
Just looking for some ideas to fix the following error:
Method setDummyStuff may not return value of type NULL, its return declaration is "void"
It happens only when you're creating a createConfiguredMock() and passing a null method as a argument.
Here's my test:
<?php
use Lib\IDummyCode;
class DummyTest extends PHPUnit\Framework\TestCase
{
public function setUp(): void
{
parent::setUp();
}
public function testDummyThatReturnsVoid()
{
$this->createConfiguredMock(IDummyCode::class, [
'setDummyStuff' => null
]);
}
}
And here's the dummy class:
<?php
namespace Lib;
interface IDummyCode
{
public function setDummyStuff(
int $testInt,
string $testString
): void;
}
Do you guys, have some thoughts about how to improve this?
Thanks a lot!
The second parameter to createConfiguredMock takes an associative array where the key is the method to mock and the value is the value the method should return. Since the setDummyStuff method can not return anything (void return type) it does not make sense to define a return value. It isn't the null value in particular. It will fail with any value.
So you could just leave that method out:
$mock = $this->createConfiguredMock(IDummyCode::class, []);
Which can also be written in a nicer way:
$mock = $this->createStub(IDummyCode::class);
If you need to verify that setDummyStuff was called, you have to set up an expectation.
$mock = $this->createMock(IDummyCode::class);
$mock->expects(self::once())
->method('setDummyStuff')
->with(123, 'Hello');
I've got a method in UsersController
public function addMailbox($data)
{
$this->LoadModel('Mailbox');
$mailbox = $this->Mailbox->newEntity();
$mailbox->username = $data('username');
$mailbox->name = $data('name');
if ($this->Mailbox->save($mailbox)) {
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Error'));
}
, the code works fine when pasted into the add() method, but after using
$this->addMailbox($this->request->getData());
all i get is
error: Function name must be a string
Any ideas?
You've got the wrong syntax for accessing arrays in PHP, use square brackets:
$mailbox->username = $data['username'];
$mailbox->name = $data['name'];
The way you've got it, it's trying to call a function with the variable named in $data, but $data is an array not a string (see Variable functions for more info on that).
Also you shouldn't set user input on $mailbox properties directly - this bypasses validation. Instead just stick $data in newEntity():
public function addMailbox($data)
{
$this->loadModel('Mailbox'); // This also is not required if this function is inside the MailboxController
$mailbox = $this->Mailbox->newEntity($data);
if ($this->Mailbox->save($mailbox)) {
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Error'));
}
I would like to mock the following function:
private fun <T> updateItemInDb(id: Long, column: String, data: T)
which is invoked in the following way by my class:
updateItemInDb(it, DB_POS, i), where it is a Long, DB_POS is String and i is an Int.
I want the function to just run without doing anything. I tried the following in my unit test:
1) every { adapter["updateItemInDb"](any<Long>(), any<String>(), any<Int>()) } just Runs
This gives me a type mismatch error: required MockKStubScope<Unit>, found MockKStubScope<Any?>
2) every { adapter["updateItemInDb"](any<Long>(), any<String>(), any<Int>()) } answers { }
This fails at runtime with io.mockk.MockKException: can't find function updateItemInDb(-1078155520644112829, -d008fa83c4f49c0, 843241211) for dynamic call
Now yes. Generic private functions was fixed since 1.7.16
I've created myself a helper class called Perm that is meant to return user of current session (I know, I could use default auth/user, but that wouldn't be as much of a fun as creating one from scratch!)
..sadly, the created helper class only works inside a view, but doesn't work at all in controllers.. which kinda misses the point.
Whenever I'm trying to use it inside a controller, it pops:
"Class 'App\Http\Controllers\Perm' not found"
I would most appreciate any help.
HelperServiceProvider.php:
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
public function boot()
{
//
}
public function register()
{
foreach( glob (app_path().'/Helpers/*.php' ) as $filename ) // register all helpers
{
require_once($filename);
}
}
}
Helpers/PermHelper.php:
use App\User;
class Perm
{
public static function user()
{
if(!session('user_id')) return null;
return User::find(session('user_id'));
}
}
Portion of config/app.php, the 'providers' array:
// Custom
App\Providers\HelperServiceProvider::class,
If you are having this issue aswell.
Use proper namespacing.
I am making a RTS style game and I have an error:
error CS0120: An object reference is required to access non-static member `CameraOperator.InvertMouseY(float)'
Here is the script with the error:
camPos.y = CameraOperator.InvertMouseY (camPos.y);
I was told to do an instance of it but not sure what to do exactly.
I was told to use, but not sure how to write it. If it replaces whole line or just part.
CameraOperator co;
co.InvertMouseY(camPos.y);
Here is whole script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Unit2 : MonoBehaviour
{
public bool selected = false;
private void Update ()
{
if (renderer.isVisible && Input.GetMouseButtonDown (0)) {
Vector3 camPos = Camera.main.WorldToScreenPoint (transform.position);
camPos.y = CameraOperator.InvertMouseY(camPos.y);
selected = CameraOperator.Selection.Contains (camPos);
}
if (selected) {
renderer.material.color = Color.red;
} else {
renderer.material.color = Color.white;
}
}
}
You're trying to call a member function of CameraOperator without having an instance of the declaring class.
Also InvertMouseY() seems to be declared as a member function, that needs to have an instance of the actual class in order to get called.
CameraOperator is class and you're trying to call InvertMouseY as if it was a static function.
You need an instance,
CameraOperator co;
co.InvertMouseY(camPos.y);