1、MACPHP7.3安装phalcon扩展及安装phalcon-devtools 测试用例及ide支持

本文介绍了如何安装和配置Phalcon框架及其devtools,包括PSR扩展、Phalcon扩展的编译,使用Composer管理依赖,以及创建Controller、Model和进行单元测试的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码自动生成工具

https://github.com/phalcon/phalcon-devtools

1、安装扩展

The PSR extension is required to be loaded before Phalcon. Please ensure that it is available in your system

先安装 psr扩展

https://github.com/jbboehr/php-psr

git clone https://github.com/jbboehr/php-psr.git cd php-psr phpize ./configure make make test sudo make install

echo extension=psr.so | tee -a /usr/local/etc/php/7.3/php.ini

2、编译安装phalcon扩展

https://pecl.php.net/package/phalcon

wget https://pecl.php.net/get/phalcon-4.1.0.tgz tar -zxvf phalcon-4.1.0.tgz && cd phalcon-4.1.0 phpize ./configure make && make install

php.ini加入

extension=phalcon.so

php -m | grep phal phalcon

表明安装成功

3、composer安装phalcon-devtools

composer global require phalcon/devtools

Phalcon Documentation - Devtools

如果全局不能使用phalcon命令

在.bashrc 我用的是zsh 文件是.zshrc 结尾加入下面代码

export PATH="$HOME/.composer/vendor/bin:$PATH"

$ phalcon Phalcon DevTools (4.1.0) Available commands: info (alias of: i) commands (alias of: list, enumerate) controller (alias of: create-controller) module (alias of: create-module) model (alias of: create-model) all-models (alias of: create-all-models) project (alias of: create-project) scaffold (alias of: create-scaffold) migration (alias of: create-migration) webtools (alias of: create-webtools) serve (alias of: server) console (alias of: shell, psysh)

phalcon project phalcon_demo

生成一个controller

phalcon create-controller --name test

修改数据库配置

生成model

phalcon model user

后面是表名

安装phpunit

composer require --dev phpunit/phpunit:^9.0

创建目录 tests/Unit/

创建 phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         verbose="true"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">

    <testsuite name="Phalcon - Unit Test">
        <directory>./tests/Unit</directory>
    </testsuite>
</phpunit>

安装Phalcon Incubator Test

composer require --dev phalcon/incubator-test:^v1.0.0-alpha.1

First create a base Unit Test called AbstractUnitTest.php in your tests/Unit directory:

<?php

declare(strict_types=1);

namespace Tests\Unit;

use Phalcon\Di;
use Phalcon\Di\FactoryDefault;
use Phalcon\Incubator\Test\PHPUnit\UnitTestCase;
use PHPUnit\Framework\IncompleteTestError;

abstract class AbstractUnitTest extends UnitTestCase
{
    private bool $loaded = false;

    protected function setUp(): void
    {
        parent::setUp();

        $di = new FactoryDefault();

        Di::reset();
        Di::setDefault($di);

        $this->loaded = true;
    }

    public function __destruct()
    {
        if (!$this->loaded) {
            throw new IncompleteTestError(
                "Please run parent::setUp()."
            );
        }
    }
}

创建第一个测试用例FirstUnitTest.php

<?php

declare(strict_types=1);

namespace Tests\Unit;

class FirstUnitTest extends AbstractUnitTest
{
    public function testTestCase(): void
    {
        $this->assertEquals(
            "roman",
            "roman",
            "This will pass"
        );

        $this->assertEquals(
            "hope",
            "ava",
            "This will fail"
        );
    }
}

跑测试用例

./vendor/bin/phpunit

直接用phpunit 可能报错。是你系统的phpunit版本太低 升级 或者用vendor下面的

$ phpunit
PHPUnit 9.1.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.5 with Xdebug 2.9.5
Configuration: /var/www//phpunit.xml


Time: 3 ms, Memory: 3.25Mb

There was 1 failure:

1) Test\Unit\UnitTest::testTestCase
This will fail
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'hope'
+'ava'

/var/www/tests/Unit/UnitTest.php:25

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.

phalcon ide IDE 自动完成代码提示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值