小编sea*_*saw的帖子

在单元测试时使用Laravel辅助函数

我正在测试一个使用辅助函数的类.我正在测试的功能如下:

public function upload(UploadedFile $file)
{
    $file = $file->move(base_path() . '/temp');
    $file = new FileSystemPicture($file);

    return $file;
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试并且它命中辅助函数时,它说:

PHP Fatal error:  Call to a member function make() on a non-object
Run Code Online (Sandbox Code Playgroud)

我已经跟踪了辅助函数,它是以下代码:

function base_path($path = '')
{
    return app()->make('path.base').($path ? '/'.$path : $path);
}
Run Code Online (Sandbox Code Playgroud)

我知道我收到了错误,因为"app"尚未创建,因为我只测试了我的类,而不是整个应用程序.是否有其他有效的方法来获取我的项目的根目录或有没有办法强制"app"加载,以便它可以在此功能中使用?

php phpunit unit-testing laravel-4

14
推荐指数
1
解决办法
5302
查看次数

FOSUserBundle未在DoctrineFixtures中设置Salt

我正在尝试将一些用户加载到我的数据库中,以便使用DoctrineFixturesBundle进行测试.我在StackOverflow上做了很多搜索,并且发现了很多其他问题.似乎没有我的答案.我正在加载我的用户使用FOS用户管理器.下面是我的夹具课程.

namespace Kandidly\UserBundle\DataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
    private $container;

    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $userManager = $this->container->get('fos_user.user_manager');

        $user1 = $userManager->createUser();
        $user1->setFirstName('Admin');
        $user1->setLastName('User');
        $user1->setCity('Somewhere');
        $user1->setState('TN');
        $user1->setUsername('admin');
        $user1->setEmail('admin@example.com');
        $user1->setPlainPassword('admin');
        $user1->setEnabled(true);
        $user1->setRoles(
            array(
                 'ROLE_ADMIN'
            )
        );

        $user2 = $userManager->createUser();
        $user2->setFirstName('Normal');
        $user2->setLastName('User');
        $user2->setCity('Another');
        $user2->setState('CA');
        $user2->setUsername('user');
        $user2->setEmail('use@example.com');
        $user2->setPlainPassword('user');
        $user2->setEnabled(true);
        $user2->setRoles(
            array(
                 'ROLE_USER'
            )
        );

        $userManager->updateUser($user1);
        $userManager->updateUser($user2);

        $this->addReference('admin-user', $user1);
    }

    /**
     * Get the order of this fixture …
Run Code Online (Sandbox Code Playgroud)

php fixtures symfony fosuserbundle

5
推荐指数
1
解决办法
2889
查看次数

Laravel - 使用$ app而不是Facades

在Github上查看一些源代码时,我注意到一些软件包使用app容器本身来访问IOC,而不是Facades.你为什么要用这样的东西......

$app = app();
$user = $app['db']->connection()->table('users')->where('name', '=', 'Foo')->first();
Run Code Online (Sandbox Code Playgroud)

......而不是这个?

$user = User::where('name', '=', 'Foo')->first();
Run Code Online (Sandbox Code Playgroud)

facade inversion-of-control laravel

1
推荐指数
1
解决办法
2521
查看次数