"Continuously integrate your project" to create automated tests and code coverage tests for your project

Source: Internet
Author: User
Tags autoload configuration php tag name composer install
This article takes the PHP project as an example to be owned (prepared):

    • GitHub account

    • A project

Look at the space is very big, inevitably have what omission, if the text has the wrong place, also please you treatise! Thank you. Because the original space is large, so there is no map, if there are a lot of people do not understand the feedback or failure, I later supplement. Thank you!

Travis-ci

Project to ensure that the project is always in a healthy and stable state, we need a continuous automated testing of the contribution of the Code of the service.

Travis-ci is in this context in 2011 to open the service, so far has more than 300k open source projects and 235k users in use.

Travis-ci's job is to automatically run in the virtual machine. Travis.yml The content set in the unit test, generate and export the report.

Composer

Open source projects typically have interdependent relationships, such as one component of project A that relies on another project B. When this dependency is more, you need a tool to manage dependencies.

Composer is a dependency management tool for PHP. It allows you to declare the code base on which the project depends, and installs them in your project. Composer Chinese

Packagist

Packagist is the main Composer repository. It Aggregates public PHP packages installable with Composer.

This site is the main composer warehouse, through the composer release of the project, the storage of the warehouse is this site, which is also composer installation relies on the download source. You can sign in with your GitHub account

You can submit your project after logging in, but you will need to have the Composer.json file in your project, which is described later.

Coveralls

In unit tests, code coverage is often used to measure the quality of the test.

The so-called code coverage is simply that after running the test case, the number of lines passed the code, for example, you want to test a function has 100 rows, but the test case only walked 80 lines, so the test case code coverage is 80%

Coveralls is a tool for analyzing the data exported from unit tests and showing code coverage. Can be used with many automated build tools, this article takes Travis-ci as an example.

The final directory structure of the project

/├──src/│   └──classname.php|──tests/|   ├──classnametest/|   |   └──classnametest.php|   └──bootstrap.php|──.coveralls.yml|──.travis.yml|──license|──readme.md|──composer.json|──example.php└── Phpunit.xml.dist

Here's how to start a specific configuration.

Composer

If you want to learn more in-depth composer can view the official documentation (address at the end of this section). An important concept is that every project is a bag of notes

