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;
}
Related
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.
I am writing Slim3 API, for some reason slim is not loading namespace defined in composer.
Here is the project structure.
FolderStruc:
projectApi
- composer.json
- src
- public
- index.php
- ProjectName
- Api
- Controllers
- Entities
- Commands
My composer file packages and PSR-4 autoloader settings.
composer.json
{
"require": {
"slim/slim": "^3.0",
"symfony/yaml": "3.1",
"symfony/console": "3.1",
"symfony/process": "3.1",
"doctrine/orm": "^2.5",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"sabre/xml": "1.1.0",
"php-di/php-di": "#dev",
"php-amqplib/php-amqplib": "#dev",
"ramsey/uuid": "dev-master",
"monolog/monolog": "~1.15#dev",
"predis/predis": "~1.0.1",
"spipu/html2pdf": "^4.5",
"iio/libmergepdf": "~3.0"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"src\\ProjectName\\": "src/ProjectName/"
}
},
"autoload-dev": {
"psr-4": { "src\\ProjectName\\Tests\\": "tests" }
}
}
index.php
CategoryController
namespace ProjectName\Api\Controllers;
class CategoryController
{
/**
* #param \Slim\App $app
* #return array
*/
public function index(\Slim\App $app)
{
return ['Cats', 'Cats2', 'Cats3'];
}
}
** routes.php **
$app->get('/v1/category/list', function (Request $request, Response $response) {
$response = $response->withHeader('Content-type', 'application/json');
$categoryCtrl = new \ProjectName\Api\Controllers\CategoryController();
});
Slim fails to load namespace \ProjectName\Api\Controllers\CategoryController
any idea where and what is going wrong?
Best Regards
Danyal
You have incorrect autoloader definitions in your composer.json file.
The pattern is:
"psr-4": {
"Namespace\\Prefix\\": "/path/to/source/root"
}
So in your case it probably should be:
"autoload": {
"psr-4": {
"ProjectName\\": "src/ProjectName/"
}
},
"autoload-dev": {
"psr-4": { "ProjectName\\Tests\\": "tests/" }
}
autoload-dev definition is assuming there's a tests directory under project root path, which is not mentioned in your question.
I am using slim framework version 3. I have used Laravel's illuminate database. In my controller when I write below query it gives me error that "Class DB not Found" I have also specified use DB; in my controller inspite of that it does not allow me to write such mysql query.
$students = DB::table('students')->row();
This is my composer.json file content
"require": {
"slim/slim": "^3.0",
"slim/twig-view": "^1.0",
"vlucas/valitron": "^1.2",
"slim/csrf": "^0.3.3",
"slim/flash": "^0.1.0",
"illuminate/database": "5.2.*",
"illuminate/events": "5.2.*",
"illuminate/cache": "5.2.*",
"illuminate/filesystem": "5.2.*",
"luracast/config": "2.*",
"vlucas/phpdotenv": "~1.0",
"league/flysystem": "~1.0",
"illuminate/pagination": "5.2.*"
},
I have all necessary files in my vendor directory. But I want to use DB in my controller
Here is my controller code:-
<?php
namespace Controller;
use Slim\Views\Twig;
use Slim\Router;
use Slim\Flash\Messages as FlashMessages;
use Model\Student;
use DB;
final class StudentController
{
private $view;
private $router;
private $flash;
public function __construct(Twig $view, Router $router, FlashMessages $flash)
{
$this->view = $view;
$this->router = $router;
$this->flash = $flash;
}
public function listStudents($request, $response, $params)
{
$students = DB::table('students')->get();
return $this->view->render($response, 'students/list.twig', [
'students' => Student::all(),
]);
}
}
You have to correct used namespace:
use \DB;
or
use Illuminate\Support\Facades\DB;
instead of use DB
Also you might take a look at tutorial for using database illuminate without Laravel
I have a simple composer project on GitHub with the directory structure as:
/--
-composer.json
-lib/ComposerTest.php
with the ComposerTest.php file as:
namespace lib;
class ComposerTest{
public function doTest(){
return "This class was loaded from Composer\n";
}
}
and composer.json as:
{
"name":"sc/composerTest",
"autoload":{
"psr-0":{
"lib":"./"
}
}
}
Using the following composer.json file, I'm able to include the GitHub project into my vendor folder, but cannot get it to autoload.
{
"description" : "The CodeIgniter Application with Composer",
"require": {
"php": ">=5.3.2",
"codeigniter/framework": "3.1.*",
"kriswallsmith/buzz":"*",
"maltyxx/bower": "^1.0",
"sc/composerTest":"dev-master"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/sc/composerTest.git"
}
]
}
Can someone advise?
Edit: ComposerTest Controller
class ComposerTest extends CI_Controller
{
public function index()
{
$composerTest = new ComposerTest();
echo $composerTest->doTest();
}
}
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