Method not found - PHPUnit Confusion - php

Code
class Rechner {
public function add($a, $b){
return $a + $b;
}
My TestCase
class RechnerTest extends PHPUnit\Framework\TestCase
{
public $calculator;
protected function setUp ()
{
$this->calculator = new Rechner();
}
protected function tearDown ()
{
$this->calculator = NULL;
}
public function testAdd ()
{
$result = $this->calculator->add(1, 2);
$this->assertEquals(3, $result);
}
}
Error Output
Error: Class 'Rechner' not found
Composer.json file
{
"autoload":{
"psr-0" :{
"":"src"
},
"classmap": [
"tests"
]
},
"require": {
"phpunit/phpunit": "^6.5",
"authorizenet/authorizenet": "^1.9"
}
}
My Problem
I am trying to figure out why the Method is not found.
If i try to require __DIR__ the 'Recher.php' does not show up, only the test case...and thats the one i don't need.
Im am not sure what to do.

Related

cant use laravel facase inside the package

i am trying to write a package for laravel . i want to use facade and call some dynamic class with :: like this :
Zaya::test();
so , my structure is /packages/company/zaya and here is my composer for package :
"extra": {
"laravel": {
"providers": [
"Company\\Zaya\\ZayaServiceProvider"
],
"aliases": {
"Zaya": "Company\\Zaya\\ZayaFacade"
}
}
this is my fadace :
protected static function getFacadeAccessor()
{
return 'zaya';
}
and this is my service provider :
// Register the main class to use with the facade
$this->app->singleton('zaya', function () {
return new Zaya;
});
and finally this is my class :
<?php
namespace company\Zaya;
class Zaya
{
public function test()
{
return 123;
}
}
now in my controller when i call :
dd(Zaya::test());
i get this error :
"message": "Non-static method Company\\Zaya\\Zaya::test() should not be called statically",
You can use the app helper to resolve the class:
class MyFacade
{
public static function __callStatic($method, $arguments)
{
$class = app(MyClass::class);
return $class->$method(...$arguments);
}
}
To use it:
MyFacade::foo();
MyFacade::bar();
MyFacade::baz();
// or in your case
Zaya::test();

Making Laravel Package. but "Class Not Found"

I'm making laravel's package.
But Class "Username/PackageName/Class" not found.
I read this.
But I can't find problem.
Please help me.
This is my codes
composer.json
{
"name": "username/packageName",
"require": {
"php": "7.3",
"laravel/framework": "^6.0"
},
"autoload": {
"psr-4": {
"username\\packageName\\": "src/"
}
},
"license": "MIT",
"authors": [
],
"extra": {
"laravel": {
"providers": [
"username\\packgeName\\TestServiceProvider"
]
}
}
}
src/TestServiceProvider
<?php
namespace username\packageName;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind('test', function ($app) {
return new Test();
});
}
}
src/Test.php
<?php
namespace username\packageName;
class Test {
public static function test(){
return 'abc';
}
}
in Controller
use username\packageName\Test;
/* ~~~~ */
dd(Test::test()); // error
after add current composer.json.
"repositories": [
{
"type": "path",
"url": "vendor/username/packageName",
"symlink": true
}
]
> composer dump-autoload
Thank you for reading my codes.
username -> my username
packageName -> my package name
Have you extend Facades class in your Test Class.
If you want to fetch all function of your files statically,
class Test extends Facades {
public static function getFacadeAccessor(){
return 'classNameWhereFunctionIsWritten' //say TestService has test() function
}
}
And Pass this classNameWhereFunctionIsWritten in your provider 'classNameWhereFunctionIsWritten' here.

PHP Class not found using namespaces