So we first need to create a new Composer.json file at the root of the project , where the content is (we'll see what it means later)

{  "name": "Jshadowman/package",  "description": "This was a test package",  "version": "0.0.1",  "type": " Library ",  " keywords ": [" Database "," Logging "],  " license ":" MIT ",  " require ": [    " php ":" >=5.4.0 "  ],  "Require-dev": [    "Satooshi/php-coveralls": "*",    "Phpunit/phpunit": "*"  ],  " AutoLoad ": {    " files ": ["./src/classname.php "]  }}
  • Name: This field, as the name implies, should contain the Verdor name (vendor) and project name (project name). It is important to note that the value of this field should be lowercase , which is related to the repository publishing registration. Please refer to packagist for details.

  • Description: This field should be a brief introduction to this project. One line can

  • Version: the versions of the project are not required, and are recommended to ignore , see version Chinese for details

  • Type: Types of projects, optional values have library project Metapackage Composer-plugin specific please refer to type Chinese

  • Keywords: used when searching for keywords, can be an array keywords Chinese

  • License: Open source protocol used by project release, optional value please refer to license Chinese

  • Require: This represents the list of packages on which the project depends, and will not complete the installation unless those dependencies are met. Require Chinese

  • Require: Our project relies on platform packages, which are php,php extensions and some system class libraries. So we added a dependency on PHP in require, and if you have dependencies on other packages, you can fill them in this format. For details, please refer to platform-packages Chinese

  • Require-dev: This field lists dependencies that are only installed when tested and developed, and are additional dependencies. For details, please refer to Require-dev Chinese

  • Require & Require-dev: The list items below the two fields should be the package name-to-version mappings, where versions are written in many ways and can be filtered according to requirements. For details, please refer to package-links Chinese

  • AutoLoad: Represents an auto-load mapping for autoloader. For details, please refer to Autoload Chinese

  • AutoLoad: Some knowledge of the mapping relationship design to the PHP namespace (name space), please refer to psr-0-psr-4-Psr0-4_github

There are many optional fields and values in Composer.json, which can be written in document, Chinese documents

Coveralls

Create a new. coveralls.yml file at the root of the project, where the contents are

Coverage_clover:build/logs/clover.xmljson_path:build/logs/coveralls-upload.json
    • Coverage_clover: Represents an XML file in Clover XML format that uses the specified directory, with the default point to Build/logs/clover.xml

    • Json_path: Used to specify the JSON file that will be uploaded to the Coveralls Web site, by default pointing to Build/logs/coveralls-upload.json

It is important to note that the src_dir used in the previous version has been removed in the 1.0.0 version, so please be careful not to use this option

Removed Src_dir from coverallsconfiguration

There are other configuration options, please refer to Github-satooshi/php-coveralls

Next, add the warehouse to the Coveralls website:

    • Go to coveralls website https://coveralls.io/

    • In the upper-right corner, click Sign In, and then on the next page, select GitHub signs in, and then authorize your login with your GitHub account.

    • If you have not used coveralls, the login successful interface should be for you to add a code repository

    • In the add repo header, set your item from off to on in the following table

    • Next, configure the PHPUnit unit test.

PHPUnit

In your project root directory to create a new phpunit.xml.dist file, in fact, this file is not necessarily new in the root directory, mainly remember to modify the file contents of the path in the line , but preferably the root directory and the tests folder.

The content of the Phpunit.xml.dist file is

 
   
    
     
     
      ./tests 
      
     
                   
     
    
     
     
      ./SRC 
      
                     
     
      
       ./vendor 
                      
     
      
       ./tests 
              
   
      
       ./example.php 
       
             
   
     
    
    
     
     
    
   
  • attributes in the root element

    • Bootstrap indicates that a "Bootstrap" PHP file is run before the test runs and is typically used in conjunction with automatic onboarding in composer to ensure that no class is found

    • Colors indicates whether to use color output

    • Converterrorstoexceptionsphpunit will place an error handler to convert the error to an exception, and set to False to disable

    • Convertnoticestoexceptions This option is set to True, the error handler placed by Converterrorstoexceptions will E_notice, E_user_notice, e_strict The error is converted to an exception.

    • Convertwarningstoexceptions This option is set to True, the error handler that is placed by converterrorstoexceptions converts the e_warning or e_user_warning error to an exception.

  • Elements with one or more child elements for multiple sets of test suites

  • Filter, as the name implies, filters files or folders under the directory

  • Under the white list, that is the part that is needed

  • As the name implies, the directory that is needed

  • Below This is the part that needs to be excluded, the bottom of the exclusion item to see the tag name to know, you can exclude the directory or a single file

  • The final part is the contents of the log record.

    • Represents the exported Coverage-clover format file, the export file is named Build/logs/clover.xml

    • Indicates that the log is output directly to the standard output, which is on the terminal.

Full XML format, content can refer to XML configuration file

It is important to note that the attributes in the root element are not all on that page, and some are in the command-line options, so if you can't find it in Appendix C, go to the command-line option (note that the root element attribute is in the command-line option in the-delimited) section, which is sure to be found.

Travis-ci

    • Login to Travis-ci sign up using your GitHub account

    • Click on your avatar for the profile interface, and in your project below, click the button in front of the project you need to build automatically, this button will turn into a green tick.

    • After clicking on your user information interface, there will be a simple introduction on your repo, and opening travis-ci is easy.

Create a new. travis.yml file in your project root directory , where the contents are

language:phpphp:  -' 5.4 '  -' 5.5 '  -' 5.6 '  -' 7.0 ' Before_script:  -Composer Install--prefer-dist-- Dev--no-interactionscript:  -mkdir-p build/logs  -phpunit-c phpunit.xml.dist--coverage-clover build/logs/ Clover.xmlafter_script:  -travis_retry php vendor/bin/coveralls-v
    • Language: As the name implies, this is the language of your project, supported languages and formats can be consulted Document, configuration PHP

    • PHP: This is the environment under which automatic builds are used. Note that there is a fixed format

    • Before_script: As the name implies, a script (Shell) command that runs before a formal script

    • Script: The command used to start the test

    • After_script: Commands that run after the end of the test, such as for exporting results to Coveralls

Where the command is used to introduce

The preparation required to start the test, which is the dependent packages required to install the project.

Composer Install--prefer-dist--dev--no-interaction

    • The purpose of this command is to rely on the dependencies described by Composer.json, depending on the installation, please refer to the install Chinese

      • --prefer-dist:composer will get the dependent project from Dist as much as possible, which will significantly accelerate the installation on build servers

      • --dev: Install the packages listed in the Require-dev field

      • --no-interaction: Do not ask for any interaction issues. Because it is automatically dependent on the installation, we can not manually control, so there is no need to interact with the problem, we can not handle the

After the preparation is done, start the formal testing work, first of all, of course, you need to create a new directory to store the log

Mkdir-p Build/logs

    • This command causes the system to create a contiguous directory, and if the parent directory does not exist, the parent directory is created first

Start unit Testing and export the Code Coverage report

Phpunit-c phpunit.xml.dist--coverage-clover Build/logs/clover.xml

    • This command is run PHPUnit for unit testing, please refer to phpunit-command line options

      • -C Phpunit.xml.dist: Reads configuration information from the specified file, where the configuration file is Phpunit.xml.dist

      • --coverage-clover build/logs/clover.xml: Generate and export code coverage reports in Clover XML format

After the test, the next step is to export the report to Coveralls.

Travis_retry PHP vendor/bin/coveralls-v

    • This command is actually PHP vendor/bin/coveralls-v, the function of the previous travis_retry is to check the return value of the following command, if it is not 0 (the return value of 0 means the normal end), then repeat 3 times, if 3 times are not 0, then error.

    • PHP Vendor/bin/coveralls-v:

      • This command is the use of PHP to execute vendor/bin/under the coveralls this file,-V for verbose, that is, display detailed reports.

        After this command executes, you can see the detailed data in the Coveralls Web site.

The results of phpunit execution and the results of coveralls export can be seen under Travis-ci Build jobs

git push

The next step is to push these files onto GitHub, and Travis-ci will build them automatically, then start the unit test and send the code coverage in the test results to coveralls. So, a set of processes is over.

Show

Hard half a day, is to show their achievements ah. So we have to see the other people's projects underground such a beautiful icon we have to have AH.

Add in the Readme.md file (note Replace the following github_id with your own github-id, and replace Repo_name with your project name.) No angle brackets Oh ~ and attention to the case-sensitive OH ~ If you need to change the branch (branch), see the link you should also understand it? I believe you! )

