0

I am learning unit testing with PHP and am following the TDD session on tutsplus: http://net.tutsplus.com/sessions/test-driven-php/

I have set up a ruby watchr script to run the PHPUnit unit tests every time a file is modified using Susan Buck's script: https://gist.github.com/susanBuck/4335092

I would like to change the ruby script so that in addition to testing a file when it is updated it will test all files that inherit from it. I name my files to indicate inheritance (and to group files) as Parent.php, Parent.Child.php, and Parent.Child.GrandChild.php, etc so the watchr script could just search by name. I just have no idea how to do that.

I would like to change:

watch("Classes/(.*).php") do |match|
 run_test %{Tests/#{match[1]}_test.php}
end

to something like:

watch("Classes/(.*).php") do |match|
 files = get all classes that inherit from {match[1]} /\b{match[1]}\.(.*)\.php/i
 files.each do |file|
  run_test %{Tests/{file}_test.php}
 end 
end

How do I do the search for file names in the directory? Or, is there an easier/better way to accomplish this?

Thanks

EDIT

This is what I ended up with:

watch("#{Library}/(.*/)?(.*).php") do |match|
 file_moded(match[1], match[2])
end

def file_moded(path, file)
 subclasses = Dir["#{Library}/#{path}#{file}*.php"]
 p subclasses
 subclasses.each do |file|
    test_file = Tests + file.tap{|s| s.slice!(".php")}.tap{|s| s.slice!("#{Library}")} + TestFileEnd
    run_test test_file
 end
end

Where Library, Tests, and TestFileEnd are values defined at the top of the file. It was also changed so that it will detect changes in subfolders to the application library and load the appropriate test file.

Aaron Luman
  • 635
  • 1
  • 10
  • 29

1 Answers1

1

I'm not entirely certain, but i think this will work:

watch("Classes/(.*).php") do |match|
  subclasses = Dir["Classes/#{match[1]}*.php"]
  filenames = subclasses.map do |file|
    file.match(/Classes\/(.*)\.php/)[1]
  end
  filenames.each do |file|
    run_test "Tests/#{file}_test.php"
  end
end

It's probably not the cleaneast way, but it should work.

The first line saves all the relative paths to files in the Classes directory beginning with the changed filename in subclasses. in the map block I use a regex to only get the filename, without any folder names or the .php extensions.

Hope this helps you