I can't find out why my classes won't load. I am using Composer for psr-4 autoloading and have been using it successfully. Here's how I have my classes setup:
/project
/classes
/feeds
/pull
/factory
composer.json
testMyFactory.php
feeds/factory/FeedFactory.php
namespace MyClasses\Feeds\Factory;
interface FeedFactory
{
public function build($provider);
}
feeds/factory/PullFeedFactory.php
namespace MyClasses\Feeds\Factory;
use MyClasses\Feeds\Factory\FeedFactory;
use MyClasses\Feeds\Pull\Providers\One;
/**
* Class FeedFactory
*/
class PullFeedFactory implements FeedFactory
{
public function __construct(){}
/**
* Build provider object for factory
* #param string $provider Type of feed provider to return
* #return Object Provider object
*/
public function build($provider) {
switch ($provider) {
case 'one':
$provider = new One();
break;
default:
$provider = new One`();
break;
}
return $provider;
}
}
project/feeds/pull/One.php
namespace MyClasses\Feeds\Pull\Providers;
class One
{
public function pull() {
echo 'Pull One';
}
}
project/testMyFactory.php
require __DIR__ . '/vendor/autoload.php';
use MyClasses\Feeds\Factory\PullFeedFactory;
$feed = new PullFeedFactory();
$feed->build('one');
$feed->pull();
project/composer.json
{
"require": {
//Remove for example
},
"config": {
"preferred-install": "dist"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"MyClasses\\": "./classes",
}
}
}
This is the error I keep getting Class 'MyClasses\Feeds\Factory\PullFeedFactory' not found in /var/www/html/testPullFactory.php on line xx
I have other classes that work in the Classes directory with autoload but for some reason cannot get this to work. I feel like it's something glaringly obvious but have been stuck on this for hours now.
UPDATE:
Updated to include my vendor/autoload.php file. Stil getting an error, although it's different now Class 'MyClasses\Feeds\Pull\Providers\One' not found in /var/www/html/classes/Feeds/Factory/PullFeedFactory.php
in your composer.json, change to
"autoload": {
"psr-4": {
"MyClasses\\": "classes/",
}
}
Then, execute composer dump-autoload.

Silex web test fails

I'm trying to write a simple web test to test my silex api using phpunit but
I keep getting this error when i try to run it..
1) App\Tests\BlogControllerTest::testInitialPage
TypeError: Argument 1 passed to Symfony\Component\HttpKernel\Client::__construct() must implement interface Symfony\Component\HttpKernel\HttpKernelInterface, null given, called in C:\vendor\silex\silex\src\Silex\WebTestCase.php on line 63
This is my web test
<?php
namespace App\Tests;
use Silex\WebTestCase;
class BlogControllerTest extends WebTestCase
{
public function createApplication()
{
require dirname(dirname(__DIR__)) . '/src/Application.php';
}
public function testInitialPage()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/Blog/1');
$this->assertTrue($client->getResponse()->isOk());
}
}
The content of my Composer file:
{
"require": {
"silex/silex": "~2.0",
"symfony/validator": "^4.0",
"crell/api-problem": "~1.7",
"tobiassjosten/responsible-service-provider": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "6",
"symfony/browser-kit": "^4.0",
"symfony/css-selector": "^4.0"
},
"autoload": {
"psr-4": {
"App\\": "src/",
"Tests\\": "tests/"
}
}
}
From documentation:
For your WebTestCase, you will have to implement a createApplication method, which returns your application instance:
public function createApplication()
{
// app.php must return an Application instance
return require __DIR__.'/path/to/app.php';
}
So here's what you need:
public function createApplication()
{
return new Application();
}
But in the most cases it would be better to return configured application, from your bootstrap (bootstrap example):
public function createApplication()
{
require __DIR__.'../web/index-test.php'; // path to your bootstrap file
return $app;
}

ServiceProvider not found on phpunit testing for Laravel 4

I am new to laravel 4, and I am following a Laravel tutorial on Culttt.com right now. I added a package into the project and create a Facade to access: Philipbrown/Suypo, it works fine.
workbench\philipbrown\supyo\src\Philipbrown\Supyo\SuypoServiceProvider.phh
<?php namespace Philipbrown\Supyo;
use Illuminate\Support\ServiceProvider;
class SupyoServiceProvider extends ServiceProvider {
protected $defer = false;
public function boot()
{
$this->package('philipbrown/supyo');
}
public function register()
{
$this->app['supyo'] = $this->app->share(function($app)
{
return new Supyo;
});
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Supyo', 'Philipbrown\Supyo\Facades\Supyo');
});
}
public function provides()
{
return array('supyo');
}
}
This is the composer.json file of my package:
{
"name": "philipbrown/supyo",
"description": "",
"authors": [
{
"name": "ChaoMeng",
"email": "cmeng#idfbins.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "4.2.*"
},
"autoload": {
"classmap": [
"src/migrations"
],
"psr-0": {
"Philipbrown\\Supyo": "src/"
}
},
"minimum-stability": "stable"
}
But when I write some tests and use phpunit to run them, it shows this error:
Fatal error: Class 'Philipbrown\Supyo\SupyoServiceProvider' not found in C:\Dev\wamp\www\Culttt\laravel\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php on line 158
I tried to run command: composer dump-autoload but it does not work. and I did not call or use this package in the test, so I really don't know what happens here, below is my test.php:
class CliqueTest extends TestCase {
/**
* Test that the name is required for Clique
*/
public function testNameIsRequired()
{
// Create a new Clique
$clique = new Clique;
// Post should not save
$this->assertFalse($clique->save());
// Save the errors
$errors = $clique->errors()->all();
// There should be 1 error
$this->assertCount(1, $errors);
// The error should be set
$this->assertEquals($errors[0], "The name field is required.");
}
public function testCliqueUserRelationship()
{
// Create a new Clique
$clique = FactoryMuff::create('Clique');
// Create two Users
$user1 = FactoryMuff::create('User');
$user2 = FactoryMuff::create('User');
// Save Users to the Clique
$clique->users()->save($user1);
$clique->users()->save($user2);
// Count number of Users
$this->assertCount(2, $clique->users);
}
}
So please give me a idea about what's going on. Thanks in advance.
This is the whole code in github: https://github.com/mc422/laravel.git

Categories