[! [Build Status] (https://travis-ci.org/
 
  
   
  /
  
   
    
   . Svg?branch=master)] (https://travis-ci.org/
   
    
     
    /
    
     
      
     ) [![ Coverage Status] (https://coveralls.io/repos/github/
     
      
       
      /
      
       
         /badge.svg?branch=master) ] (https://coveralls.io/github/ 
        
          / 
         
           ? branch=master) 
          
         
       
     
      
    
     
   
    
  
   
 
  

In fact, these markdown statements can be copied directly

    • Travis-ci:build icon can be in the TRAVIS-CI site on the right side of the project name has a build:**** icon, directly click on this icon, the Image URL is changed to Makedown can be seen

    • Coveralls: Also into their own repo details, the middle is LATEST builds information, on the right there is a README BADGE, the bottom of the icon to the right there is a button Embed▾, click Copy markdown statement can be.

Packagist (optional)

In the sense of the development of their own projects, we can publish their own packages on the Packagist, after the release, you can be someone else's project through the composer rely on ~

  • You can log in using your GitHub account, or log in with your own account, in the top right-hand corner, sign in or login with your GitHub account.

  • Once you have registered, you can submit a package in the upper-right submit, click the Submit button-submit

  • The next step is to let you enter the Repository URL, directly enter git://github.com/ /

  • Packagit will clone your project in the background and check the project's Composer.json file, the first thing to check is the name of the package, if the package name has uppercase letters, PACKAGIT will report a package name should not have an uppercase error, so, this is said package name is best lowercase.

  • After submission, you can see some information about the package, such as how many times it was downloaded and how many times it was installed.

  • Go back to GitHub, open Settings-Webhooks & Services in the Code warehouse, then have an ADD service button to the right of the services and click Enter to find Packagit

  • You will then be asked to enter your username and token, which is on your Packagit home page

  • Personal homepage has a Your API token button, press the button, you can see their API token, pay attention to secrecy oh

  • Which packagist Sea oil 4 small icons, remember to replace packagist_id and Package_name Oh, not github_id and Repo_name OH

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.